Home  >  Article  >  Backend Development  >  yii2 drop-down box comes with search

yii2 drop-down box comes with search

不言
不言Original
2018-04-27 14:44:271480browse

The content introduced in this article is about the built-in search of the yii2 drop-down box, which has a certain reference value. Now I share it with you. Friends in need can refer to it.

First make sure whether composer is installed. Composer is for It is an indispensable part for PHP programmers. Then hold down the shift key and right-click the mouse in the project directory and click "Open command window here"; the execution of

composer require kartik-v/yii2-widget-select2 "@dev"。

takes a bit long, please wait patiently.

After installation, if it is an AF page

use kartik\select2\Select2; 
 <?= $form->field($model, &#39;charger_id&#39;)->widget(Select2::classname(), [  
                                            &#39;data&#39; => \yii\helpers\ArrayHelper::map(\common\models\Category::find()->asArray()->all(),&#39;id&#39;,&#39;name&#39;),
                                            &#39;options&#39; => [&#39;placeholder&#39; => &#39;请选择 ...&#39;], 
                                        ]); 
 ?>

If it is another page

use kartik\select2\Select2; 
echo Select2::widget([ &#39;name&#39; => &#39;title&#39;, 
    &#39;data&#39; => \yii\helpers\ArrayHelper::map(\common\models\Category::find()->asArray()->all(),&#39;id&#39;,&#39;name&#39;), 
    &#39;options&#39; => [&#39;placeholder&#39; => &#39;请选择...&#39;] 
]);

Of course, the data is requested when the page is loaded model layer; data can also be loaded asynchronously. Try it yourself!

Related recommendations:

yii2 resetful authorization verification


The above is the detailed content of yii2 drop-down box comes with search. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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