具體怎么實(shí)現(xiàn)喃?考慮到一張數(shù)據(jù)表要下拉效果的字段可能有很多個(gè),我們先在其model中實(shí)現(xiàn)一個(gè)方法方便后續(xù)操作
/**
* 下拉篩選
* @column string 字段
* @value mix 字段對(duì)應(yīng)的值,不指定則返回字段數(shù)組
* @return mix 返回某個(gè)值或者數(shù)組
*/
public static function dropDown ($column, $value = null)
{
$dropDownList = [
'is_delete'=> [
'0'=>'顯示',
'1'=>'刪除',
],
'is_hot'=> [
'0'=>'否',
'1'=>'是',
],
//有新的字段要實(shí)現(xiàn)下拉規(guī)則,可像上面這樣進(jìn)行添加
// ......
];
//根據(jù)具體值顯示對(duì)應(yīng)的值
if ($value !== null)
return array_key_exists($column, $dropDownList) ? $dropDownList[$column][$value] : false;
//返回關(guān)聯(lián)數(shù)組,用戶下拉的filter實(shí)現(xiàn)
else
return array_key_exists($column, $dropDownList) ? $dropDownList[$column] : false;
}
然后我們上代碼看看具體怎么實(shí)現(xiàn)的下拉搜索
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
// ......
[
'attribute' => 'is_hot',
'value' => function ($model) {
return Article::dropDown('is_hot', $model->is_hot);
},
'filter' => Article::dropDown('is_hot'),
],
[
'attribute' => 'is_delete',
'value' => function ($model) {
return Article::dropDown('is_delete', $model->is_delete);
},
'filter' => Article::dropDown('is_delete'),
],
// ......
],
]); ?>
像這樣,我們就簡(jiǎn)單地實(shí)現(xiàn)了兩個(gè)下拉效果,要實(shí)現(xiàn)篩選功能,在你的dataProvider自定添加該字段的搜索條件即可。