[英]Use array as sorting criteria in gridview (Yii2)
在yii2 gridview表中,我有一個簡單的類型數組[1 => car,2 => motobike,3 => ferry],我想將其用於字母排序(而不是經典的關系)。
如何與數據提供者進行設置? 任何幫助深表感謝!
<?= GridView::widget([
'dataProvider' => $dataProvider,
// 'filterModel' => $searchModel,
'tableOptions' => ['class' => 'table'],
'layout'=>"{summary}\n<div class='rounded'>{items}</div>\n{pager}",
'summary' => "Zeige {begin} - {end} von {totalCount} Ideen",
'columns' => [
'id',
[
'attribute' => 'art',
'value' => function ($model) {
$arten = \Yii::$app->params['ideenArten']; //[1=>car, 2=>bike, 3=>scooter etc]
return $arten[$model->art];
},
'label' => 'Kategorie',
'filter' => Html::activeDropDownList($searchModel, 'art', \Yii::$app->params['ideenArten'],['class'=>'form-control','prompt' => 'Select Type']),
],
],
]); ?>
在下面的搜索模型中,我使用關系成功地為其他一些字段設置了排序:
public function search($params)
{
$query = Idee::find();
// add conditions that should always apply here
$query->joinWith(['authorprofile']);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->sort->attributes['authorprofile'] = [
// The tables are the ones our relation are configured to
// in my case they are prefixed with "tbl_"
'asc' => ['user_profile.lastname' => SORT_ASC],
'desc' => ['user_profile.lastname' => SORT_DESC],
];
$dataProvider->sort->attributes['art'] = [
'asc' => ['user_profile.lastname' => SORT_ASC],
'desc' => ['user_profile.lastname' => SORT_DESC],
];
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'art' => $this->art,
'status' => $this->status,
'projektstart' => $this->projektstart,
'projektende' => $this->projektende,
'created_by' => $this->created_by,
'updated_by' => $this->updated_by,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere(['like', 'title', $this->title])
->andFilterWhere(['like', 'body', $this->body])
->andFilterWhere(['or',
['like', 'user_profile.firstname', $this->authorprofile],
['like', 'user_profile.lastname', $this->authorprofile],
]);
return $dataProvider;
}
控制器代碼為:
public function actionSpeicher()
{
$lastIdea = Idee::find()->orderBy("updated_at DESC")->one();
$searchModel = new IdeeSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'lastIdea' => $lastIdea,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
1,2,3 ..這些是DB中數據的表示形式,因此您的數據庫不知道1在框架中被稱為“汽車”。 您應該將類型存儲為字符串(這不是一個好主意),或者手動按其編號對其進行排序(例如1 => car,2 => ferry,3 => motobike)。
但是,如果您對此不滿意,則可以使用類似的方法來解決(盡管它又臟又慢,我不建議以這種方式實現)
public function search($params)
{
$query = Idee::find();
// add conditions that should always apply here
$query->joinWith(['authorprofile']);
// ************* GENERATE SQL TABLE FROM arten PHP ARRAY ********************
// Get your List from params
$arten = \Yii::$app->params['ideenArten']; // [1=>car, 2=>bike, 3=>scooter etc]
$artenQuery = null;
// Convert PHP array to SQL table, pass value (1,2,3) and values (car, boke, scooter)
foreach ($arten as $key => $value) {
if(!($artenQuery instanceof \yii\db\Query)) {
$artenQuery = new \yii\db\Query();
$artenQuery->select(new \yii\db\Expression("{$key} AS value, \"{$value}\" AS label"));
} else {
$artenQuery->union("SELECT {$key} AS value, \"{$value}\" AS label");
}
}
// If you execute $artenQuery itself, you can notice that it will return a table (generated from php array of cource)
// Then we can leftJoin the table to our main table. Call our temporary arten table as "dummy"
// Make sure "art" attribute stores number from "arten" list
$query->leftJoin(['dummy' => $artenQuery], 'dummy.value = art');
// *************************************************************
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
// REGISTER SORT FOR RELATIONAL FIELD (joined 'dummy' table, which holds string representative of "art" now)
$dataProvider->sort->attributes['art'] = [
'asc' => ['dummy.label' => SORT_ASC],
'desc' => ['dummy.label' => SORT_DESC],
];
//..... continue your code
我可能會錯過您的代碼的一些細節。 在我的情況下,我測試了存儲在數據庫中的狀態為0,1,2 ..(並在Web中顯示為Deleted,Active,Disabled ..),因此它可以很好地工作並以字符串表示形式進行排序
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.