Grid is used in almost every application. So its better to create a gird view helper than to create a grid on each page where it needs.
In this article I am going to discuss how to create a helper for creating grid with a single line of code.
Although you can create view helper wherever you want and can use that helper only by adding path to that helper and than call that view helper in your view template, however here I am going to create a gird helper in the library/Zend/View/Helper directory where all the helper shipped with Zend reside.
So lets have a look.
First create Grid.php in library/Zend/View/Helper directory and place the following code in it.
<?php
require_once ‘Zend/View/Helper/Abstract.php’;
class Zend_View_Helper_Grid2 extends Zend_View_Helper_Abstract
{
public $view = null;
public function setView(Zend_View_Interface $view)
{
$this->view = $view;
return $this;
}
/* this constructor takes 4 args, name of the grid, header columns, paginator and the bool value sorting.
public function grid ($name, $fields = null,$paginator=null,$sorting=false)
{
// taking value of sort using Fron controller getRequest() method.
$sort = Zend_Controller_Front::getInstance()->getRequest()->getParam(’sort’,'DESC’);
// checking and handling sorting.
if ($sort == ’ASC’) {
$sort = ‘DESC’;
} else {
$sort = “ASC”;
}
// start constructing the gird.
$output=”<div id=’”.$name.”‘>”;
$output .= “<table class=’grid’>
<thead>
<tr>”;
// this foreach loop display the column header in “th” tag.
foreach ($fields as $key => $value) {
$output .= “<th>”;
// check if sorting is true, if so add link to the fields headers
if ($sorting){
$output .= “<a href=’”.$this->view->url(array(’sort’=>$sort,’by’=>$key)).”‘>”.$value.”</a>”;
} else {
$output .= $value;
}
$output .= “</th>”;
}
$output .= “</tr>
</thead>”;
// constructing the body that contain the records in rows and columns.
$output .=”<tbody>”;
// this loop display the actual data.
foreach($paginator as $p) {
$output.=”<tr>”;
foreach($p as $record) {
$output .= “<td>”.$record.”</td>”;
}
$output.=”</tr>”;
}
$output .= “</tbody>”;
// check if paginator is ture, if so then add paginator component to the table “tfoot”.
if($paginator) {
$output .=”<tfoot>”;
$output .=”<td align=’center’ colspan=’”.count($fields).”‘>”;
$output.= $this->view->paginationControl($paginator,
‘Sliding’,
’pagination.phtml’);
$output .=”</tfoot>”;
}
$output .=”</table><div>”;
return $output;
}
}
In the code above we first extend our helper form the Zend_View_Helper_Abstract class. All view helper classes extend this abstract class.
Next we define function setView(Zend_View_Interface $view). This function is necessary if you want to access variable assign to you view template in your helper class.
The next method/constructor is the most important one. In constructor we are creating our gird.
The argument passed to this constructor are the name of the gird, fields array that will be displays as the column name of the grid.
The paginator variable is the object of paginator component.
In the fist lines we get the sort from the request if sent. Otherwise set its default value.
Next we create a table and check if paginator is set to true, if yes then we call paginator controller, passing it paginator instance, the type of the pagination, and the partial view helper.
That’s it, if you break the code you will easily understand what we are building.
After creating your helper class, its time to call it from your view template. Use the following code for this purpose.
<?
$fields = array(
‘id’ => ‘ID’,
‘firstname’ => ‘First Name’,
‘lastname’ => ‘Last Name’,
‘email’ => ‘Email’,
‘username’ =>’Username’
);
echo $this->grid(
’grd’,
$fields,
$this->paginator,
true);
?>
cheers.