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.

General Questions

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

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

Drag and drop order of Gridfield elements


Go to End
Reply

13 Posts   2623 Views

Avatar
esakrielaart

10 July 2012 at 3:10am Community Member, 56 Posts

Hi there,

is there any way to order elements in a gridfield using drag and drop, like the dataobjectmanager plugin used to do?

Friendly regards,
Maurice

Avatar
UndefinedOffset

10 July 2012 at 11:13am Community Member, 27 Posts

Shameless plug https://github.com/UndefinedOffset/SortableGridField, it actually missed getting merged into the core of SS 3.0

Avatar
Willr

10 July 2012 at 10:12pm Forum Moderator, 5511 Posts

Nice work UndefinedOffset!

Avatar
esakrielaart

10 July 2012 at 10:55pm Community Member, 56 Posts

Well that certainly makes live more easier, thanks!

Only one question, I think your documentation is good up to the point where you state " If you are using a many_many relationship you will need to do a custom getter to set the sort order of this relationship". How should I interpret this, I don't really have a clue on it.

Thanks in advance,
Maurice

Avatar
esakrielaart

11 July 2012 at 1:06am Community Member, 56 Posts

Hmm, it seems that fixSortColumn() causes some trouble here:

$owner = $gridField->Form->getRecord();
$sortColumn = $this->sortColumn;
$i = 1;
$many_many = ($list instanceof ManyManyList);
if ($many_many) {
   list($parentClass, $componentClass, $parentField, $componentField, $table) = $owner->many_many($gridField->getName());
}

this causes the query further on the line to not execute (for the many_many case at least). Does this have something to do with not setting a 'custom getter'? How should I fix that?

Thanks in advance,
Maurice

Avatar
esakrielaart

11 July 2012 at 1:29am Community Member, 56 Posts

Temporarely solved this by changing all queries inside if($many_many) { } to

DB::query('UPDATE "' . $table
   . '" SET "' . $sortColumn.'" = ' . $sortPositions[0]
   . ' WHERE "' . $this->componentField . '" = ' . $targetItem->ID . ' AND "' . $this->parentField . '" = ' . $owner->ID);

and putting the following statics in the class

public $table;
public $componentField;
public $parentField;

And naming them in the definition of the GridFieldSortableRows (inside the getCMSFields()):

$config = GridFieldConfig_RelationEditor::create();
$config->addComponent($sort = new GridFieldSortableRows('SortOrder'));
$sort->table = 'ProductHolder_Products';
$sort->parentField = 'ProductHolderID';
$sort->componentField = 'ProductID';

Well, that's it for now, I hope someone else may also find this useful. Also, I don't know i this is a nice way to do this and therefor I'm still interested in a possibly better, more clean way to achieve this.

Friendly regards,
Maurice

Avatar
UndefinedOffset

11 July 2012 at 2:30am (Last edited: 11 July 2012 2:37am), Community Member, 27 Posts

Can you post your code that defines the many_many relationship? As well as the line (or lines) you use to create the grid field instance? In my testing many_many relationships seem to work fine.

Avatar
esakrielaart

11 July 2012 at 2:35am Community Member, 56 Posts

Hi, this is the definition:

class Product:

static $belongs_many_many = array(
'ProductHolder' => 'ProductHolder'
);

class ProductHolder:

static $many_many = array(
'Products' => 'Product'
);

static $many_many_extraFields = array(
'Products' => array(
'Volgorde' => 'Int'
)
);

$productList = $this->Products();
$productList->sort('Volgorde');
$config = GridFieldConfig_RelationEditor::create();
$row = "Volgorde";
$config->addComponent($sort = new GridFieldSortableRows(stripslashes($row)));
$sort->table = 'ProductHolder_Products';
$sort->parentField = 'ProductHolderID';
$sort->componentField = 'ProductID';
$productfield = new GridField("Product", "Producten op deze pagina", $productList, $config);

$fields->addFieldToTab('Root.Product', $productfield);

return $fields;

Hope this is enough information, wondering if you spot what went wrong.

Friendly regards,
Maurice

Go to Top