how to disable one item in yii2 ActiveFrom dropDownList?

巧了我就是萌 提交于 2019-12-12 11:32:23

问题


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 $options parameter of yii\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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!