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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
