【问题标题】:Sorting Yii2 Gridview in descending order first先对 Yii2 Gridview 进行降序排序
【发布时间】:2016-10-01 23:42:20
【问题描述】:

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

我在搜索控制器的搜索方法(asc->SORT_DESC)中进行了破解:

   $dataProvider->sort->attributes['updated_at'] = [ 
      'asc'  => [$this->tablename() . '.updated_at' => SORT_DESC ], 
      'desc' => [$this->tablename() . '.updated_at' => SORT_ASC], 
   ]; 

有没有更好的解决方案?

【问题讨论】:

    标签: sorting gridview yii2


    【解决方案1】:
    $dataProvider->sort = ['defaultOrder' => ['id' => 'DESC']];
    

    【讨论】:

    • 这是不正确的 - 订单需要定义为SORT_DESCSORT_ASC
    • 为我工作。
    【解决方案2】:

    我的解决方案:

    添加排序.php

    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);
    

    【讨论】:

      【解决方案3】:
      $dataProvider = new ActiveDataProvider([
        'query' => YourClass::find(),
        'sort' => [
          'defaultOrder' => [
              'updated_at' => SORT_ASC,
          ],
        ],
      ]);
      

      您可以在$dataProvider 中使用sort 选项。它将在ascending order 中显示数据,当您第一次单击列时,它将首先显示在descending order 中。

      我检查过了。它对我有用。

      欲了解更多信息,请查看Rendering Data In List View & Grid View : Yii2

      【讨论】:

        【解决方案4】:

        使用default

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

        $dataProvider->sort->attributes['updated_at'] = [ 
            'default' => SORT_DESC
        ]; 
        

        【讨论】:

        • 最佳解决方案是:$dataProvider->sort->attributes['updated_at']['default'] = SORT_DESC。这使所有其他默认行为保持不变,
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-25
        • 1970-01-01
        相关资源
        最近更新 更多