SortableGridField
SortableGridField copied to clipboard
Drag and Drop sorting for ModelAdmin is not working
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;
}
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.
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 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 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.
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 :(
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. :-)