Example of a drop-down list with search function in yii2 and a drop-down list in yii2
Simple and small functions, but it is quite easy to use. Share it to let more people have a faster development efficiency, happy and happy programming.
If you have not used composer, you can get out of it. In my tutorials, composer is absolutely magical. Let's talk about this point. Let's use composer for installation.
First, we will show you:
The following describes the implementation methods:
Composer require kartik-v/yii2-widget-select2 "@ dev" # special note, because the dev version installed here, that is, the development version, the unstable version, if your project is hosted by git, after the composer is installed, remember to delete the \ vendor \ kartik-v \ yii2-widget-select2 directory. git file, or you cannot submit it.
It will take about five minutes for him to complete the installation. Then we can start using it like below.
// If your form is ActiveForm, use
Use kartik \ select2 \ Select2; // $ data is a key-value Pair array. key-value, all $ data declared below are key-value pairs, take this array as an example $ data = [2 => 'widget ', 3 => 'dropdownlist', 4 => 'ii2']; echo $ form-> field ($ model, 'title')-> widget (Select2: classname (), ['data' => $ data, 'options' => ['holder' => 'select... '],]);
// If your form is not ActiveForm, you can refer to the following
Use kartik \ select2 \ Select2; echo Select2: widget (['name' => 'title', 'data' => $ data, 'options' => ['holder' => 'select... ']);
# When updating data generated by non-ActiveFomr, it must be selected by default. Add the value.
Use kartik \ select2 \ Select2; echo Select2: widget (['name' => 'title', 'value' => 2, 'data' => $ data, 'options' => ['holder' => 'select... ']);
# What if your form is generated by ActiveForm but the field is often not a table field? For example, you only need to specify $ model-> title = ['title1', 'title2 '];
The above content is a detailed example of the yii2 drop-down box with search function introduced by xiaobian. I hope to help you and thank you very much for your support for the help House website!