Yii2 framework dropDownList drop-down menu usage instance Analysis _ php instance

Source: Internet
Author: User
This article mainly introduces the usage of the dropDownList drop-down menu in the Yii2 framework, and analyzes the definition, implementation methods, and usage skills of the dropDownList drop-down menu in the Yii Framework in combination with examples, for more information about the usage of the dropDownList drop-down menu in the Yii2 framework, see the following example. 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.

The 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

The code is as follows:

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:

The 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.

The 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.

The 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.

The 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 that has written the php tag is filtered out. Therefore, I add 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.