Simple analysis of Yii2 GridView implementation Drop-down Search Tutorial _php instance

Source: Internet
Author: User

No more nonsense, first to show you the effect of the picture, if you are still very satisfied please continue reading:

Specifically how to achieve Nan? Considering a data table to pull down the effect of the field may have many, we first in its model to implement a method to facilitate subsequent operations

/** * Drop-down
filter
* @column string field
* @value the corresponding value of the Mix field, not specified returns the field array
* @return Mix returns a value or array/public
s tatic function DropDown ($column, $value = null)
{
$dropDownList = [
' is_delete ' => ['
0 ' => ' show '] c11/> ' 1 ' => ' delete ',
],
' is_hot ' => [
' 0 ' => ' no ',
' 1 ' => ' is ',
],
///There is a new field to implement the dropdown rule , and can be added as above
//...
];
Display the corresponding value based on the specific value
if ($value!== null) return 
array_key_exists ($column, $dropDownList)? $dropDownList [$column] [$value]: false;
Returns the associative array, the user Drop-down filter implementation
else return
array_key_exists ($column, $dropDownList)? $dropDownList [$column]: false;
}

And then we'll take a look at the code to see how the dropdown search is implemented

<?= gridview::widget ([
' Dataprovider ' => $dataProvider,
' columns ' => [/
...
] [
' attribute ' => ' is_hot ', '
value ' => function ($model) {return
Article::d ropdown (' Is_hot ', $model- >is_hot);
},
' filter ' => Article::d ropdown (' Is_hot '),
],
[
' attribute ' => ' Is_ Delete ',
' value ' => function ($model) {return
Article::d ropdown (' Is_delete ', $model->is_delete);
} ,
' filter ' => Article::d ropdown (' Is_delete '),
],
//... ...
]
;?>

Like this, we simply implemented a two drop-down effect, to implement the filtering function, in your dataprovider to add the field of the search criteria can be.

Recommended readings for cloud-dwelling communities:

Simple analysis of common operation of GridView in Yii2

Yii2 the bottom of the page load CSS and JS tips

Brief analysis of YII2 GridView date format and date to search tutorials

A brief analysis of YII2 GridView implementation Drop-down Search Tutorial

About YII2 GridView implementation of the Drop-down search tutorial to introduce so many people, I hope to 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.