简体   繁体   English

我如何定制Yii2 gridview排序?

[英]How i do custom Yii2 gridview sort?

How can I sort with a customized gridview header? 如何使用自定义的gridview标题进行排序?

Please give the difference between label and header in the Yii2 gridview widget dataprovider . 请在Yii2 gridview小部件dataprovider给出labelheader之间的区别。

Here is my code: 这是我的代码:

    <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'columns' => [
                 [
                   'class' => 'yii\grid\DataColumn',
                   'value' => function ($data) {
                    return $data->myTitle; 
                    },
                   'headerOptions' => ['style'=>'text-align:center'],
                   'header' => 'Page Title',
                   'label' => 'Title'
                ],
   ]); ?>

Do header and label perform the same function? headerlabel执行相同的功能?

How can I perform sorting in $data->myTitle ? 如何在$data->myTitle执行排序?

Here my Output Screen: 这是我的输出屏幕:

在此输入图像描述

I want Page Title, Status, Date Modified should be active. 我想要页面标题,状态,修改日期应该是活动的。

Thanks in advance. 提前致谢。

Found the answer. 找到了答案。

Please add attributes to ActiveDataProvider in your Search Model. 请在搜索模型中向ActiveDataProvider添加属性。

$dataProvider = new ActiveDataProvider([
        'query' => $query,
        'pagination' => [
                'pageSize' => 5,
                ],
        'sort' => ['attributes' => ['myTitle']],
       ]);

Add the attribute option in widget: 在窗口小部件中添加属性选项:

    <?= GridView::widget([
            'dataProvider' => $dataProvider,
            'columns' => [
                 [
                   'class' => 'yii\grid\DataColumn',
                   'value' => function ($data) {
                    return $data->myTitle; 
                    },
                   'headerOptions' => ['style'=>'text-align:center'],
                   'attribute' => 'myTitle',
                   'label' => 'Page Title'
                ],
   ]); ?>

Since myTitle is a field from the database and not a custom value, you can just use attribute . 由于myTitle是数据库中的字段而不是自定义值,因此您只需使用属性即可 The rest may be unnecessary eg the default class is DataColumn 其余可能是不必要的,例如默认classDataColumn

'columns' => [ 
    [ 
        'attribute' => 'myTitle',
        'label' => 'Label',
    ]

I am not very sure I understand your question, but sorting option can be included in your modelsearch.php . 我不太清楚我理解你的问题,但排序选项可以包含在你的modelsearch.php So in your case you have to do like this. 所以在你的情况下你必须这样做。

$dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort'=> ['defaultOrder' => ['your_column'=>SORT_ASC]]
        ]);

if myTitle is a field in the database, why you are using such a long syntax. 如果myTitle是数据库中的一个字段,为什么要使用如此长的语法。 Just 只是

'Columns'=>[
           .. 
           'myTitle',
           ..
           ],

should work fine and should be active for sorting as you want 应该工作正常,应该活跃进行排序

if you want a different header/label for the column, use label instead of header as header is only a cell content and cannot be used for sorting, while label can. 如果你想为列提供不同的标题/标签,请使用标签而不是标题,因为标题只是一个单元格内容,不能用于排序,而标签可以。 details 细节

[
..
'attribute'=>'myTitle',
'label' => 'Page Title'
..
],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM