In the Yii Framework, fully customize the gridview View by inheriting CGridView

Source: Internet
Author: User
In the Yii Framework, fully customize the gridview View by inheriting CGridView

Today, in the project, we want to implement the table flip function. Naturally, we think of the gridview view on the admin page of the Yii Framework background, which displays all the content in the form of a table, and then has the paging function, therefore, we are ready to use its implementation ideas for implementation.

Because many front-end style and interface modifications and addition and deletion of functions are involved in the specific implementation process, the Yii built-in CGridView is not enough to fully meet my needs, therefore, the first thing to Enable is to inherit the CGridView to implement a own pendant class TGridView, mainly to delete some built-in classes and IDs, rendering table units in the CGridView also call the CDataColumn and CGridColumn, therefore, TDataColumn and TGridColumn are customized (inherited from CDataColumn and CGridColumn respectively, where CDataColumn is inherited from CGridColumn. here, we use TDataColumn to inherit from TGridColumn and TGridColumn to inherit from CGridColumn ), in this way, the interface and function customization of the entire gridview table generation are completed. the specific code is as follows:

TGridView. php:

Class TGridView extends CGridView {protected function initColumns () {if ($ this-> columns === array () {if ($ this-> dataProvider instanceof CActiveDataProvider) $ this-> columns = $ this-> dataProvider-> model-> attributeNames (); elseif ($ this-> dataProvider instanceof IDataProvider) {// use the keys of the first row of data as the default columns $ data = $ this-> dataProvider-> getData (); if (isset ($ data [0]) & is_array ($ data [0 ]) $ This-> columns = array_keys ($ data [0]) ;}$ id = $ this-> getId (); foreach ($ this-> columns as $ I => $ column) {if (is_string ($ column) $ column = $ this-> createDataColumn ($ column ); else {if (! Isset ($ column ['class']) $ column ['class'] = 'cdatacolumn'; $ column = Yii: createComponent ($ column, $ this );} if (! $ Column-> visible) {unset ($ this-> columns [$ I]); continue;} $ this-> columns [$ I] = $ column ;} foreach ($ this-> columns as $ column) $ column-> init ();} public function renderItems () {if ($ this-> dataProvider-> getItemCount ()> 0 | $ this-> showTableOnEmpty) {echo"
 
 
  
  
\ N "; $ this-> renderTableHeader (); ob_start (); $ this-> renderTableBody (); $ body = ob_get_clean (); $ this-> renderTableFooter (); echo $ body; // TFOOT must appear before TBODY according to the standard. echo"
 
 
";}Else $ this-> renderEmptyText ();} public function renderTableHeader () {if (! $ This-> hideHeader) {if ($ this-> filterPosition = self: FILTER_POS_HEADER) $ this-> renderFilter (); echo"\ N "; foreach ($ this-> columns as $ column) $ column-> renderHeaderCell (); echo"\ N "; if ($ this-> filterPosition = self: FILTER_POS_BODY) $ this-> renderFilter ();} elseif ($ this-> filter! = Null & ($ this-> filterPosition = self: FILTER_POS_HEADER | $ this-> filterPosition = self: FILTER_POS_BODY )) {$ this-> renderFilter () ;}} public function renderTableBody () {$ data = $ this-> dataProvider-> getData (); $ n = count ($ data ); if ($ n> 0) {for ($ row = 0; $ row <$ n; ++ $ row) $ this-> renderTableRow ($ row );} else {echo''; $ This-> renderEmptyText (); echo"\ N ";}} protected function createDataColumn ($ text) {if (! Preg_match ('/^ ([\ w \.] +) (:( \ w *))? (:(.*))? $/', $ Text, $ matches) throw new CException (Yii: t ('zii', 'The column must be specified in The format of "Name: Type: label ", where" Type "and" Label "are optional. '); $ column = new TDataColumn ($ this); // custom TDataColumn renders the table unit content and style $ column-> name = $ matches [1]; // Obtain the column name, such as id, name, and type. if (isset ($ matches [3]) & $ matches [3]! = '') $ Column-> type = $ matches [3]; if (isset ($ matches [5]) $ column-> header = $ matches [5]; return $ column ;}}

TDataColumn. php:

Class TDataColumn extends TGridColumn {protected function renderHeaderCellContent () {if ($ this-> grid-> enableSorting & $ this-> sortable & $ this-> name! = Null) echo $ this-> grid-> dataProvider-> model-> getAttributeLabel ($ this-> name ); // remove the default generated Header link elseif ($ this-> name! = Null & $ this-> header = null) {if ($ this-> grid-> dataProvider instanceof CActiveDataProvider) echo CHtml :: encode ($ this-> grid-> dataProvider-> model-> getAttributeLabel ($ this-> name); elseecho CHtml: encode ($ this-> name );} elseparent: renderHeaderCellContent ();} protected function renderDataCellContent ($ row, $ data) {if ($ this-> value! = Null) $ value = $ this-> evaluateExpression ($ this-> value, array ('data' => $ data, 'row' => $ row )); elseif ($ this-> name! = Null) $ value = CHtml: value ($ data, $ this-> name); echo $ value = null? $ This-> grid-> nullDisplay: $ this-> grid-> getFormatter ()-> format ($ value, $ this-> type );}}

TGridColumn. php

Class TGridColumn extends CGridColumn {public $ name; // The attribute name public $ value of the model corresponding to the column name; // the attribute value public $ type = 'text' of the model corresponding to the column value '; public function renderHeaderCell () {if ($ this-> name = 'XXX'): echo''; Elseif ($ this-> name = 'xxxx'): echo''; Elseif ($ this-> name = 'xxxxx'): echo''; Else: echo''; Endif; $ this-> renderHeaderCellContent (); echo"";} Public function renderDataCell ($ row) {$ data = $ this-> grid-> dataProvider-> data [$ row]; $ options = $ this-> htmlOptions; if ($ this-> cssClassExpression! = Null) {$ class = $ this-> evaluateExpression ($ this-> cssClassExpression, array ('row' => $ row, 'data' => $ data )); if (! Empty ($ class) {if (isset ($ options ['class']) $ options ['class']. = ''. $ class; else $ options ['class'] = $ class ;}} echo CHtml: openTag ('TD ', $ options); $ this-> renderDataCellContent ($ row, $ data); echo'';}}

These custom classes are stored in the/protected/components Directory for the view to call:

 Widget ('tgridview ', array ('summarytext' => '', 'dataprovider' => $ dataProvider, 'enablesorting' => false, 'pager' => array ('class' => 'tplinkpager ', 'maxbuttoncount' => 10, 'htmloption' => array ('class' => 'paging '), 'header' => '', 'prevpagelabel '=> 'previous page', 'nextpagelabel' => 'next page',), 'columns '=> array ('id ', 'partner', 'pname', 'type. name',),);?>

Here, the Yii built-in paging class is CLinkPager, and because it does not meet my needs, I have customized a inherited from TLinkPager (inherited from CLinkPager) (The definition of this class is described in detail in the Yii Framework using CLinkPager to implement the paging function. TPLinkPager mainly removes the function of redirecting to a specified page ).

Yii background admin. in the php page, dataProvider comes from the model-> search () method. here, because I have removed the sort function, it is not necessary to use the search method, I use the variable $ dataProvider passed by the controller to provide data support:

$ DataProvider = new CActiveDataProvider ('Model name', array ('criteria '=> array ('order' => 'Id ASC ',), 'pagination' => array ('pagesize' => 10, // 10 records are displayed on each page ),));

The entire custom CGridView process is completed as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.