« Return to Thread: Datagrid in Zend Framework

Re: Datagrid in Zend Framework

by Laurent Melmoux :: Rate this Message:

Reply to Author | View in Thread

Hi Filipe,

I have done it base on 3 others component:

Mmx_Data_Sort
Mmx_Data_Criterion
Mmx_Data_Pager

There is no coupling between components so you can use them
individually. The rendering part is done as in Zend_Form : a
__toString() method is making use of view helpers.

May be http://xyster.devweblog.org/ and Xyster_Data is worth a like look
as component to build something upon. I haven’t time to get a closer
look though.

A basic use case:


// Data Grid
Zend_Loader::loadClass('Mmx_Data_Grid');
// Params: grid name, filter data, sort column
$grid = new Mmx_Data_Grid('users', $_POST, @$_GET['sort']);

$grid->addElement('integer', 'user_id');
$grid->addElement('integer', 'acl_role_id');
$grid->addElement('string', 'username');
$grid->addElement('string', 'firstname');
$grid->addElement('string', 'lastname');
$grid->addElement('boolean', 'isActive');

$grid->setDefaultSort('username');

$rowsPerPage = $this->getRequest()->getParam('limit');
$currentPage = $this->getRequest()->getParam('page', 1);
$totalRows = $usersTable->count($grid->getFilterCriteria());

$grid->initPager($totalRows, $rowsPerPage, $currentPage);

// Query DB
$rows = $usersTable->fetchAll($grid->getFilterCriteria(),
$grid->getSortCriteria(),$grid->getRowsPerPage(), $grid->getPageOffset());

$grid->populate($rows);

// Grid Renderer
$options = array(
'labels' => $this->_labels,
'pkname' => 'user_id',
'urlbase' => $this->view->url(array('page'=>'@@page@@'), null, false,
false),
'urledit' => $this->view->url(array('action'=>'edit',
'page'=>null)).'/@@id@@?Mmx_Back=1',
'urldelete'=>
$this->view->url(array('action'=>'delete')).'/@@id@@?Mmx_Back=1'
);

$grid->setDecorators($options);

$this->view->grid = $grid;

--
Laurent Melmoux
Conseils et Solutions Web | laurent@...
2mx - Annecy, France | http://www.2mx.fr/


Filipe Carvalho a écrit :

>
> Hi all,
>
> I'm looking for something like phpGrid (http://www.phpgrid.com/). Not
> so advanced... i will be happy in having out the box features like:
> - Paging
> - Sorting columns
> - Capability to create links values of some columns
>
> It is not hard to do it, but using model-view-controller pattern I
> can't realize how to do something clean and reusable.
>
> Can you give some clues how to do it?
>
> Best Regards,
>
> Filipe Carvalho
>

 « Return to Thread: Datagrid in Zend Framework

LightInTheBox - Buy quality products at wholesale price