Yii2 active form
<?= $form->field($model, 'pid')->dropDownList([1=>1,2=>2])->hint('上级分类') ?>
我想禁用选项2=>2。
有什么方法可以做到吗?
Yii2 active form
<?= $form->field($model, 'pid')->dropDownList([1=>1,2=>2])->hint('上级分类') ?>
我想禁用选项2=>2。
有什么方法可以做到吗?
<?= $form->field($model, 'pid')->dropDownList([1 => 1, 2 => 2], ['options' => [2 => ['disabled' => true]]])->hint('上级分类') ?>
ActiveField 的 dropDownlist() 明确调用了 BaseHtml 的 activeDropDownList():
从 ActiveField
dropDownList()
的文档中可以看到:
标签选项以名称-值对的形式表示。
有关可用选项的列表,请参阅
yii\helpers\Html::activeDropDownList()
中的$options
参数。
从 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'], ];
所以传递这些选项:
[
2 => ['disabled' => true],
],
dropDownList()
的第二个参数。<?= $form->field($model, 'pid')->dropDownList([1=>1,2=>2], ['options'=>['2'=>['disabled'=>true]]]) ?>
试试这个:
$disableDataArr['1'] = ['disabled' => true];
dropDownList( $dataArr, ['options'=> $disableDataArr ])