您的位置:首页 > 编程语言 > PHP开发

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

2020-01-11 00:00 253 查看
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);
?>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  857530