yii2中的自定义gridview

时间:2014-02-10 10:52:28

标签: php gridview yii2 cgridview

如何在Yii2中删除特定网格视图的摘要和排序器。在Yii1.1中,我们可以通过设置模板属性来实现。在yii2中如何实现这个目标?

5 个答案:

答案 0 :(得分:36)

要仅更改summary选项,您可以使用:

'summary' => "{begin} - {end} {count} {totalCount} {page} {pageCount}",

然后,如果要清空summary设置空字符串值,如:

'summary'=> "",

并更改您可以使用的布局:

'layout'=> "{summary}\n{items}\n{pager}"

然后,如果你想用空字符串值清空layouts设置布局,如:

'layout'=> "",

Ref link

Ref link

因此,对于示例,我认为以下示例代码可以帮助您了解如何在Yii 2中更改和GridView表:

                <?= \yii\grid\GridView::widget([
                'id' => 'table',
                'dataProvider' => $dataProvider,
                'layout'=>"{sorter}\n{pager}\n{summary}\n{items}",
                'summary' => "Showing {begin} - {end} of {totalCount} items",
                'tableOptions' => ['class' => 'table  table-bordered table-hover'],
                'rowOptions' => function ($model, $key, $index, $grid) {
                    return [
                        'style' => "cursor: pointer",
                        'onclick' => 'location.href="'
                            . Yii::$app->urlManager->createUrl('test/index')
                            . '?id="+(this.id);',
                    ];
                },
                'columns' => [
                    [
                        'class' => 'yii\grid\SerialColumn',
                        'contentOptions' => ['style' => 'width: 20px;', 'class' => 'text-center'],
                    ],
                    [
                        'class' => 'yii\grid\DataColumn',
                        'attribute' => 'date',
                        'headerOptions' => ['class' => 'text-center'],
                        'label' => 'Date',
                        'contentOptions' => ['style' => 'width: 130px;', 'class' => 'text-center'],
                    ],
                        'template' => '{view}',
                        'buttons' => [
                            'view' => function ($url, $model) {
                                return \yii\helpers\Html::a('<div class="text-center"><em data-toggle="tooltip"
                                                            data-placement="top" title="more detail"
                                                            class="fa fa-external-link-square text-warning"></em></div>',
                                    (new yii\grid\ActionColumn())->createUrl('test/index', $model, $model['id'], 1), [
                                        'title' => Yii::t('yii', 'view'),
                                        'data-method' => 'post',
                                        'data-pjax' => '0',
                                    ]);
                            },
                        ]
                    ],
                ],
            ]); ?>

答案 1 :(得分:9)

知道了。通过设置布局属性,我们可以实现它。

'layout'=>"{summary}\n{items}\n{pager}"

答案 2 :(得分:6)

如果您只想使用网格项,请使用'layout'=>"{items}"

如果您只想使用摘要'layout'=>"{summary}"

如果您只想使用分拣机'layout'=>"{pager}"

答案 3 :(得分:1)

将参数summaryText设置为空字符串:

array(
    'summaryText' => '',
    'dataProvider' => $model->search(),
...

答案 4 :(得分:0)

[
    'class' => 'yii\grid\ActionColumn',
    'buttons' =>
        [
            'update'=>function($url,$model,$key)
            {
                return Html::a( "update" , $url ); //use Url::to() in order to change $url
            },
            'view'=>function($url,$model,$key)
            {
                return Html::a( "update" , $url ); //use Url::to() in order to change $url
            },
            'delete'=>function($url,$model,$key)
            {
                return Html::a( "update" , $url, [
                    'class' => 'btn btn-lg btn-primary',
                    'data' => [
                        'method' => 'post',
                        'params' => ['derp' => 'herp'], // <- extra level
                    ],
                ] ); //use Url::to() in order to change $url
            }
        ],
    'template' => '<div class="column-buttons">
                                        <span>{update}</span>
                                        <span>{view}</span>
                                        <span>{delete}</span>
                                    </div>',
    'header' => 'Actions'
]