Home  >  Article  >  Backend Development  >  Yii Framework Development Tutorial Zii Component-GridView Example_PHP Tutorial

Yii Framework Development Tutorial Zii Component-GridView Example_PHP Tutorial

WBOY
WBOYOriginal
2016-07-14 10:07:43844browse

CGridView displays data in the form of tables. CGridView also supports paging and sorting. The most basic usage of CGridView and ListView type is also by setting the data provider, usually CActiveDataProvider.

Modify the previous example Yii Framework Development Tutorial (31) Zii Component-DetailView example and change the ListView to GridView:
[php]
widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'ajaxUpdate'=>false,
'template'=>'{pager}{summary}{items}{pager}',
)); ?>
widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'ajaxUpdate'=>false,
'template'=>'{pager}{summary}{items}{pager}',
)); ?>
The results are displayed as follows:
Yii Framework Development Tutorial Zii Component-GridView Example_PHP Tutorial
You can see that GridView displays all fields by default and uses the default format to display fields. If you need to control field display and format, you can configure the CGridView::columns property. Each column of GridView is a CGridColumn object:
Yii Framework Development Tutorial Zii Component-GridView Example_PHP Tutorial
CGridColumn is the base class of all Grid list items. Each table instance can have a header, multiple data cells, and an optional footer cell.
CButtonColumn indicates that the cell is one or more buttons. By default, three buttons are displayed, "view", "update" and "delete", which can be changed by setting buttons and template.
CCcheckBoxColumn indicates that the cell is a Checkbox, supports read-only, single-select or multi-select, and can be modified by configuring selectableRows
CDataColumn indicates that the unit is data or expression. By configuring name or value, the former represents the attribute name of the data model, and the latter codes a PHP expression.
CLinkColumn represents a hyperlink, and the link is set by configuring label, url or imageUrl.
Redefine the columns attribute of GridView as follows:
[php]
widget('zii.widgets.grid.CGridView', array(
'id'=>'person-grid',
'dataProvider'=>$dataProvider,
'htmlOptions'=>array('style'=>'width:740px'),
'pager'=>array(
                                                                                                                                                                                     
        ),  
'columns'=>array(
array(
                  'header'=>'Name',  
                'type'=>'raw',  
                                                                                                                                                                                                                                                              having
                                                                             
$this->grid->controller->createUrl
                ("view",array("CustomerId"=>$data->CustomerId)))',
  
                    ),  
  
                'Company',  
  
                array(  
                    'class'=>'CLinkColumn',  
                    'header'=>'Email',  
                    'imageUrl'=>'images/email.png',  
                    'labelExpression'=>'$data->Email',  
                    'urlExpression'=>'"mailto://".$data->Email',  
                    'htmlOptions'=>array('style'=>'text-align:center'),  
                    ),  
                array(  
                    'class'=>'CButtonColumn',  
                    'deleteConfirmation'=>'Are you sure to delete this item?',  
                    ),  
                ),  
  
));  
?>  
 
widget('zii.widgets.grid.CGridView', array(
'id'=>'person-grid',
'dataProvider'=>$dataProvider,
'htmlOptions'=>array('style'=>'width:740px'),
'pager'=>array(
'maxButtonCount'=>'7',
),
'columns'=>array(
 
array(
'header'=>'Name',
'type'=>'raw',
'value'=>'CHtml::link($data->FirstName .
" " . $data->LastName,
$this->grid->controller->createUrl
("view",array("CustomerId"=>$data->CustomerId)))',
 
),
 
'Company',
 
array(
'class'=>'CLinkColumn',
'header'=>'Email',
'imageUrl'=>'images/email.png',
'labelExpression'=>'$data->Email',
'urlExpression'=>'"mailto://".$data->Email',
'htmlOptions'=>array('style'=>'text-align:center'),
),
array(
'class'=>'CButtonColumn',
'deleteConfirmation'=>'Are you sure to delete this item?',
),
),
 
));
?>
 
显示如下:
Yii Framework Development Tutorial Zii Component-GridView Example_PHP Tutorial
点击姓名可以显示DetailView。 ButtonColumn 的update,search,delete没有添加对应的view ,就留给你自己加上了。
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477835.htmlTechArticleCGridView 以表格的形式显示数据,CGridView 也支持分页和排序,CGridView最基本的用法和ListView类型,也是通过设置 data provider,通常是CActiveDat...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn