search
Homephp教程php手册Yii Framework 开发教程Zii组件-GridView示例

Yii Framework 开发教程Zii组件-GridView示例

Jun 13, 2016 am 10:54 AM
frameworkyiibydevelopTutorialofExamplecomponentssheet

CGridView 以表格的形式显示数据,CGridView 也支持分页和排序,CGridView最基本的用法和ListView类型,也是通过设置 data provider,通常是CActiveDataProvider。

 

修改上个例子Yii Framework 开发教程(31) Zii组件-DetailView 示例,把ListView该为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}',

 

)); ?>

 

 

 

显示结果如下:

可以看到GridView缺省显示了所有的字段,并使用缺省的格式显示字段,如果需要控制字段显示和格式,可以通过配置 CGridView::columns属性来实现。GridView的每列为一个CGridColumn对象:

 

CGridColumn为所有Grid 列表项的基类,表格的每例可以有一个表头,多个数据单元,和一个可选的表尾单元格。

CButtonColumn 表示该单元格为一个或多个按钮,缺省显示三个按钮, “view”, “update” 和”delete”,可以通过设置 buttons 和 template来更改。

CCheckBoxColumn 表示该单元格为Checkbox,支持只读,单选或多选,通过配置selectableRows来修改

CDataColumn 表示该单元为数据或是表达式,通过配置name或value,前者表示数据模型的属性名称,后者代码一个PHP表达式。

CLinkColumn 表示一个超链接,通过配置 label , url或 imageUrl来设置链接。

重新定义GridView的columns属性如下:

 

[php]  

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?',  

                    ),  

                ),  

  

));  

?>  

 

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?',

),

),

 

));

?>

 

显示如下:

点击姓名可以显示DetailView。 ButtonColumn 的update,search,delete没有添加对应的view ,就留给你自己加上了。

 

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version