Yii2 with the search function of the Drop-down box example detailed _php example

Source: Internet
Author: User

Simple little function, but it's pretty cool to use. Share out so that more people have faster development efficiency, happy heart programming.

If you have not used composer, you can out, to see my tutorials to share, composer is absolutely necessary magical wood has. All said to this point, we hurriedly use composer to install it.

First to show you the effect of the picture:

Below gives everybody to say the concrete realization thought:

Composer require kartik-v/yii2-widget-select2 "@dev"
#特别说明, because the dev version installed here, the development version, the unstable version, if your project is git hosted, Composer installed down here remember to delete the \vendor\kartik-v\yii2-widget-select2 directory under the. git file, or you can't submit it.

Just wait for him for about 5 minutes, and the installation will be fine, and then we can start using it like the following.

If your form is activeform, please use

Use Kartik\select2\select2; 
$data is an array of key values Oh, key-value, all of the $data declared below are key-value pairs, taking the array as an example 
$data = [2 => ' widget ', 3 => ' DropDownList ', 4 => ' Yii 2 ']; 
echo $form->field ($model, ' title ')->widget (Select2::classname (), [ 
' Data ' => $data, 
' options ' => [' placeholder ' => ' please choose ... '] 
;

If your form is not activeform, you can refer to the following

Use Kartik\select2\select2; 
Echo select2::widget ([' Name ' => ' title ', ' 
data ' => $data, 
' options ' => [' placeholder ' => '] Please choose ... ' ] 
]);

#非ActiveFomr生成的更新数据的时候就需要默认选中, good to do, add value can be

Use Kartik\select2\select2; 
Echo select2::widget ([ 
' name ' => ' title ', ' 
value ' => 2, 
' data ' => $data, 
' Options ' => [ ' Placeholder ' => ' please choose ... '] 
);

#但是如果你的表单是ActiveForm生成的, but often the field is not a table segment? Better, take the above example, you only need to specify $model->title = [' Title1 ', ' title2 '];

The above content is small series to introduce the YII2 with the search function of the Drop-down box examples, I hope to help you, but also very grateful to the cloud Habitat Community support site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.