编程学习网 > PHP技术 > Yii2 > yii2 视图中 dropDownList 直接显示数据库内容
2020
01-11

yii2 视图中 dropDownList 直接显示数据库内容

yii2 视图中 dropDownList 直接显示数据库内容

// 不操作数据库,直接填充数据显示


<? echo $form->field($model, 'edu')->dropDownList(['1'=>'大学','2'=>'高中','3'=>'初中'], ['prompt'=>'请选择','style'=>'width:120px']) ?>


// 下拉框读取数据库操作


<?= $form->field($user, 'communityid')->dropDownList(ArrayHelper::map(Community::find()->all(), 'id', 'name')) ?>


// 读取数据库,在前面加一个提示性的文字


<?= $form->field($exchange, 'target_communityid')->dropDownList(
    Community::find()->select(['name', 'id'])->indexBy('id')->column(), ['prompt' => '请选择目标小区', 'value' => $user->communityid]
) ?>
或者用下面的方式



<?php
    $arr = ArrayHelper::map(Community::find()->all(), 'id', 'name');
    $arr = array_merge(['请选择小区'], $arr);
    echo $form->field($user, 'communityid')->dropDownList($arr);
?> 


扫码二维码 获取免费视频学习资料

Python编程学习

查 看2022高级编程视频教程免费获取