2016-01-19 60 views
0

我需要做的違約編號排序,我們不顯示在網格中的ID爲什麼默認分類並不在這裏工作是我的代碼進行排序Yii2默認排序不工作

public function search($params) 
    { 
     $this->load($params); 
     $query = new \yii\db\Query; 
     $expression = new \yii\db\Expression('CASE WHEN b.status = 1 THEN "Active" WHEN b.status = 2 THEN "Inactive" END AS status'); 
     $query->select(['b.image','bl.name',$expression,'b.brand_id']) 
       ->from('brand AS b') 
       ->join('INNER JOIN', 
        'brand_lang AS bl', 
        'bl.brand_id = b.brand_id AND lang_id = 1'); 

     $query->andFilterWhere([    
      'status' => $this->status, 
     ]); 
     $query->andFilterWhere(['like', 'name', $this->name]); 

     $command = $query->createCommand(); 
     $data = $command->queryAll();  
     $dataProvider = new \yii\data\ActiveDataProvider([ 
      'query' => $query, 
      'totalCount' => count($data), 
      'sort' => [ 
       'attributes' => [ 
        'name' => [ 
         'asc' => ['name' => SORT_ASC, 'name' => SORT_ASC], 
         'desc' => ['name' => SORT_DESC, 'name' => SORT_DESC], 
         'default' => SORT_DESC, 
         'label' => 'Name', 
        ], 
        'status' => [ 
         'asc' => ['status' => SORT_ASC, 'status' => SORT_ASC], 
         'desc' => ['status' => SORT_DESC, 'status' => SORT_DESC], 
         'default' => SORT_DESC, 
         'label' => 'Status', 
        ], 
        'brand_id' => [ 
         'asc' => ['brand_id' => SORT_ASC, 'brand_id' => SORT_ASC], 
         'desc' => ['brand_id' => SORT_DESC, 'brand_id' => SORT_DESC], 
         'default' => SORT_ASC, 
         'label' => 'Brand', 
        ], 
        'defaultOrder' => ['brand_id' => SORT_ASC] 
       ], 
      ], 
      'pagination' => [ 
       'pageSize' => 20, 
      ], 
     ]); 
     return $dataProvider; 
    } 

誰能告訴我這是什麼解決方案?我做了很多谷歌上搜索,但沒有成功

回答

1

defaultOrder PARAM應該在sort,而不是在attributes

'sort' => [ 
    'attributes' => [...], 
    'defaultOrder' => ['brand_id' => SORT_ASC], 
], 
1

使用$dataProvider->sort->attributes['attribute_name']

$dataProvider = new ActiveDataProvider([ 
     'query' => $query, 

     'sort' => [ 
      'defaultOrder' => ['brand_id' => SORT_ASC] 
     ], 
    ]); 

    $dataProvider->sort->attributes['name'] = [ 
     'asc' => ['name' => SORT_ASC], 
     'desc' => ['name' => SORT_DESC], 
    ]; 
+0

你的答案是正確的,但什麼是標準,爲什麼它不在排序數組中使用。 –

+0

@HiteshJangid。 [defaultOrder](http://www.yiiframework.com/doc-2.0/yii-data-sort.html#$defaultOrder-detail) –