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!