Jump to:

23371 Posts in 18161 Topics by 2865 members

General Questions

SilverStripe Forums » General Questions » Drag and drop order of Gridfield elements

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

Page: 1 2
Go to End
Author Topic: 2448 Views
  • esakrielaart
    Avatar
    Community Member
    54 Posts

    Drag and drop order of Gridfield elements Link to this post

    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

  • UndefinedOffset
    Avatar
    Community Member
    27 Posts

    Re: Drag and drop order of Gridfield elements Link to this post

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

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Drag and drop order of Gridfield elements Link to this post

    Nice work UndefinedOffset!

  • esakrielaart
    Avatar
    Community Member
    54 Posts

    Re: Drag and drop order of Gridfield elements Link to this post

    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

  • esakrielaart
    Avatar
    Community Member
    54 Posts

    Re: Drag and drop order of Gridfield elements Link to this post

    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

  • esakrielaart
    Avatar
    Community Member
    54 Posts

    Re: Drag and drop order of Gridfield elements Link to this post

    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

  • UndefinedOffset
    Avatar
    Community Member
    27 Posts

    Re: Drag and drop order of Gridfield elements Link to this post

    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.

  • esakrielaart
    Avatar
    Community Member
    54 Posts

    Re: Drag and drop order of Gridfield elements Link to this post

    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

    2448 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.