Yii2 drop-down box with search function, yii2 drop-down box search function
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.
Author: White Wolf Source: http://www.manks.top/yii2_dropdown_search.html this article copyright to the author, welcome to reprint, but without the author's consent must retain this paragraph of the statement, and in the Article Page clearly given the original connection, otherwise, you are entitled to pursue legal liability.
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.
Don't worry. Let's first look at what it looks like, or you're not in the mood to see it. For more information, see.
What are you not interested in? Continue to read it. After reading the operation, you can find out where it is.
You may feel very handsome. Of course, there are far more than what you can do. It is also very helpful to you.
Okay, so hurry up and install it. Otherwise, the conversation will be endless.
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, You need to select it by default. You can 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 '];
Continue reading