2013-01-08 48 views
4

我需要在CGridView中添加一列。如何在YII中的CGridView中添加自定義列?

我用這個:

$this->widget('zii.widgets.grid.CGridView', array(
      'id'=>'user-grid', 
      'dataProvider'=>$model->search(), 
      'filter'=>$model, 
      'pager' => array(
       'firstPageLabel' => '<<', 
       ), 
      'columns'=>array(
       'username', 
       'name', 
       'email', 
       'creationDate', 
       array(
         'class' => 'CButtonColumn', 
         'template' => '{change} {view}', 
         'buttons' => array(
             'change' => array(
                'url'=> "'http://test.com/userservice/".$model->username."'", 
             ), 
         ), 

       ), 
       array(
        'name' => 'test', 
        'value' => 'testtest', 
       ) 
      ), 
)); 

但我得到的錯誤:

Property "User.test" is not defined.

+0

Look [using-standard-filters-in-cgridview-custom-fields](http://www.yiiframework.com/wiki/117/using-standard-filters-in-cgridview-custom-fields /),瞭解更多信息。 – shgnInc

回答

10

就快,你的列數組中你可以使用name參數有關的模型的屬性數據提供者,而不是自定義列,您可以使用header像這樣:

'columns'=>array(
    ... 
    array(
     'header' => 'test', 
     'value' => '"testtest"', 
    ), 
    ... 
) 
4

您可以像這樣在CGridView上編寫普通代碼。

'columns'=>array(
       'username', 
       'name', 
       'email', 
       'creationDate', 
       'test', 
        --- 
      ), 

如果你把代碼放在你的模型上。

--- 
    public $test ; 
    public function afterFind() { 
     $this->test = 'Test Variable' ; // put your custom code to reflect exact need 
     return parent::afterFind(); 
    } 
    --- 
相關問題