Home > Backend Development > PHP Tutorial > yii2 drop-down box with search function, yii2 drop-down box search function_PHP tutorial

yii2 drop-down box with search function, yii2 drop-down box search function_PHP tutorial

WBOY
Release: 2016-07-12 08:52:57
Original
1337 people have browsed it

yii2 drop-down box with search function, yii2 drop-down box search function

It is a simple little function, but it is 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, and you are welcome to reprint it. However, this statement must be retained without the author's consent, and the original text must be given in a prominent position on the article page. connection, 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 conversation will be endless.

<code class="hljs nginx"><span class="hljs-attribute">composer require kartik-v/yii2-widget-<span class="hljs-literal">select2 <span class="hljs-string">"<span class="hljs-variable">@dev"
<span class="hljs-comment">#特别说明,因为这里安装的dev版本,也就是开发版本,不稳定版本,如果你的项目是git托管的,composer安装下来之后这里记得删掉 \vendor\kartik-v\yii2-widget-select2目录下的.git文件,不然你提交不上去的哦
</span></span></span></span></span></code>
Copy after login

Wait for about 5 minutes and it will be installed. Then we can start using it as follows
//If your form is ActiveForm, please use

<code class="hljs xquery">use kartik\select2\Select2; 
//$data是键值对数组哦,key-value ,下面所声明的所有$data均为键值对数组,以该数组为例 
$data = [<span class="hljs-number">2 => <span class="hljs-string">'widget', <span class="hljs-number">3 => <span class="hljs-string">'dropDownList', <span class="hljs-number">4 => <span class="hljs-string">'yii2']; 
echo $form->field($model, <span class="hljs-string">'title')->widget(Select2::classname(), [  
    <span class="hljs-string">'data' => $data, 
    <span class="hljs-string">'options' => [<span class="hljs-string">'placeholder' => <span class="hljs-string">'请选择 ...'], 
]);
</span></span></span></span></span></span></span></span></span></span></span></code>
Copy after login

//If your form is non-ActiveForm, you can refer to the following

<code class="hljs php"><span class="hljs-keyword">use <span class="hljs-title">kartik\<span class="hljs-title">select2\<span class="hljs-title">Select2; 
<span class="hljs-keyword">echo Select2::widget([ <span class="hljs-string">'name' => <span class="hljs-string">'title', 
    <span class="hljs-string">'data' => $data, 
    <span class="hljs-string">'options' => [<span class="hljs-string">'placeholder' => <span class="hljs-string">'请选择...'] 
]);

</span></span></span></span></span></span></span></span></span></span></span></code>
Copy after login

When updating data that is not generated by ActiveFomr, it needs to be selected by default. It is easy to handle. Just add value

<code class="hljs php"><span class="hljs-keyword">use <span class="hljs-title">kartik\<span class="hljs-title">select2\<span class="hljs-title">Select2; 
<span class="hljs-keyword">echo Select2::widget([ 
    <span class="hljs-string">'name' => <span class="hljs-string">'title', 
    <span class="hljs-string">'value' => <span class="hljs-number">2, 
    <span class="hljs-string">'data' => $data, 
    <span class="hljs-string">'options' => [<span class="hljs-string">'placeholder' => <span class="hljs-string">'请选择...'] 
]);
<span class="hljs-comment">#但是如果你的表单是ActiveForm生成的,但是往往字段不是表字段怎么办呢?更好办啦,以上面的为例,你只需要指定$model->title = ['title1', 'title2'];即可

</span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
Copy after login

Continue reading

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1125683.htmlTechArticleyii2 drop-down box with search function, yii2 drop-down box search function is a simple and small function, but it is still quite fun to use . Share it so that more people can have faster development efficiency and be happy...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template