Sorting 在gridview(Yii2)中将数组用作排序条件
在一个yii2gridview表中,我有一个简单的类型数组[1=>car,2=>motobike,3=>ferry],我想用于按字母顺序排序(而不是经典关系) 如何与数据提供程序进行设置?非常感谢您的帮助Sorting 在gridview(Yii2)中将数组用作排序条件,sorting,gridview,yii2,Sorting,Gridview,Yii2,在一个yii2gridview表中,我有一个简单的类型数组[1=>car,2=>motobike,3=>ferry],我想用于按字母顺序排序(而不是经典关系) 如何与数据提供程序进行设置?非常感谢您的帮助 <?= GridView::widget([ 'dataProvider' => $dataProvider, // 'filterModel' => $searchModel, 'tableOptions
<?= 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 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。。这些是数据库中数据的表示,所以数据库不知道1在框架中被称为“car”。您应该将类型存储为字符串(这不是一个好主意),或者在它们的计数中手动对它们进行排序(比如1=>car,2=>ferry,3=>motobike) 但是,如果您对此不满意,那么您可以使用类似的东西解决问题(尽管它脏而且慢,我不建议以这种方式实现) 我可能会错过你代码的一些细节。
在我的例子中,我使用存储在DB中的状态0,1,2进行测试。。(并在web中显示为已删除、活动、禁用..),因此它在字符串代表中运行和排序非常好添加gridview代码和相关控制器/操作代码更新您的问题。。pelaseHi scaisEdge,我的gridview代码(相关部分)已添加到我的初始PostYerke中,非常感谢,我会尝试一下,看看它是否能完成任务-如果不行,我会将数组数据存储在数据库中!
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,
]);
}
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