Difference between revisions of "API/Grid"

From aMember Pro Documentation
Jump to: navigation, search
Line 11: Line 11:
 
     function createGrid()
 
     function createGrid()
 
     {
 
     {
         $ds = new Am_Query(BanTable::getInstance());
+
        // create query to database table
 +
         $ds = new Am_Query($this->getDi()->banTable); // inside controllers, plugins and tables, Am_Di instance is available with this method call: $this->getDi()
 +
        // create grid with id, title, query, pass request and view
 
         $g = new Am_Grid_Editable('_ban', "Ban Records", $ds, $this->_request, $this->view);
 
         $g = new Am_Grid_Editable('_ban', "Ban Records", $ds, $this->_request, $this->view);
 +
        // set form creation callback for edit/add forms in grid
 
         $g->setForm(array($this, 'createForm'));
 
         $g->setForm(array($this, 'createForm'));
 +
        // add fields to grid. @see Am_Grid_Field
 
         $g->addGridField("type", "Type");
 
         $g->addGridField("type", "Type");
 
         $g->addGridField("value", "Locked Value");
 
         $g->addGridField("value", "Locked Value");
Line 21: Line 25:
 
     public function createForm(Am_Grid_Editable $grid)
 
     public function createForm(Am_Grid_Editable $grid)
 
     {
 
     {
 +
        // create admin-side form
 
         $form = new Am_Form_Admin;
 
         $form = new Am_Form_Admin;
 +
        // add selection box with 2 options
 
         $form->addSelect("type")->setLabel(array("Type")->loadOptions(array('ip'=>'IP', 'email'=>'E-Mail',));
 
         $form->addSelect("type")->setLabel(array("Type")->loadOptions(array('ip'=>'IP', 'email'=>'E-Mail',));
 +
        // add text field for "value" field of ban table
 
         $form->addText("value", array('size' => 40))->setLabel(array("Value", "You can use % as wildcard mask"));
 
         $form->addText("value", array('size' => 40))->setLabel(array("Value", "You can use % as wildcard mask"));
 +
        // add text field for "comment" field of ban table
 
         $form->addText('comment', array('size' => 40))->setLabel("Comment");
 
         $form->addText('comment', array('size' => 40))->setLabel("Comment");
 +
        // buttons will be added by grid
 
         return $form;
 
         return $form;
 
     }
 
     }
 
     public function indexAction()
 
     public function indexAction()
 
     {
 
     {
 +
        // now just run what we created before, and put grid output to response object of controller
 
         $this->createGrid()->run($this->getResponse());
 
         $this->createGrid()->run($this->getResponse());
 
     }
 
     }
 
}
 
}
 
</source>
 
</source>

Revision as of 07:10, 17 November 2011

The grid component we developed from scratch provides very easy way to deal with table records - editing and displaying. There is a simple example of usage. You can save it to file AdminSampleBanController.php and copy to am/application/default/controllers/ , then access as http://example.com/am/admin-sample-ban

<?php
class AdminSampleBanController extends Am_Controller
{
    public function checkAdminPermissions(Admin $admin)
    {
        return $admin->isSuper();
    }
    function createGrid()
    {
        // create query to database table
        $ds = new Am_Query($this->getDi()->banTable); // inside controllers, plugins and tables, Am_Di instance is available with this method call: $this->getDi()
        // create grid with id, title, query, pass request and view
        $g = new Am_Grid_Editable('_ban', "Ban Records", $ds, $this->_request, $this->view);
        // set form creation callback for edit/add forms in grid
        $g->setForm(array($this, 'createForm'));
        // add fields to grid. @see Am_Grid_Field
        $g->addGridField("type", "Type");
        $g->addGridField("value", "Locked Value");
        $g->addGridField("comment", "Comment");
        return $g;
    }
    public function createForm(Am_Grid_Editable $grid)
    {
        // create admin-side form
        $form = new Am_Form_Admin;
        // add selection box with 2 options
        $form->addSelect("type")->setLabel(array("Type")->loadOptions(array('ip'=>'IP', 'email'=>'E-Mail',));
        // add text field for "value" field of ban table
        $form->addText("value", array('size' => 40))->setLabel(array("Value", "You can use % as wildcard mask"));
        // add text field for "comment" field of ban table
        $form->addText('comment', array('size' => 40))->setLabel("Comment");
        // buttons will be added by grid
        return $form;
    }
    public function indexAction()
    {
        // now just run what we created before, and put grid output to response object of controller
        $this->createGrid()->run($this->getResponse());
    }
}