Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Drag and drop reordering of SS3 Grid Field


Go to End


7 Posts   8970 Views

Avatar
Fraser

Community Member, 48 Posts

19 July 2012 at 3:13pm

Edited: 19/07/2012 3:14pm

I seem to be very good at creating ghost town posts on this forum but thought I'd have one last attempt.

There is very minimal documentation out there on SS3 Grid Fields at the moment. I am looking for a way to achieve drag and drop (or another method) reordering of the items in a Grid Field. Using the dataobjectmanager in SS2.4 I just had to add SortableDataObject::add_sortable_class('Classname') to my config file and hey presto, I could order the items.

Does anyone know how I can achieve this with Grid Fields in SS3?

Avatar
(deleted)

Community Member, 473 Posts

19 July 2012 at 3:46pm

Hey Fraser

There's a Sortable Grid Field module that provides a component for allowing drag and drop reordering of GridField objects. You just need to provide a sort field.

Avatar
Fraser

Community Member, 48 Posts

19 July 2012 at 3:52pm

Perfect.

Thanks Simon.

Avatar
LuiLoop

Community Member, 2 Posts

4 January 2013 at 5:47am

Hi,

i have installed this plugin but i dont have an idea how i can use this module.

I try it on an example from the official tutorial (http://doc.silverstripe.com/framework/en/tutorials/5-dataobject-relationship-management) but it doesnt work.

Where i can find a usable tutorial?

Avatar
copernican

Community Member, 189 Posts

4 January 2013 at 5:58am

Edited: 04/01/2013 5:58am

Hi LuiLoop. Setting up the SortableGridField is pretty easy. Here's an example

In your DataObject class, you need to first create a column to sort by. I usually use something simple like SortID.

class MyDataObject extends DataObject {
    public static $db=array(
        'SortID'=>'Int'
    );
}

then where you're setting up your gridfield, add the SortableGridFieldComponent.

class Page extends SiteTree {

    public static $has_many=array(
        'MyDataObjects'=>'MyDataObject'
    );

    public function getCMSFields(){
        $fields=parent::getCMSFields();
         
        $fields->addFieldToTab('Root.Main', 
                GridField::create('MyDataObjects', 'Data Object', 
                        $this->MyDataObjects(), 
                        GridFieldConfig_RecordEditor::create()
                        ->addComponent(new GridFieldSortableRows('SortID')) //use addComponent() function to add SortableGridFieldRows
                    )
                );       

        return $fields;
    }
}

hope that helps!

Avatar
LuiLoop

Community Member, 2 Posts

4 January 2013 at 8:31pm

Edited: 04/01/2013 8:56pm

thx, now it works

But i have a litle second question. How i can create a DropDownField in my Gridfield - i cant find any documentation

Avatar
datswicked

Community Member, 15 Posts

6 January 2013 at 9:39pm

Edited: 06/01/2013 9:40pm

Hi LuiLoop,

thanks for this post was very helpfull just now... with the dropdown list, i discovered i could use an Enum data type.
worked for what i needed, let me know if that works for you or if you find another way..

  static $db = array(
        'Name' => 'Varchar(255)',
		'Title' => 'Varchar(255)',
		'Visible' => "Enum('Yes, No')",
		 'SortID'=>'Int', 
    );