YII2 - 如何合并行yii2 gridview?

时间:2017-02-17 06:35:22

标签: php gridview yii2

我有这样的gridview表: Gridview Storelist

我想将商店1(示例)与4行详细信息合并。有可能做到吗? 我在gridview中有一个代码:

<?php Pjax::begin(['id' => 'pjax_filesetting','timeout'=>false]) ?>
<?php 

    $this->registerJs(
    "
        $('.btneditfile').click(function(){
            var hqid = $(this).attr('data-hqid');
            var storeid = $(this).attr('data-storeid');
            var filename = $(this).attr('data-filename');

        location.href = '/pos/editfilesetting?hqid='+hqid+'&storeid='+storeid+'&filename='+filename;
            return false;
            });
    ");
?>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'options' => ['class' => 'grid-view active-table'],
        'columns' => 
            [
                ['class' => 'yii\grid\SerialColumn'],
                [
                    'label' => 'Store Name',
                    'attribute' => 'store_name',
                    'encodeLabel' => false,
                ],
                [
                    'label' => 'Filename',
                    'attribute' => 'filename',
                    'encodeLabel' => false,
                ],
                [
                    'label' => 'Datecheck',
                    'attribute' => 'datecheck',
                    'encodeLabel' => false,
                    'value' => function($model){

                                $datecheck = $model["datecheck"];

                                if($datecheck)
                                {
                                    return $model["datecheck"] = "Check";
                                }
                                else
                                {
                                    return $model["datecheck"] = "Not Check";

                                }
                        }
                ],
                [
                    'label' => 'Timecheck',
                    'attribute' => 'timecheck',
                    'encodeLabel' => false,
                    'value' => function($model){

                                $timecheck = $model["timecheck"];

                                if($timecheck)
                                {
                                    return $model["timecheck"] = "Check";
                                }
                                else
                                {
                                    return $model["timecheck"] = "Not Check";

                                }
                           }
                ],
                [
                    'label' => 'Maintenance code',
                    'attribute' => 'maincode',
                    'encodeLabel' => false,
                ],
                [
                    'label' => 'Final Filename',
                    'attribute' => 'usedfilename',
                    'encodeLabel' => false,
                ],
                [
                    'class' => 'yii\grid\ActionColumn',
                    'template' => '<div align="center">{update} {delete}</div>',
                            'buttons' => [                    
                                'update' => function ($url, $model) use ($controller) {
                                    return Html::button(
                                        '<span class="glyphicon glyphicon-pencil"></span>',
                                        [
                                        'class' => 'btn btn-primary btn-xs btneditfile',
                                        'title' => Yii::t( $controller->transmodule, 'Edit' ),
                                        'style' => ['padding-top' => '5px', 'padding-bottom' => '5px'],
                                        'id' => 'editfile',
                                        'data-storeid' => $model['id'],
                                        'data-hqid' => $model['cmshqid'],
                                        'data-filename' => $model['filename'] 
                                    ]
                                );
                            },

                            'delete' => function ($url, $model)use ($controller){   
                                return Html::button(
                                '<span class="glyphicon glyphicon-trash"></span>',
                                [
                                    'class' => 'btn btn-danger btn-xs btndeletefile',
                                    'title' => Yii::t( $controller->transmodule, 'Delete' ),
                                    'style' => ['padding-top' => '5px', 'padding-bottom' => '5px'],
                                    'data-storeid' => $model['id'],
                                    'data-hqid' => $model['cmshqid'],
                                    'data-filename' => $model['filename'] 
                                ]
                            );
                        },        
                    ],
                ]
            ],
    ]) ?>
    <?php Pjax::end() ?>

我必须做些什么来合并它? 我需要的是如果商店名称相同,请合并它列。

1 个答案:

答案 0 :(得分:1)

商店和其他细节是在一张桌子还是单独的桌子上?如果它在单独的表中,$ dataProvider应该从商店表中查询。在商店模型中,创建与包含详细信息的其他表的hasMany关系。因此在视图中,在gridview列函数中,您可以遍历这些值并将其显示在同一行中。