php 在 yii2 中自定义网格视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21675170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Customise grid view in yii2
提问by Dency G B
How to remove summary and sorter for a particular grid view in Yii2. In Yii1.1 we can do that by setting the template property. In yii2 how to achieve this?
如何在 Yii2 中删除特定网格视图的摘要和排序器。在 Yii1.1 中,我们可以通过设置模板属性来做到这一点。在yii2中如何实现?
采纳答案by Dency G B
Got it.By setting the layout property,we can achieve it.
明白了,通过设置layout属性,就可以实现了。
'layout'=>"{summary}\n{items}\n{pager}"
回答by Sajjad Dehghani
To change only summaryoption, you can use:
要仅更改summary选项,您可以使用:
'summary' => "{begin} - {end} {count} {totalCount} {page} {pageCount}",
Then, if you want to empty summaryset with empty string value like:
然后,如果您想summary使用空字符串值清空集,例如:
'summary'=> "",
and to change layouts you can use:
并更改布局,您可以使用:
'layout'=> "{summary}\n{items}\n{pager}"
Then, if you want to empty layoutsset layout with empty string value like:
然后,如果您想layouts使用空字符串值清空设置布局,例如:
'layout'=> "",
So, for sample, i think bellow sample code can be help know how to change and custome GridViewtable in Yii 2:
因此,对于示例,我认为以下示例代码可以帮助您了解如何GridView在 Yii 2 中更改和自定义表:
<?= \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',
]);
},
]
],
],
]); ?>
回答by Ajey
if you want only grid items use 'layout'=>"{items}"
如果您只想使用网格项目 'layout'=>"{items}"
if you want only summary use 'layout'=>"{summary}"
如果你只想摘要使用 'layout'=>"{summary}"
if you want only sorter use 'layout'=>"{pager}"
如果您只想使用分拣机 'layout'=>"{pager}"
回答by Developerium
Set the paremeter summaryTextto empty string:
将参数设置summaryText为空字符串:
array(
'summaryText' => '',
'dataProvider' => $model->search(),
...
回答by Ziya Vakhobov
[
'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'
]

