Home > Backend Development > PHP Tutorial > Yii2 framework dropDownList drop-down menu usage example analysis_php example

Yii2 framework dropDownList drop-down menu usage example analysis_php example

WBOY
Release: 2016-08-04 08:56:58
Original
1106 people have browsed it

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.

Copy the code The code is as follows:
field($model, 'name[]')->dropDownList(['a' => 'Item A' , 'b' => 'Item B', 'c' => 'Item C']); ?>

Add a drop-down menu for please select in yii2

Copy the code The code is as follows:
field($model, 'name[]')->dropDownList($listData, ['prompt'=>'Select ...']);>

DropDownList is used in the model

<&#63;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...']);
&#63;>

Copy after login

We use the prompt keyword to set the default value of the drop-down menu

Example:

Copy the code The code is as follows:
$form->field($searchmodel, 'moneytype')->dropDownList($soucetype, ['prompt' => 'Please select the amount source')])

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

Copy code The code is as follows:
$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, uses yii style by default)
1. In the controller method, we need to get 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,
    ]);
}

Copy after login

On the view page, we use yii’s form builder.

Copy the code 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 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

activeDropDownList method of Html class (advantage, 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 implementation method of the drop-down list activeDropDownList method



Copy code The code is as follows:Html::activeDropDownList($model, 'username', ArrayHelper::map($data,'id', 'customer_name'), ['style' => 'border :1px solid red;']);
I didn’t write the php tag. I believe programmers who have written Sina blogs know that if I write the php tag, the entire code will be filtered, so copy the code and add the tag yourself

The parameters have the same meaning as those of the first method and will not be explained.

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template