Jump to:

23486 Posts in 18995 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » ss3 GridField

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

Page: 1
Go to End
Author Topic: 2736 Views
  • Futureweb
    Avatar
    Community Member
    232 Posts

    ss3 GridField Link to this post

    Hi,
    I'm in the process of playing with the alpha of silverstripe3. The 'datagrid' / GridField seems to be a crucial part of the CMS. I've got the gridfield working with a 'has_many' relationship to show a list of items. You can see my code here http://www.silverstriperesources.com/articles/new-articlepage/

    But, I don't know how I would 'add' another item. Eg, the old dataobjectmanager used to have a button that opened a popup window.
    As this is such a big part of setting up the CMS, I'm sure there must be a way of doing this.

    Has anyone found it yet? or am I being too hasty and has it just not be built yet

  • prawnstar
    Avatar
    Community Member
    116 Posts

    Re: ss3 GridField Link to this post

    Hi, Futureweb,

    I haven't made it to that point in my own exploration of SS3, but does something like this still work?

    $gridField->setPermissions(array('export', 'edit', 'delete', 'add'));

    Chris

  • Futureweb
    Avatar
    Community Member
    232 Posts

    Re: ss3 GridField Link to this post

    Thanks for the idea. but doesn't seem to work. Although of course it's all in prerelease so it's a bit of guessing game. Maybe it will work in the released version.... would be nice!

  • Futureweb
    Avatar
    Community Member
    232 Posts

    Re: ss3 GridField Link to this post

    ok, looks like you have to set up a config to the datagrid,

    $config = GridFieldConfig::create()

    $config->addComponent(new GridFieldFilter());
    $config->addComponent(new GridFieldDefaultColumns());
    $config->addComponent(new GridFieldAction_Delete());
    $config->addComponent(new GridFieldAction_Edit());

    $itemsInGrid = DataList::create('GalleryItem');
    $gridField = new GridField("GalleryItems", "Images in this gallery", $itemsInGrid,$config);

    this makes the buttons, but the buttons don't do anything... looks like you have to define the action somewhere on the items
    also, haven't yet found a way to 'add'

    the pages, and file management pages in the CMS have add buttons in the left col rather than on the page. But the graphic designs for these pages include add buttons on the pages, so it might just not be implemented yet

  • Futureweb
    Avatar
    Community Member
    232 Posts

    Re: ss3 GridField Link to this post

    Just an update. I still haven't figured how to add the simple, but very important 'add new' button, but I have got the the data grid field working better, with edit, and remove buttons.

    You can see my code here http://www.silverstriperesources.com/articles/using-the-silverstripe-3-gridfield

  • Gerry S
    Avatar
    Community Member
    2 Posts

    Re: ss3 GridField Link to this post

    I hope the great SS team will include the "Add" methods.

    Hope to share this code snippet to temporarily make the prototype work.
    1. via Front end
    a. Copy CSS into your local class
    copy GridField.css to <<myclass>>.css
    then replace ".cms table.ss-gridfield.field" with ".ss-gridfield.field"

    b. Create html "Add" and "Print" html button
    class <<myclasspage>>_Controller extends Page_Controller {

       function Form() {
    // Form name to be called in layout <formname>.ss or page.ss
    $formName = 'Form';       
          // Create fields
          $fields = $this->getCMSFields(); //<--- this includes the GridField
          // Create actions
          $addbtn = new FormAction('doAdd', 'Add'); //<--- this will be next to the Save button
          $printbtn =new FormAction('doPrint', 'Print');
          $actions = new FieldSet(array($addbtn,$printbtn));      
          // create the form
          $form = new Form($this, $formName, $fields, $actions);
          return $form;
       }
    function doAdd($data, $form) {      
    $this->redirect( "http://localhost/mydemo/myaddpage/" ); //<--- just a typical add form
          return;
       }

    class MyAddPage_Controller extends Page_Controller {

       public static $allowed_actions = array (
       );

    function init() {
    parent::init();
    Requirements::customCSS("#ModelAdminPanel {overflow:auto;}");
    }
    function Form() {

    // Form name to be called in layout <formname>.ss or page.ss
    $formName = 'Form';
    // Create fields
       $fields = Partners::getCMSFields();
       // Create actions
       $actions = new FieldSet(new FormAction('doSave', 'Save'));   
       // create the form
       $form = new Form($this, $formName, $fields, $actions); //, $validator);
       return $form;
       }
    function doSave($data, $form) {
       $table = new MyClass(); //Setup class for Model DB
       $form->saveInto($table); //Move Form fields into DB fields
    $table->MyClassPageID = $this->dataRecord->ID;
    $table->write();
       $this->redirectback();
       return;
       }

    2. via CMS backend
    Add a new ModelAdmin
    class <<myclassmodeladmin>> extends ModelAdmin {
       public static $managed_models = array ('myclass');
       static $url_segment = 'democlass';
       static $menu_title = 'My Demo';

    2736 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.