首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将2个或更多数据放入框网格视图

如何将2个或更多数据放入框网格视图
EN

Stack Overflow用户
提问于 2015-08-26 17:16:57
回答 2查看 65关注 0票数 0

我正在为我的网站创建反馈页面,我使用网格视图来显示反馈列表。在网格视图的一行中,我想在一个框中填充照片、日期和用户名。我确实把照片贴在盒子上了。但是我想知道如何把另一个数据

视图:

代码语言:javascript
复制
[   'attribute' => 'iduser.photo',
            'format' => 'html',
            'value'=>  function($data) { return Html::img($data->imageurl,['id'=>'photo']); },
            'contentOptions'=>['style'=>'max-width: 10px; max-height: 10px'],
        ],

来自模型/反馈实体的反馈属性:

代码语言:javascript
复制
 * @property integer $ID_KOMENTAR
 * @property integer $id
 * @property string $KOMENTAR //comment
 * @property string $TANGGAL  //date
 * @property User $iduser     //related to the user

反馈与用户的关系。反馈有一个用户名,用户名有多个反馈

代码语言:javascript
复制
public function getIduser()
{
    return $this->hasOne(User::className(), ['id' => 'id']);
}

用户实体: iduser、username、photo

EN

回答 2

Stack Overflow用户

发布于 2015-08-27 22:17:04

我在一年级也做了同样的事情,希望能对你有所帮助。您可以自定义任何列,并可以在其中放置任何html。为此,我将为您提供一个小示例:)

在视图文件中,您可以编写cgridview代码。我正在调用一个用于获取列的值的函数,在该函数中,您可以相应地创建代码。在我的示例中,列名是Office Managers,函数名是getManagerListFromOfficeBranch

代码语言:javascript
复制
<?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中包含该文件

代码语言:javascript
复制
Exm: require_once realpath(__DIR__ . ‘/../includes/customFunctions.php’);

函数

代码语言:javascript
复制
<?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 
    }
?>
票数 2
EN

Stack Overflow用户

发布于 2015-08-26 20:18:40

我还没有测试过,但是这个应该对你有效。

代码语言:javascript
复制
public function actionIndex()
{
    $dataProvider = new ActiveDataProvider([ 'query' => Feedback::find()->with('iduser')->orderBy(['ID' => SORT_ASC, 'TANGGAL' => SORT_ASC])]);
    return $this->render('index', [ 'dataProvider' => $dataProvider ]);
}

在您的视图中

代码语言:javascript
复制
<?= 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'],
    ],
]); ?>

希望这就是你所需要的

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32222580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档