yii2學習筆記 ---- Yii2上下聯動下拉框功能

在模型上添加代碼

public $grade;
public $userGrade=[
        '1'=>'等級1',
        '2'=>'等級2',
        '3'=>'等級3',
        '4'=>'等級4',
        '5'=>'等級5',
        '6'=>'等級6',
        '7'=>'等級7',
    ];
public function getUserGrade(){
        $arr=[];
        foreach ($this->userGrade as $key=>$value){
            $arr[]=['id'=>$key,'name'=>$value];
        }
        return \yii\helpers\ArrayHelper::map($arr,'id','name');
    }

控制器添加代碼

public function actionCreate()
$model = new Model();
$userGrade = $model->getUserGrade();
return $this->render('create', [
            'userGrade' => $userGrade,
            'model' => $model
        ]);

視圖

<?php echo $form->field($model, 'grade')->dropDownList($userGrade); ?>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章