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.
How to use the default dropdownlist in Yii2.0.
<?php echo $form->field($model, 'name[]')->dropDownList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C']); ?>
Add a selectable dropdown menu in yii2
<php echo $form->field($model, 'name[]')->dropDownList($listData, ['prompt'=>'Select...']);>
DropDownList Use
<?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...']); ?>
DropDownList in the model to set the default value of the dropdown menu. We use the prompt keyword
Example :
$form->field($searchmodel, 'moneytype')->dropDownList($soucetype, ['prompt' => '请选择金额来源')])
The default value setting of a good drop-down menu is as simple as this. Now we will talk about how to set the default value of the text box with a plug-in
I will now take the two at the back of this form that use the time plug-in. Take the text field as an example. The prompt keyword will not work here. We have to use the placeholder keyword
$form->field($searchmodel, 'startdate')->widget(DatePicker::className(),['clientOptions' => ['dateFormat' => 'yy-mm-dd']])->textInput(['placeholder' => Yii::t('app', 'Start time')])
The dropDownList method of the ActiveForm class (advantage, the default style of Yii is used)
1. In the controller method, we need to get The data must be the data of the findAll() or all() method. The example is as follows:
public function actionIndex() { $model = new UserModel(); $data = Customer::find()->all(); return $this->render('index', [ 'model' => $model, 'data' => $data, ]); }
In the view page, we use the yii form generator.
$form->field($model, 'username')->dropDownList(ArrayHelper::map($data,'id', 'customer_name'));
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. 1. $ data --- & gt; data source
2.3.2, id --- & gt; option's value value
2.3.3, Customer_name --- & gt; option label
Hhtml class ActiveDropdownList method (advantages , you can customize any style)
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 activeDropDownList method to implement the drop-down list
Html::activeDropDownList($model, 'username', ArrayHelper::map($data,'id', 'customer_name'), ['style' => 'border:1px solid red;']);
I did not write the php tag. I believe programmers who have written Sina blogs know that the entire code will be filtered if the php tag is written, so copy the code , add the label yourself
The parameters have the same meaning as the parameters of the first method and will not be explained.
I hope this article will help you design PHP programs based on the Yii framework.
For more related articles on Yii2 framework dropDownList drop-down menu usage example analysis, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software