Dropdownlist is a drop-down function in the Yii framework.We can directly use dropdownlist to implement the html select menu, let's take a look below.

The use of yii2.0's default dropdownlist.

<?php echo $form->field ($model, "name []")->dropdownlist (["a" =>"item a", "b" =>"item b "," c "=>" item c "]);?>

Add a drop-down menu for selection in yii2

<php echo $form->field ($model, "name []")->dropdownlist ($listdata, ["prompt" =>"select ..."]);>

dropdownlist is used in the model

//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 setting of the drop-down menu we use the prompt keyword


$form->field ($searchmodel, "moneytype")->dropdownlist ($soucetype, ["prompt" =>"Please select the amount source")])

Ok, the default setting of the drop-down menu is as simple as that,Let's talk about how the default value of the text box with the plugin is set

I will now take the two text fields that use the time plug-in as an example.The prompt keyword doesn't work here,We are going to use the placeholder keyword

$form->field ($searchmodel, "startdate")->widget (datepicker ::classname (), ["clientoptions" =>["dateformat" =>"yy-mm-dd"]]) ->Textinput (["placeholder" =>yii ::t ("app", "start time")])

dropdownlist method of the activeform class (advantages, use Yii's style by default)

1. In the controller method, we need to get the data,Must be the data of the findall () or all () method,Examples are 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 Yii's form generator.

$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.1, $data --->data source

2.3.2, id --->value of option

2.3.3, customer_name --->value of option tag

html class's activedropdownlist method (advantages, you can customize any style)

1. Same as the first step of the first method,Get the data.But more explanation.

2.The \ yii \ helpers \ html class provides us with an implementation method of the dropdown list activedropdownlist method

html ::activedropdownlist ($model, "username", arrayhelper ::map ($data, "id", "customer_name"), ["style" =>"border:1px solid red;"]);

I did n’t write the php tag. I believe that the programmers who wrote the Sina blog know thatWrote php tags, the entire code was filtered,So copy the code,Tag yourself

The parameters have the same meaning as the parameters of the first method.No explanation.

  • Previous VS2015 Update2 build Android program summary
  • Next Yii2 uses dropdownlist to implement regional three-level linkage function