我正在为我的网站创建反馈页面,我使用网格视图来显示反馈列表。在网格视图的一行中,我想在一个框中填充照片、日期和用户名。我确实把照片贴在盒子上了。但是我想知道如何把另一个数据
视图:
[ 'attribute' => 'iduser.photo',
'format' => 'html',
'value'=> function($data) { return Html::img($data->imageurl,['id'=>'photo']); },
'contentOptions'=>['style'=>'max-width: 10px; max-height: 10px'],
],来自模型/反馈实体的反馈属性:
* @property integer $ID_KOMENTAR
* @property integer $id
* @property string $KOMENTAR //comment
* @property string $TANGGAL //date
* @property User $iduser //related to the user反馈与用户的关系。反馈有一个用户名,用户名有多个反馈
public function getIduser()
{
return $this->hasOne(User::className(), ['id' => 'id']);
}用户实体: iduser、username、photo
发布于 2015-08-27 22:17:04
我在一年级也做了同样的事情,希望能对你有所帮助。您可以自定义任何列,并可以在其中放置任何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的文件夹,并将此文件保存在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
}
?>发布于 2015-08-26 20:18:40
我还没有测试过,但是这个应该对你有效。
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([ 'query' => Feedback::find()->with('iduser')->orderBy(['ID' => SORT_ASC, 'TANGGAL' => SORT_ASC])]);
return $this->render('index', [ 'dataProvider' => $dataProvider ]);
}在您的视图中
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
// user attributes examples
[
'attribute' => 'iduser.photo',
'value' => function($model)
{
return Html::img($model->iduser->imageurl,['id'=>'photo']);
},
'contentOptions' => [
'style' => 'max-width: 10px; max-height: 10px'
],
'format' => 'raw',
],
// or in this mode
[
'value' => 'iduser.username'
],
...................
// your feedback attributes
'KOMENTAR',
'TANGGAL',
// actions colum
['class' => 'yii\grid\ActionColumn'],
],
]); ?>希望这就是你所需要的
https://stackoverflow.com/questions/32222580
复制相似问题