Jump to:

23454 Posts in 18904 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » SS3: SortableGridFieldComponent only sorts when allow drag and drop selected

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
Go to End
Author Topic: 391 Views
  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    SS3: SortableGridFieldComponent only sorts when allow drag and drop selected Link to this post

    I'm trying to sort a GridField in SS3. Installed the module SortableGridFieldComponent, which only sorts when 'Allow drag and drop' is selected, as soon as the checkbox is unchecked or the page is saved the sort order reverts to the order they dataobjects where created in. Anyone any ideas?

    // HomePage
    class HomePage extends Page {
       static function isLocalhost() {
          return Utils::isLocalHost();
       }

       public static $has_one = array(

       );
       
    public static $has_many = array(
    "Thumbnails" => "Thumbnail",
    );

       function getCMSFields() {
          $fields = parent::getCMSFields();

          $gridFieldConfig = GridFieldConfig_RecordEditor::create()->addComponents(new GridFieldDeleteAction('unlinkrelation'));
          $gridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));
          $gridField = new GridField("Thumbnails", "Thumbnails", $this->Thumbnails(), $gridFieldConfig);
          $fields->addFieldToTab("Root.Thumbnails", $gridField);      
          return $fields;
       }
    }

    // Thumbnails
    <?php

    class Thumbnail extends DataObject {
       public static $db = array(
          'Name' => 'Text',
          'Caption' => 'Text',
          'SortOrder'=>'Int'
       );

    public static $has_one = array(
       "Image" => "Image",
    "HomePage" => "HomePage",
    "LinkedPage" => "Page"
    );

    // Summary fields
    public static $summary_fields = array(
    'Thumbnail' => 'Thumbnail',
    'Name' => 'Name',
    'Caption' => 'Caption'
    );

    public function getThumbnail() {
    return $this->Image()->CMSThumbnail();
    }
    }

  • IOTI
    Avatar
    Community Member
    189 Posts

    Re: SS3: SortableGridFieldComponent only sorts when allow drag and drop selected Link to this post

    Hi Marijn

    Make sure you set your $default_sort on your DataObject like so:

    public static $default_sort='SortOrder';

    Regards

  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Re: SS3: SortableGridFieldComponent only sorts when allow drag and drop selected Link to this post

    Hi Ioti,

    Thanks, I knew it would be something simple that I missed!

    Cheers,

    Marijn.

    391 Views
Page: 1
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.