我如何自定义 Yii2 gridview 排序?

2022-01-06 00:00:00 php yii2 datagrid

如何使用自定义的 gridview 标题进行排序?

How can I sort with a customized gridview header?

请给出Yii2 gridview widget dataproviderlabelheader 的区别代码>.

Please give the difference between label and header in the Yii2 gridview widget dataprovider.

这是我的代码:

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

headerlabel 执行相同的功能吗?

Do header and label perform the same function?

如何在 $data->myTitle 中进行排序?

How can I perform sorting in $data->myTitle?

这是我的输出屏幕:

我希望页面标题、状态、修改日期应处于活动状态.

I want Page Title, Status, Date Modified should be active.

提前致谢.

推荐答案

找到答案.

请将属性添加到您的搜索模型中的 ActiveDataProvider.

Please add attributes to ActiveDataProvider in your Search Model.

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

在widget中添加属性选项:

Add the attribute option in widget:

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

相关文章