我想为每个option
项生成一个带有附加属性的下拉列表,如下所示:
<select>
<option value="1" data-height="45">item 1</option>
<option value="2" data-height="70">item 2</option>
<option value="3" data-height="10">item 3</option>
</select>
是否有正确的方法来生成它作为这个非正确的例子:
数据库模型,已重试的行:
[
{id: 1, name: "item 1", height: 45},
{id: 2, name: "item 2", height: 70},
{id: 3, name: "item 3", height: 10},
]
PHP:
<?php
$form->field($model, $attribute)->dropDownList($items_array, $options, function($item) {
return "<option value='{$item->id}' data-height='{$item->height}'>{$item->name}</option>";
})
?>
答案 0 :(得分:0)
<?php
// Get Array from Database
$items_array = ArrayHelper::map(ModelName::find()->asArray()->all(),'id','name');
?>
<?= $form->field($model, 'designation')->dropDownList($items_array,['prompt' => 'Select Any One', 'data-height="45",'class' => 'form-control']); ?>