I have a ModelAdmin
with MyDataObject
has_many AnotherDataObject
and SilverStripe Grid Field Extensions Module that is controlling the
class TestAdmin extends ModelAdmin {
static $managed_models = array('MyDataObject');
static $url_segment = 'testadmin';
static $menu_title = 'TestAdmin';
}
class MyDataObject extends DataObject {
private static $db = array('Name' => 'Varchar(255)');
private static $has_many= array('AnotherDataObjects' => 'AnotherDataObject');
function getCMSFields() {
$fields = parent::getCMSFields();
if ($grid = $fields->dataFieldByName('AnotherDataObjects')) {
$grid->getConfig()
->removeComponentsByType('GridFieldAddExistingAutocompleter')
->addComponent(new GridFieldOrderableRows('Priority'));
$fields->removeByName('AnotherDataObjects');
$fields->insertAfter($grid,'Name');
}
return $fields;
}
}
class AnotherDataObject extends DataObject {
private static $db = array(
'Name' => 'Varchar(255)',
'Type' => "Enum('Middle,Top,End','Middle')",
'Priority' => 'Int'
);
private static $has_one = array('MyDataObject' => 'MyDataObject');
function onBeforeWrite() {
parent::onBeforeWrite();
if($this->Type == 'Top')
$this->Priority = DB::query("SELECT MIN(Priority) FROM Type = 'Top'")->value();
if($this->Type == 'End')
$this->Priority = DB::query("SELECT MAX(Priority) FROM AnotherDataObjectWHERE Type = 'End'")->value() + 1;
}
}
I'd like to use Type
on AnotherDataObject
to control the sort order so that the Top items remain at the top and then End at the end. I think the easit way would be to prevent the ability to "drap" or "drop" into those positions and control the Priority for the Top/End items within onBeforeWrite
... but I'm not sure how to disable those from being "drag/drop"... or if there is a better way?
You need to override jQuery UI sortable initialisation:
$(".ss-gridfield-orderable tbody").entwine({
onadd: function() {
// skipped code
// the `cancel` option allows to exclude items from drag&drop sorting
this.sortable({
handle: ".handle",
helper: helper,
opacity: .7,
update: update,
cancel: ".ui-state-nosort"
});
},
});
You will need to add new class '.ui-state-nosort' to the rows that you want to exclude from sorting.