Home > php教程 > php手册 > body text

yii2下拉框带搜索功能 - 白狼栈

WBOY
Release: 2016-05-20 10:14:30
Original
1172 people have browsed it

简单的小功能,但是用起来还是蛮爽的。分享出来让更多的人有更快的开发效率,开开心心快乐编程。
作者:白狼 出处:http://www.manks.top/yii2_dropdown_search.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

如果你还没有使用过composer,你可就out了,看我的教程分享,composer简直就是必备神奇有木有。都说到这个点上了,我们赶紧使用composer进行安装吧。

不急,先来看看效果图是啥样的,不然都没心情没欲望看下去。原文自带图片,可参考。

啥玩意,不感兴趣?继续看嘛,看完再操作一边才能觉得好在哪里。
有木有感觉很帅气,当然啦,远远不止,还很上档次用起来效果也是杠杠的有木有。
好了好了,抓紧时间安装,不然聊起来真是没完没了。

<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

等他个大概5分钟的样子差不多了,安装就好了,然后我们就可以像下面一样开始使用了
//如果你的表单是ActiveForm,请使用

<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

//如果你的表单是非ActiveForm,可以参考下面的

<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

非ActiveFomr生成的更新数据的时候就需要默认选中,好办,加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

继续阅读

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template