Yii2 framework dropDownList drop-down menu usage instance analysis, yii2dropdownlist

Source: Internet
Author: User

Yii2 framework dropDownList drop-down menu usage instance analysis, yii2dropdownlist

This document describes the usage of the dropDownList drop-down menu in the Yii2 framework. We will share this with you 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 select menu of html. Let's take a look at it.

Yii2.0 default dropdownlist usage method.
Copy codeThe Code is as follows: <? Php echo $ form-> field ($ model, 'name [] ')-> dropDownList (['A' => 'item ', 'B' => 'item B ', 'c' => 'item C']);?>

Add the selected drop-down menu to yii2
Copy codeThe Code is as follows: <php echo $ form-> field ($ model, 'name [] ')-> dropDownList ($ listData, ['propt' => 'select... ']);>

DropDownList used in 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...']);?>

The default value of the drop-down menu sets the use of the prompt keyword.

Instance:
Copy codeThe Code is as follows: $ form-> field ($ searchmodel, 'moneytype')-> dropDownList ($ soucetype, ['propt' => 'select the amount source')])

It's easy to set the default value of the drop-down menu. Let's talk about how to set the default value of the text box with plug-ins.

Now I will take the two text fields behind this form that use the time plug-in as an example. Here, the prompt keyword will not work. We will use the placeholder keyword.
Copy codeThe Code is as follows: $ form-> field ($ searchmodel, 'startdate')-> widget (DatePicker: className (), ['clientopexception' => ['dateformat' => 'yy-mm-dd'])-> textInput (['placeholder '=> Yii :: t ('app', 'start Time')])
The dropDownList method of the ActiveForm class (advantage: the yii style is used by default)
1. In the Controller method, we need to obtain the data, which 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,    ]);}

On the view page, we use the form builder of yii.
Copy codeThe Code is as follows: $ 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 (key => value)
2.3.1, $ data ---> data Source
2.3.2. id ---> option value
2.3.3. customer_name ---> option label Value

ActiveDropDownList method of the Html class (advantage: Any style can be customized)

1. Get data like the first step in the first method. But I explained it more.
2. The \ yii \ helpers \ Html class provides the activeDropDownList implementation method.
Copy codeThe Code is as follows: Html: activeDropDownList ($ model, 'username', ArrayHelper: map ($ data, 'id', 'customer _ name '), ['style' => 'border: 1px solid red; ']);
I did not write the php tag. I believe all programmers who have written Sina blogs know that the entire code written with the php tag is filtered out, so I copied the code and added the tag myself.

Parameters have the same meaning as the parameters in the first method and are not explained.

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.