Home > php教程 > PHP开发 > Yii2 framework dropDownList drop-down menu usage example analysis

Yii2 framework dropDownList drop-down menu usage example analysis

高洛峰
Release: 2016-12-23 17:51:48
Original
1748 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.

How to use the default dropdownlist in Yii2.0.

<?php echo $form->field($model, &#39;name[]&#39;)->dropDownList([&#39;a&#39; => &#39;Item A&#39;, &#39;b&#39; => &#39;Item B&#39;, &#39;c&#39; => &#39;Item C&#39;]); ?>
Copy after login

Add a selectable dropdown menu in yii2

<php echo $form->field($model, &#39;name[]&#39;)->dropDownList($listData, [&#39;prompt&#39;=>&#39;Select...&#39;]);>
Copy after login

DropDownList Use

<?php
//use app\models\Country;
$countries=Country::find()->all();
//use yii\helpers\ArrayHelper;
$listData=ArrayHelper::map($countries,&#39;code&#39;,&#39;name&#39;);
echo $form->field($model, &#39;name&#39;)->dropDownList(
                $listData,
                [&#39;prompt&#39;=>&#39;Select...&#39;]);
?>
Copy after login

DropDownList in the model to set the default value of the dropdown menu. We use the prompt keyword

Example :

$form->field($searchmodel, &#39;moneytype&#39;)->dropDownList($soucetype, [&#39;prompt&#39; => &#39;请选择金额来源&#39;)])
Copy after login

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, &#39;startdate&#39;)->widget(DatePicker::className(),[&#39;clientOptions&#39; => [&#39;dateFormat&#39; => &#39;yy-mm-dd&#39;]])->textInput([&#39;placeholder&#39; => Yii::t(&#39;app&#39;, &#39;Start time&#39;)])
Copy after login

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(&#39;index&#39;, [
      &#39;model&#39; => $model,
      &#39;data&#39; => $data,
    ]);
}
Copy after login

In the view page, we use the yii form generator.

$form->field($model, &#39;username&#39;)->dropDownList(ArrayHelper::map($data,&#39;id&#39;, &#39;customer_name&#39;));
Copy after login

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, &#39;username&#39;, ArrayHelper::map($data,&#39;id&#39;, &#39;customer_name&#39;), [&#39;style&#39; => &#39;border:1px solid red;&#39;]);
Copy after login

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!

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template