Simple little function, but still quite fun to use. Share it so that more people can develop faster and program happily.
Author: Bailang Source: http://www.manks.top/yii2_dropdown_search.html The copyright of this article belongs to the author. Reprinting is welcome, but this statement must be retained without the author’s consent, and a link to the original text must be provided in an obvious position on the article page. Otherwise, we reserve the right to pursue legal liability.
If you haven’t used composer yet, you are out. Check out my tutorial sharing. Composer is simply a must-have and magical. Having said that, let’s quickly use composer to install it.
No rush, let’s take a look at the renderings first, otherwise you won’t be in the mood or have the desire to read on. The original article comes with pictures for reference.
What the hell are you not interested in? Just keep reading. Only then can you feel the benefits after reading it.
It feels very handsome. Of course, it is much more than that. It is also very high-end and the effect is amazing when used.
Okay, okay, hurry up and install it, otherwise the chat will be endless.
<code><span>composer require kartik-v/yii2-widget-<span>select2 <span>"<span>@dev" <span>#特别说明,因为这里安装的dev版本,也就是开发版本,不稳定版本,如果你的项目是git托管的,composer安装下来之后这里记得删掉 \vendor\kartik-v\yii2-widget-select2目录下的.git文件,不然你提交不上去的哦 </span></span></span></span></span></code>
Wait for about 5 minutes and it will be installed. Then we can start using it as below
//If your form is ActiveForm, please use
<code>use kartik\select2\Select2; //$data是键值对数组哦,key-value ,下面所声明的所有$data均为键值对数组,以该数组为例 $data = [<span>2 => <span>'widget', <span>3 => <span>'dropDownList', <span>4 => <span>'yii2']; echo $form->field($model, <span>'title')->widget(Select2::classname(), [ <span>'data' => $data, <span>'options' => [<span>'placeholder' => <span>'请选择 ...'], ]); </span></span></span></span></span></span></span></span></span></span></span></code>
//If your form Whether it is a non-ActiveForm, you can refer to the following
<code><span>use <span>kartik\<span>select2\<span>Select2; <span>echo Select2::widget([ <span>'name' => <span>'title', <span>'data' => $data, <span>'options' => [<span>'placeholder' => <span>'请选择...'] ]); </span></span></span></span></span></span></span></span></span></span></span></code>
When updating data generated by a non-ActiveFormr, it needs to be selected by default. It is easy to handle, just add the value
<code><span>use <span>kartik\<span>select2\<span>Select2; <span>echo Select2::widget([ <span>'name' => <span>'title', <span>'value' => <span>2, <span>'data' => $data, <span>'options' => [<span>'placeholder' => <span>'请选择...'] ]); <span>#但是如果你的表单是ActiveForm生成的,但是往往字段不是表字段怎么办呢?更好办啦,以上面的为例,你只需要指定$model->title = ['title1', 'title2'];即可 </span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
Continue reading
The above has introduced the yii2 drop-down box with search function, including the drop-down box and yii content. I hope it will be helpful to friends who are interested in PHP tutorials.