Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drag and Drop sorting for ModelAdmin is not working #125

Open
makolivares opened this issue Apr 1, 2019 · 6 comments
Open

Drag and Drop sorting for ModelAdmin is not working #125

makolivares opened this issue Apr 1, 2019 · 6 comments

Comments

@makolivares
Copy link

makolivares commented Apr 1, 2019

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;
    }
@UndefinedOffset
Copy link
Owner

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.

@janherman
Copy link

janherman commented Mar 17, 2020

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.

@UndefinedOffset
Copy link
Owner

@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.

@janherman
Copy link

@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.

@UndefinedOffset
Copy link
Owner

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 :(

@janherman
Copy link

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. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants