Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 先按降序排序Yi2 Gridview_Sorting_Gridview_Yii2 - Fatal编程技术网

Sorting 先按降序排序Yi2 Gridview

Sorting 先按降序排序Yi2 Gridview,sorting,gridview,yii2,Sorting,Gridview,Yii2,我有一个由Yii2 GridView呈现的表。表头包含按日期排序的链接。如果我单击它,它将首先按升序对表进行排序,然后再按降序对表进行排序。但我希望在第一次单击时按降序排列 我在搜索控制器(asc->SORT\u DESC)的搜索方法中进行了一次破解,解决了这个问题: 有更好的解决方案吗?使用: “default”元素指定属性当前未排序时应按哪个方向排序(默认值为升序) 使用: “default”元素指定属性当前未排序时应按哪个方向排序(默认值为升序) 您可以使用$dataProvider中的排

我有一个由Yii2 GridView呈现的表。表头包含按日期排序的链接。如果我单击它,它将首先按升序对表进行排序,然后再按降序对表进行排序。但我希望在第一次单击时按降序排列

我在搜索控制器(asc->SORT\u DESC)的搜索方法中进行了一次破解,解决了这个问题:

有更好的解决方案吗?

使用:

“default”元素指定属性当前未排序时应按哪个方向排序(默认值为升序)

使用:

“default”元素指定属性当前未排序时应按哪个方向排序(默认值为升序)

您可以使用
$dataProvider
中的
排序
选项。它将以升序显示数据,当您第一次单击列时,它将首先以降序显示

我查过了。它对我有用

有关更多信息,请查看

您可以使用
$dataProvider
中的
排序
选项。它将以升序显示数据,当您第一次单击列时,它将首先以降序显示

我查过了。它对我有用

有关更多信息,请查看我的解决方案:

添加Sort.php

在控制器中:

我的解决方案:

添加Sort.php

在控制器中:


最好的解决方案是:
$dataProvider->sort->attributes['updated_at']['default']=sort_DESC
。这使得所有其他默认行为保持不变,最好的解决方案是:
$dataProvider->sort->attributes['updated\u at']['default']=sort\u DESC
。这使得所有其他默认行为保持不变,这是不正确的-顺序需要定义为
SORT\u DESC
SORT\u ASC
。但对我来说是不正确的。这是不正确的-顺序需要定义为
SORT\u DESC
SORT\u ASC
。但对我来说是有效的。
   $dataProvider->sort->attributes['updated_at'] = [ 
      'asc'  => [$this->tablename() . '.updated_at' => SORT_DESC ], 
      'desc' => [$this->tablename() . '.updated_at' => SORT_ASC], 
   ]; 
$dataProvider->sort->attributes['updated_at'] = [ 
    'default' => SORT_DESC
]; 
$dataProvider = new ActiveDataProvider([
  'query' => YourClass::find(),
  'sort' => [
    'defaultOrder' => [
        'updated_at' => SORT_ASC,
    ],
  ],
]);
namespace backend\components;

use yii\base\InvalidConfigException;

class Sort extends \yii\data\Sort
{
/**
 * @var int
 */
public $defaultSort = SORT_DESC;

/**
 * Rewrite
 * @param string $attribute the attribute name
 * @return string the value of the sort variable
 * @throws InvalidConfigException if the specified attribute is not defined in [[attributes]]
 */
public function createSortParam($attribute)
{
    if (!isset($this->attributes[$attribute])) {
        throw new InvalidConfigException("Unknown attribute: $attribute");
    }
    $definition = $this->attributes[$attribute];
    $directions = $this->getAttributeOrders();
    if (isset($directions[$attribute])) {
        $direction = $directions[$attribute] === SORT_DESC ? SORT_ASC : SORT_DESC;
        unset($directions[$attribute]);
    } else {
        $direction = isset($definition['default']) ? $definition['default'] : $this->defaultSort;
    }

    if ($this->enableMultiSort) {
        $directions = array_merge([$attribute => $direction], $directions);
    } else {
        $directions = [$attribute => $direction];
    }

    $sorts = [];
    foreach ($directions as $attribute => $direction) {
        $sorts[] = $direction === SORT_DESC ? '-' . $attribute : $attribute;
    }

    return implode($this->separator, $sorts);
}
}
    $dataProvider = new ActiveDataProvider([
        'query' => MyModel::find(),
    ]);
    /**@var $sort \backend\components\Sort */
    $sort = Yii::createObject(array_merge(['class' => Sort::className()], [
        'defaultOrder' => [
            '_id' => SORT_ASC,
        ],
    ]));
    $dataProvider->setSort($sort);
$dataProvider->sort = ['defaultOrder' => ['id' => 'DESC']];