Table of Contents
yii2下拉框带搜索功能,yii2下拉框搜索功能
Home php教程 php手册 yii2下拉框带搜索功能,yii2下拉框搜索功能

yii2下拉框带搜索功能,yii2下拉框搜索功能

Jun 13, 2016 am 08:40 AM
Drop-down menu

yii2下拉框带搜索功能,yii2下拉框搜索功能

简单的小功能,但是用起来还是蛮爽的。分享出来让更多的人有更快的开发效率,开开心心快乐编程。
作者:白狼 出处: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

继续阅读

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to make drop-down menu in WPS table How to make drop-down menu in WPS table Mar 21, 2024 pm 01:31 PM

How to make the WPS table drop-down menu: After selecting the cell where you want to set the drop-down menu, click &quot;Data&quot;, &quot;Validity&quot; in sequence, and then make the corresponding settings in the pop-up dialog box to pull down our menu. As a powerful office software, WPS has the ability to edit documents, statistical data tables, etc., which provides a lot of convenience for many people who need to deal with text, data, etc. In order to skillfully use WPS software to provide us with a lot of convenience, we need to be able to master various very basic operations of WPS software. In this article, the editor will share with you how to use WPS software. Perform drop-down menu operations in the WPS table that appears. After opening the WPS form, first select the

How to add artistic page borders in Microsoft Word How to add artistic page borders in Microsoft Word Apr 27, 2023 pm 08:25 PM

Are you tired of seeing the traditional black borders on your Word documents all the time? Are you looking for ways how to add some colorful and artistic borders to your documents to make them more attractive and fun? How about adding different artistic borders to different pages of your Word document? Or apply a single artistic border to all pages in the document at once? I know you’re as excited as we are about this whole artistic borders thing! Go straight to this article to learn how to successfully apply artistic borders to Word documents. Part 1: How to Apply the Same Artistic Page Border to All Pages in a Word Document Step 1: Open the Word document and click the "Design" tab in the top ribbon. Choose in DESIGN

How to print a Word document without comments How to print a Word document without comments Apr 18, 2023 pm 02:19 PM

With Microsoft Word, comments are important, especially when the document is shared among multiple people. Everyone can add something to the document content through his/her comments, and it is very important to retain these comments for future reference. But when you need to print a document, do you really need to print comments? In some cases, yes. But for some other cases, this is a big no! In this article, we explain through 2 different solutions how to easily print a Word document without printing the comments on it. Please remember that comments are only hidden and will not be deleted. Therefore, you absolutely do not risk any part of your document here by printing it without comment. Hope you like it! Solution 1: Pass

How to create a drop-down list with symbols in Excel How to create a drop-down list with symbols in Excel Apr 14, 2023 am 09:04 AM

Creating a drop-down list in an Excel worksheet is easy, as long as it's a regular drop-down menu. But what if you have to make it special by adding a special symbol, or make it even more special by adding some text as well as symbols? Ok, sounds interesting but wondering if this is possible? What's an answer you don't know when Geek Page is here to help? This article is all about creating dropdown menus with symbols as well as symbols and text. Hope you enjoyed reading this article! Also Read: How to Add Dropdown Menu in Microsoft Excel Part 1: Create a Dropdown List with Only Symbols To create a dropdown menu with symbols, we first need to create the source

5 Ways (and Fixes) to Adjust Your Screen for Monitoring on Windows 11 5 Ways (and Fixes) to Adjust Your Screen for Monitoring on Windows 11 Apr 14, 2023 pm 03:28 PM

Due to recent improvements around the world, PC parts are now available at MSRP, which has prompted many users to finally build their dream PC. Building a PC can have its challenges, and one of the tasks is fitting your screen to your monitor's display. If you can't fit a screen to your monitor on Windows 11, here's everything you need to know. let's start. How to adjust your screen for monitoring on Windows 11 in 5 ways To make your screen fit your monitor, you can adjust the resolution, scaling, or display output settings based on your current settings. We recommend that you try resizing the resolution to maintain visual quality and dpi. However, if this doesn't work for you, you can try

Implement the drop-down menu effect in WeChat applet Implement the drop-down menu effect in WeChat applet Nov 21, 2023 pm 03:03 PM

To implement the drop-down menu effect in WeChat Mini Programs, specific code examples are required. With the popularity of mobile Internet, WeChat Mini Programs have become an important part of Internet development, and more and more people have begun to pay attention to and use WeChat Mini Programs. The development of WeChat mini programs is simpler and faster than traditional APP development, but it also requires mastering certain development skills. In the development of WeChat mini programs, drop-down menus are a common UI component, achieving a better user experience. This article will introduce in detail how to implement the drop-down menu effect in the WeChat applet and provide practical

How to set image transparency in Google Slides? How to set image transparency in Google Slides? Apr 25, 2023 pm 06:52 PM

How to Change the Transparency of an Image in Google Slides Google Slides allows you to make small changes to your images. You can use the Format Options menu in Google Slides to change the transparency level of any image you insert. To make an image transparent using the Format Options menu in Google Slides: Open your existing Google Slides presentation or create a new one. Select an existing image on one of the slides in your presentation. Alternatively, add an image by pressing Insert > Image and selecting one of the upload options. After selecting the image, press the Format Options button in the toolbar. Alternatively, right-click the image and select Format options. Format Options menu

How to disable search on Windows 11 How to disable search on Windows 11 Apr 13, 2023 pm 05:28 PM

Why do I need to disable Windows Search? Believe it or not, there are some benefits to disabling search on Windows 11 besides getting rid of those pesky Bing results. These benefits include reclaiming computer resources and space on the hard drive, as well as reducing power consumption and ongoing background tasks. Additionally, disabling features can help those low-end PCs struggling with Windows 11. Windows 11 has very high system requirements, and by reducing the amount of work your computer has to handle, you'll make your machine run smoother and faster. It is not recommended that you disable any type of search functionality. Windows Search is the most efficient yet on Windows 11

See all articles