yii2 GridView对列单击进行排序

时间:2015-06-26 15:07:15

标签: gridview yii2

我学习yii2,尝试使用可排序的日期列制作GridView :: widget。

我找到了一种简单的方法,使用:'columns' => ['created_at:datetime']但我不明白如何使用自定义方式进行相同操作。

这是我的代码:

<?=GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => 
    [
        ['class' => 'yii\grid\SerialColumn'],
        'created_at:datetime',      //an easy way, makes default asc/desc sort on link click

        [
            'header' => "Date",     //the same, but how to make sort, (need to add link someway)?
            'format' => 'datetime',
            'value' => 'created_at',
            'contentOptions'=>['style'=>'width: 130px;',
                'enableSorting' => true,
            ]
        ],
    ],
    'options' => ['class'=>'doc-table'],


]);
?>

以下是$dataProvider

        $dataProvider->sort = ['defaultOrder' =>
                        //['created_at' => 'desc'],
                        ['attributes' => 
                            ['created_at' =>
                                [
                                    'asc' => ['created_at' => SORT_ASC],
                                    'desc' => ['created_at' => SORT_DESC],
                                ]
                            ]
                        ],
                    ];  
    return $this->render('index', [
                'dataProvider' => $dataProvider,
    ]);

1 个答案:

答案 0 :(得分:1)

您在'created_at'字段中使用属性名称。

像..

[
'attribute' => 'created_at',
'header' => 'date',
'Value' => .....,
]