The examples in this paper describe the simple usage of DropDownList in YII2. Share to everyone for your reference, as follows:
Here, the practical application is used as an example to explain Yii2 DropDownList usage.
There is a classification table, infinite pole classification of the kind, table structure as follows, PID is the parent category ID
Here we want to achieve:
When creating a new category, the parent category can be selected from any of the categories or not selected
When editing a category, the parent category cannot select the currently edited category ... If you choose yourself, the parent category is your own, must be wrong!
The implementation code is as follows, I'll put all the code of the form
<?phpuse common\models\category;use yii\helpers\arrayhelper;use yii\helpers\html;use yii\widgets\ActiveForm;/* @ var $this yii\web\view *//* @var $model common\models\category *//* @var $form yii\widgets\activeform */?> <?ph P if (! $model->isnewrecord) {//If it is an edit category $cate = Arrayhelper::map (Category::find ()->andwhere (' id! =: id ', [': I d ' = $model->id])->all (), ' id ', ' title '); } else {//If it is a new classification $cate = Arrayhelper::map (Category::find ()->all (), ' id ', ' title '); }?> <?php $form = Activeform::begin ()?> <?= $form->field ($model, ' title ')->textinput ([' maxlength ' = +])->label ("category title")?> <?= $form->field ($model, ' name ')->textinput ([' maxlength ' = >])->label ("classification alias")?> <?= $form->field ($model, ' pid ')->dropdownlist ($cate, [' prompt ' = ' = ' Please select Parent category '])->label ("parent category")?> <?= $form->field ($model, ' keywords ')->textarea ([' maxlength ' = = 255])->l Abel("categorical keywords")?> <?= $form->field ($model, ' description ')->textarea ([' maxlength ' = = 255])->label ("Category description" )?> <?= Html::submitbutton ($model->isnewrecord? ' Create ': ' Update ', [' class ' = $model->isnewrecord? ' Btn btn-block btn-success ': ' btn btn-block btn-primary ']?> <?php activeform::end (); ?>
For more information on YII related content readers can view this site topic: "YII framework Introduction and common skills Summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Basic Tutorial", "PHP Object-oriented Programming tutorial", "PHP string (String) Usage Summary "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "
It is hoped that this article is helpful to the PHP program design based on YII framework.