你好,我正在尝试制作一个下拉列表,并设置默认选项,但是进展缓慢。下拉列表已经呈现出来了,但总是选择第一个选项。
$company_id = (int) $params['company_id'];
$options = [
'options' => [
$company_id => [
'selected' => 'selected',
'label' => 'test'
]
]
];
echo $form->field($model, 'company_id')->dropDownList($companies_list, $options);
这段代码有什么问题吗?我编辑了我的代码并在选项中设置了'label' => 'test',这个方法有效,但是仍然无法选中。
好的,解决方案已找到,在框架代码中我发现了renderSelectionOptions方法:
$attrs = isset($options[$key]) ? $options[$key] : [];
$attrs['value'] = (string) $key;
$attrs['selected'] = $selection !== null &&
(!is_array($selection) && !strcmp($key, $selection)
|| is_array($selection) && in_array($key, $selection));
所以我需要做的就是:
$model->company_id = $company_id;
在渲染段落之前