问题
Yii2 active form
<?= $form->field($model, 'pid')->dropDownList([1=>1,2=>2])->hint('上级分类') ?>
I want to disable the option item 2=>2.
Is there a way to do it?
回答1:
You can add attributes for all items in the dropdownlist with the 'options' key. Let's say you want to disable the second item.
<?= $form->field($model, 'pid')->dropDownList([1 => 1, 2 => 2], ['options' => [2 => ['disabled' => true]]])->hint('上级分类') ?>
In the docs: http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#activeDropDownList()-detail
回答2:
This would work definitely:
<?= $form->field($model, 'pid')->dropDownList([1=>1,2=>2], ['options'=>['2'=>['disabled'=>true]]]) ?>
回答3:
ActiveField dropDownlist() explicitly calls BaseHtml activeDropDownList():
From the docs to ActiveField dropDownList():
The tag options in terms of name-value pairs.
For the list of available options please refer to the
$optionsparameter ofyii\helpers\Html::activeDropDownList().
And from the docs to BaseHtml activeDropDownList():
options: array, the attributes for the select option tags. The array keys must be valid option values, and the array values are the extra attributes for the corresponding option tags. For example,[ 'value1' => ['disabled' => true], 'value2' => ['label' => 'value 2'], ];
So pass these options:
[
2 => ['disabled' => true],
],
as second parameter to dropDownList().
回答4:
Try this:
$disableDataArr['1'] = ['disabled' => true];
dropDownList( $dataArr, ['options'=> $disableDataArr ])
来源:https://stackoverflow.com/questions/31720718/how-to-disable-one-item-in-yii2-activefrom-dropdownlist