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.

General Questions

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

ss3 GridField


Reply

6 Posts   2757 Views

Avatar
Futureweb

25 January 2012 at 11:32am Community Member, 233 Posts

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 [url]http://www.silverstriperesources.com/articles/new-articlepage/[/url]

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

Avatar
prawnstar

26 January 2012 at 10:07am Community Member, 116 Posts

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

Avatar
Futureweb

26 January 2012 at 11:03am Community Member, 233 Posts

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!

Avatar
Futureweb

26 January 2012 at 11:53am Community Member, 233 Posts

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

Avatar
Futureweb

9 February 2012 at 9:24am Community Member, 233 Posts

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 [url]http://www.silverstriperesources.com/articles/using-the-silverstripe-3-gridfield[/url]

Avatar
Gerry S

12 March 2012 at 12:31am (Last edited: 12 March 2012 12:42am), Community Member, 2 Posts

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';