【发布时间】:2016-01-19 10:44:38
【问题描述】:
我需要按 id 进行默认排序,我们不在网格中显示 id 这就是为什么默认排序在这里不起作用的原因是我的排序代码
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;
}
谁能告诉我这个问题的解决方案是什么?我做了很多谷歌搜索,但没有成功
【问题讨论】: