Analysis of Yii2GridView implementation drop-down search tutorial _ php instance

Source: Internet
Author: User
This article mainly introduces the introduction of Yii2GridView's implementation of drop-down search tutorials. If you need more information, I will show you more, if you are still satisfied, please continue reading:

How can we implement this feature? Considering that a data table may have many drop-down fields, we first implement a method in its model to facilitate subsequent operations.

/*** Drop-down filter * @ column string field * @ value the value corresponding to the mix field, if this parameter is not specified, the returned field array * @ return mix returns a value or array */public static function dropDown ($ column, $ value = null) {$ dropDownList = ['is _ delete' => ['0' => 'display', '1' => 'delete',], 'Is _ hot '=> ['0' => 'no', '1' =>' Yes ',], // a new field must be added to the drop-down list, you can add/...] Like above; // display the corresponding value if ($ value! = Null) return array_key_exists ($ column, $ dropDownList )? $ DropDownList [$ column] [$ value]: false; // returns an associated array. the user-drop-down filter implements elsereturn array_key_exists ($ column, $ dropDownList )? $ DropDownList [$ column]: false ;}

Then let's go to the Code to see how to implement the drop-down search

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

In this way, we simply implement two drop-down effects. To implement the filter function, you can add the search criteria for this field in your dataProvider.

Recommended reading:

Common Operations of GridView in Yii2

Tips for loading css and js at the bottom of the yii2 page

Analysis on how to format and implement date searchable in Yii2 GridView

Analysis of Yii2 GridView implementation drop-down search tutorial

The Yii2 GridView implementation drop-down search tutorial will introduce you so much, I hope it will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.