Yii 的 CGridView 很好的封装了表格的创建和显示,通过数据配置,可以方便的创建表格。下面代码是 Yii 文档中的例子。
dataprovider 是要显示的数据集,可以是 array 数据 ( CArrayDataProvider ) ,也可以是数据库查找的结果集 ( CActiveDataProvider ) 。每一列的显示通过 columns 数组配置。Yii 提供了几种 column 的类型, CDataColumn, CLinkColumn, CButtonColumn, CCheckBoxColumn , 默认是使用 CDataColumn 。但是,光靠上面几种 column 类型,并不容易实现复杂的列需求,比如文章管理列表,希望在操作列里面同时显示 “编辑”和“删除”按钮。所以我另外做了一个 column 类来实现这类功能,同时也大大提高了 CGridView 的灵活性,可以在列里面显示任何你想要的东西。代码如下。
aaViewDataColumn 类继承 CDataColumn ,重载了方法 renderDataCellContent 。在函数中,将 value 的配置当做 php 文件名,调用 Controller 的 renderPartial 进行渲染。这样 value 所配置的页面里面,就可以添加任何想要的东西。
<strong>使用示例</strong>
上图所示的文章管理列表通过以下代码配置。
要自定义 CGridView 的 Column 显示,也有其他的方法,如 Joblo 的文章 《 CGridView: Render customized/complex datacolumns 》
$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',
),
), ));
dataprovider 是要显示的数据集,可以是 array 数据 ( CArrayDataProvider ) ,也可以是数据库查找的结果集 ( CActiveDataProvider ) 。每一列的显示通过 columns 数组配置。Yii 提供了几种 column 的类型, CDataColumn, CLinkColumn, CButtonColumn, CCheckBoxColumn , 默认是使用 CDataColumn 。但是,光靠上面几种 column 类型,并不容易实现复杂的列需求,比如文章管理列表,希望在操作列里面同时显示 “编辑”和“删除”按钮。所以我另外做了一个 column 类来实现这类功能,同时也大大提高了 CGridView 的灵活性,可以在列里面显示任何你想要的东西。代码如下。
class aaViewDataColumn extends CDataColumn{
/**
* Renders the data cell content.
* This method evaluates {@link value} or {@link name} and renders the result.
* @param integer $row the row number (zero-based)
* @param mixed $data the data associated with the row
*/
protected function renderDataCellContent($row,$data)
{
if($this->value!==null){
$this->grid->controller->renderPartial($this->value, array('data'=>$data,'row'=>$row));
}else if($this->name!==null){
$value=CHtml::value($data,$this->name);
echo $value===null ? $this->grid->nullDisplay : $this->grid->getFormatter()->format($value,$this->type);
}
}
}
aaViewDataColumn 类继承 CDataColumn ,重载了方法 renderDataCellContent 。在函数中,将 value 的配置当做 php 文件名,调用 Controller 的 renderPartial 进行渲染。这样 value 所配置的页面里面,就可以添加任何想要的东西。
<strong>使用示例</strong>
上图所示的文章管理列表通过以下代码配置。
$this->widget('zii.widgets.grid.CGridView',
array('dataProvider'=>$dataProvider,
'columns'=>array(
array('header'=>'title',
'value'=>'$data->title',
'type'=>'raw',
),
array('header'=>'author',
'value'=>'$data->author->username',
'type'=>'raw',
),
array('header'=>'create time',
'value'=>'$data->post_time',
'type'=>'raw',
),
array('header'=>'update time',
'value'=>'$data->change_time',
'type'=>'raw',
),
array('header'=>'operations',
'value'=>'_article_opts',
'type'=>'raw',
'class'=>'aaViewDataColumn',
),
),
));
最后的 "operations" 列,我使用了 aaViewDataColumn ,"value" 配置了一个 php 文件的名字 "_article_opts" 。 该文件内如如下。
<?php
echo '<a href="javascript:void(0)", οnclick="editarticle('.$data->id.')">edit</a> | ';
echo '<a href="javascript:void(0)", οnclick="deletearticle('.$data->id.')">delete</a>';
?>
要自定义 CGridView 的 Column 显示,也有其他的方法,如 Joblo 的文章 《 CGridView: Render customized/complex datacolumns 》