2015-08-26 38 views
0

我正在为我的网站创建反馈页面,我使用gridview显示反馈列表。在一排gridview中,我想在一个框中填入照片,日期和用户名。我确实把照片放在盒子上。但我不知道如何把另一个数据我怎么能把2个或更多的数据框gridview

的观点:

[ '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']); 
} 

用户实体:ID用户,用户名,照片

+1

你是什么意思?值的函数可以返回任何你想要的html代码。 Html :: img()返回一个html字符串。你可以在那里附加任何其他的HTML。顺便说一句。不应该格式是'生'? Afaik'html'将会被转义... – robsch

回答

1

我没有测试过,但这个shuld你

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'], 
    ], 
]); ?> 

希望这是你需要的

2

我在Yii做了同样的事情。希望它能帮助你。你可以自定义任何列,并可以把任何HTML。我会给你一个这样的小例子:)

在视图文件中你写了cgridview代码。我正在调用函数来获取列的值,并且在该函数中可以相应地创建您的代码。在我的例子列名办公室经理和函数的名称是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) 
        ), 
        ), 

    ), 

)); ?> 

现在写你在一个文件中的函数。 你可以用名字包括受保护的文件夹中创建一个文件夹,并保持此文件夹包含 路径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 
    } 
?> 
相关问题