📜 ⬆️ ⬇️

ZendX_JQuery + jqGrid

Continuing the theme of the Zend / Doctrine bundle.
Considering that it would be necessary to resolve the issue of paginal listing of data from the table, it was decided to use jqGrid as this tool.
The reason for the choice is simple - at the moment it is one of the most powerful grids, with good documentation, and so on.

1) Create a View_Helper. In our example, we create this class in our library - Xms_ZendX_JQuery_View_Helper_jqGrid

public function jqGrid ( $id , array $params = array ( ) ) <br/>
{ <br/>
$params = $this -> _adjuctParams ( $params ) ; <br/>
if ( count ( $params ) > 0 ) { <br/>
$encodedParams = ZendX_JQuery :: encodeJson ( $params ) ; <br/>
} else { <br/>
$encodedParams = "{}" ; <br/>
} <br/>
<br/>
$js = sprintf ( '%s("#%s").jqGrid(%s);' , <br/>
ZendX_JQuery_View_Helper_JQuery :: getJQueryHandler ( ) , <br/>
$id , <br/>
$encodedParams <br/>
) ; <br/>
$this -> jquery -> addOnLoad ( $js ) ; <br/>
$this -> view -> headscript ( ) -> appendFile ( '/js/jquery/jquery.jqGrid.js' ) ; <br/>
$this -> view -> headLink ( ) -> appendStylesheet ( '/css/jsgrid/steel/grid.css' ) ; <br/>
<br/>
$html = "<table id=" '. $id . ' "></table>" ; <br/>
$html .= '<div id="' . $params [ 'pager' ] . '" class="scroll" style="text-align:center;"></div>' ; <br/>
return $html ; <br/>
}
public function jqGrid ( $id , array $params = array ( ) ) <br/>
{ <br/>
$params = $this -> _adjuctParams ( $params ) ; <br/>
if ( count ( $params ) > 0 ) { <br/>
$encodedParams = ZendX_JQuery :: encodeJson ( $params ) ; <br/>
} else { <br/>
$encodedParams = "{}" ; <br/>
} <br/>
<br/>
$js = sprintf ( '%s("#%s").jqGrid(%s);' , <br/>
ZendX_JQuery_View_Helper_JQuery :: getJQueryHandler ( ) , <br/>
$id , <br/>
$encodedParams <br/>
) ; <br/>
$this -> jquery -> addOnLoad ( $js ) ; <br/>
$this -> view -> headscript ( ) -> appendFile ( '/js/jquery/jquery.jqGrid.js' ) ; <br/>
$this -> view -> headLink ( ) -> appendStylesheet ( '/css/jsgrid/steel/grid.css' ) ; <br/>
<br/>
$html = "<table id=" '. $id . ' "></table>" ; <br/>
$html .= '<div id="' . $params [ 'pager' ] . '" class="scroll" style="text-align:center;"></div>' ; <br/>
return $html ; <br/>
}


What happens in this method
1) Parameters for installing jqGrid are converted to the full view required to display the grid.
2) Script and style file added
3) Tags are written to display the grid itself and page by page.
4) The onLoad event is added to initialize the grid on the client.
')
Now register helper in bootstrap
$ view-> addHelperPath ('Xms / ZendX / JQuery / View / Helper /', 'Xms_ZendX_JQuery_View_Helper');

And in our view script, call the helper to get the grid
<? = $ this-> jqGrid ("jqGrid", $ this-> jqGridParams); ?>

Where jqGridParams is created in the controller as follows
$columns = Xms_DoctrineX_Table :: getAccessibleColumns ( 'Modules' ) ; <br/>
foreach ( $columns as $key => $val ) { <br/>
$colNames [ ] = $this -> _translate -> _ ( $key ) ; <br/>
$colModel [ ] = array ( "name" => $key , "sortable" => "true" , "align" => "right" ) ; <br/>
} <br/>
$this -> view -> jqGridParams = array ( 'url' => '/cms/modules/index' , <br/>
'colNames' => $colNames , <br/>
'colModel' => $colModel , <br/>
) ;
$columns = Xms_DoctrineX_Table :: getAccessibleColumns ( 'Modules' ) ; <br/>
foreach ( $columns as $key => $val ) { <br/>
$colNames [ ] = $this -> _translate -> _ ( $key ) ; <br/>
$colModel [ ] = array ( "name" => $key , "sortable" => "true" , "align" => "right" ) ; <br/>
} <br/>
$this -> view -> jqGridParams = array ( 'url' => '/cms/modules/index' , <br/>
'colNames' => $colNames , <br/>
'colModel' => $colModel , <br/>
) ;


The set of columns is created dynamically and passed as a parameter in the view script. This is done so that the grid can display the fields to which the user has access, as the sets of available fields are constantly changing.

That is all. The result is a dynamic grid, with a set of fields depending on rights.

Source: https://habr.com/ru/post/64337/


All Articles