自定义 Yii CGridView Column 的显示

Yii 的 CGridView 很好的封装了表格的创建和显示,通过数据配置,可以方便的创建表格。下面代码是 Yii 文档中的例子。

$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>

Custom Yii CGridView Column

上图所示的文章管理列表通过以下代码配置。


$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 》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值