setSort() public method

Sets the sort definition for this data provider.
public setSort ( array | Sort | boolean $value )
$value array | Sort | boolean the sort definition to be used by this data provider. This can be one of the following: - a configuration array for creating the sort definition object. The "class" element defaults to 'yii\data\Sort' - an instance of [[Sort]] or its subclass - false, if sorting needs to be disabled.
コード例 #1
0
 public function setSort($value)
 {
     parent::setSort($value);
     if (($sort = $this->getSort()) !== false && empty($sort->attributes)) {
         /** @var Model $model */
         $model = new $this->modelClass();
         if ($model instanceof Model) {
             foreach ($model->attributes() as $attribute) {
                 $sort->attributes[$attribute] = ['asc' => [$attribute => SORT_ASC], 'desc' => [$attribute => SORT_DESC], 'label' => $model->getAttributeLabel($attribute)];
             }
         }
     }
 }
コード例 #2
0
 /**
  * @inheritdoc
  */
 public function setSort($value)
 {
     parent::setSort($value);
     if (($sort = $this->getSort()) !== false && $this->query instanceof ActiveQueryInterface) {
         /* @var $model Model */
         $model = new $this->query->modelClass();
         if (empty($sort->attributes)) {
             foreach ($model->attributes() as $attribute) {
                 $sort->attributes[$attribute] = ['asc' => [$attribute => SORT_ASC], 'desc' => [$attribute => SORT_DESC], 'label' => $model->getAttributeLabel($attribute)];
             }
         } else {
             foreach ($sort->attributes as $attribute => $config) {
                 if (!isset($config['label'])) {
                     $sort->attributes[$attribute]['label'] = $model->getAttributeLabel($attribute);
                 }
             }
         }
     }
 }