Home >Backend Development >PHP Tutorial >Yii2 framework dropDownList drop-down menu usage example analysis_php example
The example in this article describes the usage of dropDownList drop-down menu in Yii2 framework. Share it with everyone for your reference, the details are as follows:
dropDownList is a built-in drop-down function in the Yii framework. We can directly use dropDownList to implement the HTML select menu. Let’s take a look.
Yii2.0 default dropdownlist usage.
Add a drop-down menu for please select in yii2
DropDownList is used in the model
<?php //use app\models\Country; $countries=Country::find()->all(); //use yii\helpers\ArrayHelper; $listData=ArrayHelper::map($countries,'code','name'); echo $form->field($model, 'name')->dropDownList( $listData, ['prompt'=>'Select...']); ?>
We use the prompt keyword to set the default value of the drop-down menu
Example:
Okay, setting the default value of the drop-down menu is as simple as this. Now let’s talk about how to set the default value of the text box with a plug-in
I will take the two text fields at the back of this form that use time plug-ins as an example. The prompt keyword will not work here. We have to use the placeholder keyword
public function actionIndex() { $model = new UserModel(); $data = Customer::find()->all(); return $this->render('index', [ 'model' => $model, 'data' => $data, ]); }
On the view page, we use yii’s form builder.
2.1, dropDownList ---> yii2.0 drop-down list method
2.2. ArrayHelper::map() ---> Construct a one-dimensional or multi-dimensional array of (key => value)
"
2.3.2, id ’ ’ ’ ’ s through ’ ’s ’ out’s out's out’s out’s out’s out’s out’s out out out out out out out out out out out out out out out out out off UN s t s - i s s , id ,
2.3.3. customer_name ---> The value of the option tag
1. Same as the first step of the first method, get the data. But that’s too much explanation.
2. The yiihelpersHtml class provides us with the implementation method of the drop-down list activeDropDownList method
Readers who are interested in more Yii-related content can check out the special topics on this site: "Introduction to Yii Framework and Summary of Common Techniques", "Summary of Excellent PHP Development Framework", "Basic Tutorial for Getting Started with Smarty Templates", "Introduction to PHP Object-Oriented Programming" Tutorial", "php string usage summary", "php+mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.