Yii CGridView用法实例详解

(编辑:jimmy 日期: 2026/1/3 浏览:2)

本文实例讲述了Yii CGridView用法。分享给大家供大家参考,具体如下:

CGridView的功能是用来显示的数据列表。它支持排序,分页,和AJAX数据请求。

CGridView最好使用 data provider,最好是 CActiveDataProvider 。

简单代码如下:

$dataProvider=new CActiveDataProvider('Post');
$this->widget('zii.widgets.grid.CGridView', array(
  'dataProvider'=>$dataProvider,
));

详细示例:

$this->widget('zii.widgets.grid.CGridView', array(
  'dataProvider'=>$dataProvider,
  'columns'=>array(
    'title',     // display the 'title' attribute
    'category.name', // display the 'name' attribute of the 'category' relation
    'content:html',  // display the 'content' attribute as purified HTML
    array(      // display 'create_time' using an expression
      'name'=>'create_time',
      'value'=>'date("M j, Y", $data->create_time)',
    ),
    array(      // display 'author.username' using an expression
      'name'=>'authorName',
      'value'=>'$data->author->username',
    ),
    array(      // display a column with "view", "update" and "delete" buttons
      'class'=>'CButtonColumn',
    ),
  ),
));

属性说明:

actionPrefix 方法前辍

afterAjaxUpdate AJAX成功响应后调用的JAVASCRIPT函数

ajaxUpdate 是否启用AJAX请求数据

ajaxUpdateError AJAX失败响应后调用的JAVASCRIPT函数

ajaxUrl AJAX请求的URL

ajaxVar AJAX请求,使用GET方式传送的变量

baseScriptUrl 视图资源的URL

beforeAjaxUpdate 在AJAX调用之前调用的JAVASCRIPT函数

blankDisplay 列标题为空显示的内容

columns 内容列有配置

controller 控制器

cssFile 视图的样式

dataProvider 视图的数据

emptyText 内容为空显示的信息

filterPosition 搜索框所在的位置

更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

一句话新闻

微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。