如何在Yii2 GridView中启用和禁用排序?

时间:2022-10-05 08:02:59

How to enable and disable sort in Yii2 GridView ?

如何在Yii2 GridView中启用和禁用排序?

如何在Yii2 GridView中启用和禁用排序?

4 个解决方案

#1


30  

You can customize columns sort in your DataProvider. For example if you use ActiveDataProvider in your GridView you can indicate sort-able columns like below:

您可以在DataProvider中自定义列排序。例如,如果在GridView中使用ActiveDataProvider,则可以指示可排序的列,如下所示:

$dataProvider = new ActiveDataProvider([
    'query' => Model::find(),
    'sort' => ['attributes' => ['column1','column2']]
]);

In above example, only column1 and column2 are sort-able.

在上面的示例中,只有column1和column2是可排序的。

You can also disable sorting for all columns like below:

您还可以禁用所有列的排序,如下所示:

'sort' =>false

It is suggested to take a look at Yii2's official document : Class yii\data\Sort As it defines it:

建议看一下Yii2的官方文档:Class yii \ data \ Sort它定义它:

Sort represents information relevant to sorting. When data needs to be sorted according to one or several attributes, we can use Sort to represent the sorting information and generate appropriate hyperlinks that can lead to sort actions.

Sort表示与排序相关的信息。当需要根据一个或多个属性对数据进行排序时,我们可以使用Sort来表示排序信息并生成可导致排序操作的适当超链接。

#2


15  

In addition to Ali's answer, for aggregated and related columns you could do the following:

除了Ali的答案,对于聚合和相关列,您可以执行以下操作:

public function actionIndex()
{
    $dataProvider = new ActiveDataProvider([
          'query' => User::find()->joinWith('role'),
          'sort' => ['attributes' => [
                   //Normal columns
                   'username',
                   'email',
                   //aggregated columns
                   'full_name' => [
                        'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
                        'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
                        'default' => SORT_DESC
                   ],
                   //related columns
                   'role.name' => [
                        'asc' => ['user_role.name' => SORT_ASC],
                        'desc' => ['user_role.name' => SORT_DESC],
                        'default' => SORT_DESC
                   ],
              ],],
    ]);
}

Source: http://www.yiiframework.com/doc-2.0/yii-data-sort.html

资料来源:http://www.yiiframework.com/doc-2.0/yii-data-sort.html

#3


4  

You can disable sort in controller like this:

您可以在控制器中禁用排序,如下所示:

$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->sort->sortParam = false;

#4


2  

If you want disable sorting from gridview for particular column then do like this:

如果您想要从gridview中为特定列禁用排序,请执行以下操作:

 [
     'attribute' => 'name',
     'enableSorting' => false
 ],

by using 'enableSorting' => false

使用'enableSorting'=> false

#1


30  

You can customize columns sort in your DataProvider. For example if you use ActiveDataProvider in your GridView you can indicate sort-able columns like below:

您可以在DataProvider中自定义列排序。例如,如果在GridView中使用ActiveDataProvider,则可以指示可排序的列,如下所示:

$dataProvider = new ActiveDataProvider([
    'query' => Model::find(),
    'sort' => ['attributes' => ['column1','column2']]
]);

In above example, only column1 and column2 are sort-able.

在上面的示例中,只有column1和column2是可排序的。

You can also disable sorting for all columns like below:

您还可以禁用所有列的排序,如下所示:

'sort' =>false

It is suggested to take a look at Yii2's official document : Class yii\data\Sort As it defines it:

建议看一下Yii2的官方文档:Class yii \ data \ Sort它定义它:

Sort represents information relevant to sorting. When data needs to be sorted according to one or several attributes, we can use Sort to represent the sorting information and generate appropriate hyperlinks that can lead to sort actions.

Sort表示与排序相关的信息。当需要根据一个或多个属性对数据进行排序时,我们可以使用Sort来表示排序信息并生成可导致排序操作的适当超链接。

#2


15  

In addition to Ali's answer, for aggregated and related columns you could do the following:

除了Ali的答案,对于聚合和相关列,您可以执行以下操作:

public function actionIndex()
{
    $dataProvider = new ActiveDataProvider([
          'query' => User::find()->joinWith('role'),
          'sort' => ['attributes' => [
                   //Normal columns
                   'username',
                   'email',
                   //aggregated columns
                   'full_name' => [
                        'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
                        'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
                        'default' => SORT_DESC
                   ],
                   //related columns
                   'role.name' => [
                        'asc' => ['user_role.name' => SORT_ASC],
                        'desc' => ['user_role.name' => SORT_DESC],
                        'default' => SORT_DESC
                   ],
              ],],
    ]);
}

Source: http://www.yiiframework.com/doc-2.0/yii-data-sort.html

资料来源:http://www.yiiframework.com/doc-2.0/yii-data-sort.html

#3


4  

You can disable sort in controller like this:

您可以在控制器中禁用排序,如下所示:

$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->sort->sortParam = false;

#4


2  

If you want disable sorting from gridview for particular column then do like this:

如果您想要从gridview中为特定列禁用排序,请执行以下操作:

 [
     'attribute' => 'name',
     'enableSorting' => false
 ],

by using 'enableSorting' => false

使用'enableSorting'=> false