问题
Silverstripe DataObject - I want to add drag and drop ordering to a current Class that extends dataobject like what exists for pages. So when dropped it updates an OrderBy field for all the dataobjects in that view. I created the class and can freely edit one item at a time, but a simple drag and drop ordering would make it so much easier but I can not see any such extensions currently on Dataobjects only on Pages.
回答1:
In SilverStripe 3.1 there are a few excellent modules that give you this sort of functionality. Two of these modules are SortableGridField and GridFieldExtensions.
To get this working you need to install one of these modules, add a sort field to your custom DataObject
class and add the module sort object component to your GridFieldConfig
.
SortableGridField
The SortableGridField module is specifically to allow sorting functionality for objects on a GridField
.
To get this working you need to add a sort field to your custom DataObject
class and add GridFieldSortableRows()
as a component to your GridField
.
For the following examples I will use HomePage
as the page with a has_many relationship to a Slide
DataObject.
Slide
class Slide extends DataObject
{
private static $db = array (
'Title' => 'HTMLText',
'SortOrder' => 'Int'
);
private static $has_one = array (
'HomePage' => 'HomePage'
);
private static $summary_fields = array(
'Title' => 'Title'
);
private static $default_sort = 'SortOrder ASC';
private static $singular_name = 'Slide';
private static $plural_name = 'Slides';
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->removeByName('SortOrder');
return $fields;
}
}
HomePage
class HomePage extends Page {
private static $has_many = array (
'Slides' => 'Slide'
);
public function getCMSFields()
{
$fields = parent::getCMSFields();
$slidesFieldConfig = GridFieldConfig_RecordEditor::create();
$slidesFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));
$slidesField = GridField::create(
'Slides',
'Slide',
$this->Slides(),
$slidesFieldConfig
);
$fields->addFieldToTab('Root.Slides', $slidesField);
return $fields;
}
}
GridFieldExtensions
The GridFieldExtensions
module contains GridFieldOrderableRows to control the sort order on a GridField, just like the SortableGridField
module. It also has other useful GridField
tools.
To get this working you need to add a sort field to your custom DataObject
class and add GridFieldOrderableRows()
as a component to your GridField
.
Your code would be just like the above example except the component you add to your GridFieldConfig
is GridFieldOrderableRows
:
public function getCMSFields()
{
$fields = parent::getCMSFields();
$slidesFieldConfig = GridFieldConfig_RecordEditor::create();
$slidesFieldConfig->addComponent(new GridFieldOrderableRows('SortOrder'));
...
}
来源:https://stackoverflow.com/questions/28844929/silverstripe-dataobject-drag-and-drop-ordering