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

SS3: SortableGridFieldComponent only sorts when allow drag and drop selected


Go to End


3 Posts   855 Views

Avatar
MarijnKampf

Community Member, 176 Posts

20 February 2013 at 1:50am

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
copernican

Community Member, 189 Posts

20 February 2013 at 4:56am

Edited: 20/02/2013 4:56am

Hi Marijn

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

public static $default_sort='SortOrder';

Regards

Avatar
MarijnKampf

Community Member, 176 Posts

20 February 2013 at 6:24am

Hi Ioti,

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

Cheers,

Marijn.