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

SS3: SortableGridFieldComponent only sorts when allow drag and drop selected


Reply

3 Posts   407 Views

Avatar
MarijnKampf

20 February 2013 at 1:50am Community Member, 164 Posts

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();
}
}

Avatar
IOTI

20 February 2013 at 4:56am (Last edited: 20 February 2013 4:56am), Community Member, 189 Posts

Hi Marijn

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

public static $default_sort='SortOrder';

Regards

Avatar
MarijnKampf

20 February 2013 at 6:24am Community Member, 164 Posts

Hi Ioti,

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

Cheers,

Marijn.