我需要按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;
}
有谁能告诉我这是什么解决方案?我做了很多谷歌搜索,但没有取得成功
答案 0 :(得分:1)
您的defaultOrder
参数应位于sort
,而不是attributes
:
'sort' => [
'attributes' => [...],
'defaultOrder' => ['brand_id' => SORT_ASC],
],
答案 1 :(得分: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],
];