SortableGridField icon indicating copy to clipboard operation
SortableGridField copied to clipboard

Drag and Drop sorting for ModelAdmin is not working

Open makolivares opened this issue 5 years ago • 6 comments

I'm using Silverstripe 4.

I've created a DataObject that will call using ModelAdmin. I've tried your guide on how to implement it but I'm afraid it is not working https://github.com/UndefinedOffset/SortableGridField/blob/master/docs/ModelAdminExample.md

My DataObject already have

private static $default_sort = 'SortOrder';

DistributorAdmin.php

public function getEditForm($id = null, $fields = null)
    {
        $form = parent::getEditForm($id, $fields);
        
        //This check is simply to ensure you are on the managed model you want adjust accordingly
        if($this->modelClass == 'Distributor' && $gridField = $form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass))) {
            //This is just a precaution to ensure we got a GridField from dataFieldByName() which you should have
            if($gridField instanceof GridField) {
                $gridField->getConfig()->addComponent(new GridFieldSortableRows('SortOrder'));
            }
        }

        return $form;
    }

makolivares avatar Apr 01 '19 07:04 makolivares

Are you getting any error? Does the allow sort checkbox show up? Also remember that $this->modelClass is the full namespaced class name so if your Distributor class is in a namespace then you will need to use $this->modelClass == Distributor::class instead (changing to that may not be a bad thing either.

UndefinedOffset avatar Apr 02 '19 12:04 UndefinedOffset

I can confirm the sorting does not work. I use Silverstripe 4.5.0. The "Allow sort" checkbox shows up, I can sort DataObjects, but once I untick the checkbox / leave the ModelAdmin, everything returns to the original order. I get no errors.

janherman avatar Mar 17 '20 20:03 janherman

@janherman do you have DataObject's default_sort set to your sort field or the list going into the modal admin sorted by the sort field? Sortable grid field will not change the sort order of the list used to populate the grid field, this maybe causing the issue you are seeing.

UndefinedOffset avatar Mar 17 '20 20:03 UndefinedOffset

@UndefinedOffset Yes, I had default_sort set in my DataObject.

As it turned out, the problem was I set my sort field as translatable (I use Silverstripe Fluent for translations). I needed slightly different order of DataObjects in each language and I thought this could work. If I disable translation for sort field, sorting works again.

janherman avatar Mar 17 '20 21:03 janherman

AH that'll do it, because of how fluent works and how sortable grid field works they're in compatible with each other. You'd need to update the localized tables which sortable grid field of course doesn't know about. In theory it would be a relatively simple extension to do this, if I can find the time I can try to put something together that may work for you if you want. But it may not be for a few days :(

UndefinedOffset avatar Mar 18 '20 12:03 UndefinedOffset

Thanks, @UndefinedOffset! Please do not worry about it right now. Because the number of DataObjects in my case is relatively small, I added simple numeric field to each of them to determine the order. Not so elegant, but suitable for the purpose.

If you find time for the extension one day, it will be great of course. :-)

janherman avatar Mar 18 '20 13:03 janherman