淺析Yii2 GridView實現下拉搜尋教程_php執行個體

來源:互聯網
上載者:User

廢話不多說了,先給大家展示下效果圖,如果大家還很滿意請繼續往下閱讀:

具體怎麼實現喃?考慮到一張資料表要下拉效果的欄位可能有很多個,我們先在其model中實現一個方法方便後續操作

/*** 下拉篩選* @column string 欄位* @value mix 欄位對應的值,不指定則返回欄位數組* @return mix 返回某個值或者數組*/public static function dropDown ($column, $value = null){$dropDownList = ['is_delete'=> ['0'=>'顯示','1'=>'刪除',],'is_hot'=> ['0'=>'否','1'=>'是',],//有新的欄位要實現下拉規則,可像上面這樣進行添加// ......];//根據具體值顯示對應的值if ($value !== null) return array_key_exists($column, $dropDownList) ? $dropDownList[$column][$value] : false;//返回關聯陣列,使用者下拉的filter實現elsereturn array_key_exists($column, $dropDownList) ? $dropDownList[$column] : false;}

然後我們上代碼看看具體怎麼實現的下拉搜尋

<?= 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'),],// ......],]); ?>

像這樣,我們就簡單地實現了兩個下拉效果,要實現篩選功能,在你的dataProvider自定添加該欄位的搜尋條件即可。

雲棲社區推薦閱讀:

淺析Yii2中GridView常見操作

yii2 頁面底部載入css和js的技巧

淺析Yii2 GridView 日期格式化並實現日期可搜尋教程

淺析Yii2 GridView實現下拉搜尋教程

關於Yii2 GridView實現下拉搜尋教程就給大家介紹這麼多,希望對大家有所協助!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.