diff --git a/README.md b/README.md index 94578e7..d641910 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ To move an item to another page drag the row over the respective page button and #### Full code Examples * [has_many relationship] (https://github.com/UndefinedOffset/SortableGridField/blob/master/docs/HasManyExample.md) * [many_many relationship] (https://github.com/UndefinedOffset/SortableGridField/blob/master/docs/ManyManyExample.md) +* [ModelAdmin implementation] (https://github.com/UndefinedOffset/SortableGridField/blob/master/docs/ModelAdminExample.md) #### Events GridFieldSortableRows provides 4 "events" onBeforeGridFieldRowSort(), onAfterGridFieldRowSort(), onBeforeGridFieldPageSort() and onAfterGridFieldPageSort(). These "events" are passed a clone of the DataList used in GridFieldSortableRows, in the case of page sorting this list has a limit that shows you the current page plus/minus one object. For GridFieldSortableRows that are on ModelAdmin decendents these events are called on the ModelAdmin if they do not have a owner DataObject, if you are using GridFieldSortableRows on a GridField for a DataObject's relationship the events are called on that DataObject. diff --git a/docs/HasManyExample.md b/docs/HasManyExample.md index 748bc47..6e648af 100644 --- a/docs/HasManyExample.md +++ b/docs/HasManyExample.md @@ -1,5 +1,6 @@ has_many Example ================= +Please note this example is written with 3.0.x in mind, if you are using 3.1.x make sure you scope all static properties to private not public. ```php /*** TestPage.php ***/ class TestPage extends Page { diff --git a/docs/ManyManyExample.md b/docs/ManyManyExample.md index 77fb0ef..90c9ae0 100644 --- a/docs/ManyManyExample.md +++ b/docs/ManyManyExample.md @@ -1,5 +1,6 @@ many_many Example ================= +Please note this example is written with 3.0.x in mind, if you are using 3.1.x make sure you scope all static properties to private not public. ```php /*** TestPage.php ***/ class TestPage extends Page { diff --git a/docs/ModelAdminExample.md b/docs/ModelAdminExample.md new file mode 100644 index 0000000..04082a4 --- /dev/null +++ b/docs/ModelAdminExample.md @@ -0,0 +1,38 @@ +ModelAdmin implementation Example +================= +Please note this example is written with 3.0.x in mind, if you are using 3.1.x make sure you scope all static properties to private not public. +```php +/**** MyModelAdmin.php ****/ +class MyModelAdmin extends ModelAdmin { + public static $menu_title='My Model Admin'; + public static $url_segment='my-model-admin'; + + public static $managed_models=array( + 'MATestObject' + ); + + public function getEditForm($id = null, $fields = null) { + $form=parent::getEditForm($id, $fields); + + //This check is simply to ensure you are on the managed model you want adjust accordingly + if($this->modelClass=='MATestObject' && $gridField=$form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass))) { + //This is just a precaution to ensure we got a GridField from dataFieldByName() which you should have + if($gridField instanceof GridField) { + $gridField->getConfig()->addComponent(new GridFieldSortableRows('SortOrder')); + } + } + + return $form; + } +} + +/**** MATestObject.php ****/ +class MATestObject extends DataObject { + public static $db=array( + 'Title'=>'Varchar', + 'SortOrder'=>'Int' + ); + + public static $default_sort='SortOrder'; +} +``` \ No newline at end of file