Yii2 frame DropDownList Drop-down Menu Usage Example Analysis _php instance

Source: Internet
Author: User
Tags smarty template yii

This example describes the use of the YII2 framework DropDownList drop-down menu. Share to everyone for your reference, specific as follows:

DropDownList is a self-contained drop-down feature in the YII framework, and we can use the DropDownList to implement the HTML select menu, and take a look here.

Yii2.0 the default DropDownList method of use.

Copy Code code as follows:
<?php echo $form->field ($model, ' name[] ')->dropdownlist ([' A ' => ' Item A ', ' B ' => ' item B ', ' C ' => ' item C ']);?>

Add the Drop-down menu you selected in the Yii2

Copy Code code as follows:
<php echo $form->field ($model, ' name[] ')->dropdownlist ($listData, [' Prompt ' => ' Select ... ']);>

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

Drop-down Menu Defaults Setting we use the Prompt keyword

Instance:

Copy Code code as follows:
$form->field ($searchmodel, ' Moneytype ')->dropdownlist ($soucetype, [' Prompt ' => ' Please select the source of Money ')])

The default value setting for a good drop-down menu is as simple as this, so let's talk about how the default value for a text box with a plugin is set.

I'm going to take this form now. Two text fields after the time plugin are used, for example, where the prompt keyword is not available, we want to use the PLACEHOLDER keyword

Copy Code code as follows:
$form->field ($searchmodel, ' StartDate ')->widget (Datepicker::classname (), [' Clientoptions ' => '] DateFormat ' => ' yy-mm-dd ']]->textinput ([' Placeholder ' => yii::t (' app ', ' Start time ')]

DropDownList method for ActiveForm class (advantages, default use of Yii style)
1, in the controller's method, we need to get the data, must be findall () or the All () method of data, examples are 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 table birth builder for Yii.

Copy Code code 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 ()---> Building a one-dimensional or multidimensional array (key => value)
2.3.1, $data---> Data sources
Value values for 2.3.2, ID---> option
Values for 2.3.3, customer_name---> option labels

Activedropdownlist method for Html classes (benefits, you can customize any style)

1. Like the first step of the first method, get the data. But it's a lot to explain.
2, \yii\helpers\html class for us to provide a drop-down list of implementation methods Activedropdownlist method

Copy Code code as follows:
Html::activedropdownlist ($model, ' username ', Arrayhelper::map ($data, ' id ', ' customer_name '), [' style ' => ' border : 1px solid red; ']);

I did not write PHP tags, I believe that wrote the program of Sina Blog Ape know, wrote the PHP tag the entire code is filtered, so copy the code, their own tag

Parameters have the same meaning as the first method, without explanation.

For more information on YII-related content, readers who are interested in this site can view the topics: Introduction to YII Framework and summary of common skills, "Summary of PHP Excellent development framework", "Smarty Template Introductory Course", "Introduction to PHP object-oriented programming", "PHP string" Summary of Usage , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the YII framework.

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.