我在 Yii 1 中做了同样的事情。希望它可以帮助你。您可以自定义任何列,并且可以将任何 html 放在那里。我会给你一个小例子:)
在视图文件中编写 cgridview 代码。我正在调用一个函数来获取列的值,在该函数中您可以相应地创建代码。在我的示例中,列名是Office Managers,函数名是getManagerListFromOfficeBranch
<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'id' => 'user-grid',
'columns'=>array(
array(
'class' => 'CButtonColumn',
'name',
'email',
array(
'name'=>'Office Managers',
'type'=>'raw', //for allowing raw html
'value'=>'customFunctions::getManagerListFromOfficeBranch($data->officeid)' //here I have created custom function that will get managers of office branch from office table ($data is used to get any value from current row of branch{you can send your feedback id here if you want any info from feedback toggle}only use if you want to )
),
),
),
)); ?>
现在将你的函数写在一个文件中。您可以在名称为 includes 的受保护文件夹中创建一个文件夹,并将此文件保存在包含文件夹
路径 Exm 中:/protected/includes/customFunctions.php
在 config/main.php 中包含该文件
Exm: require_once realpath(__DIR__ . ‘/../includes/customFunctions.php’);
功能
<?php
class customFunctions{
public static function getManagerListFromOfficeBranch($officeid) {
$managerDetails=Office::model()->findAllByAttributes(array('officeid'=> $officeid)); //Office is the model object of Office Table
$managerList='';
foreach ($managerDetails as $key => $value) {
$managerList=$managerList.$value->manager->first_name." ".$value->manager->last_name."<br/>";
}
echo $managerList; //all managers echo line by line in the column
echo CHtml::link('Users',array('Users/action')); //write custom HTML Here
}
?>