Drop down menu Yii2下拉列表选择默认选项

Drop down menu Yii2下拉列表选择默认选项,drop-down-menu,yii2,yii2-advanced-app,Drop Down Menu,Yii2,Yii2 Advanced App,我通过GET in url返回cat_id值,表示我的下拉列表,必须选择哪个项目。 但它不起作用 <?= $form->field($model, 'cat_id')->dropDownList( ArrayHelper::map(DeviceCats::find() ->where(['is_deleted' => 'no'])->all(),'id','title') ,['options' => [$_GET['cat_id'] => ['s

我通过GET in url返回cat_id值,表示我的下拉列表,必须选择哪个项目。 但它不起作用

<?= $form->field($model, 'cat_id')->dropDownList(
ArrayHelper::map(DeviceCats::find()
->where(['is_deleted' => 'no'])->all(),'id','title')
,['options' => [$_GET['cat_id'] => ['selected'=>true]]
, 'prompt' => ' -- Select Category --']) ?>

只需确保您的模型设置了属性cat\u id即可。在你控制器的某个地方做一个

$model->cat_id = filter_input_array(INPUT_GET, 'cat_id');

如果你真的想这样做的话,可能你也必须使用模型的名字

    <?= $form->field($model, 'cat_id')->dropDownList(ArrayHelper::map(DeviceCats::find()->where(['is_deleted' => 'no'])->all(),'id','title'),['options' => [$_GET['SOMETHIGNHERE']['cat_id'] => ['selected'=>true]], 'prompt' => ' -- Select Category --']) ?>

最终解决了一个令人难以置信的变化。刚刚将选定的第一个字母更改为大写(“选定的”应为“选定的”)。 代码如下:

<?= $form->field($model, 'cat_id')->dropDownList(
ArrayHelper::map(DeviceCats::find()
->where(['is_deleted' => 'no'])->all(),'id','title')
,['options' => [$_GET['cat_id'] => ['Selected'=>'selected']]
, 'prompt' => ' -- Select Category --']) ?>

“选定”必须用大写字母“S”书写:

'options'=>['72'=>['Selected'=>true]]

谢谢你,米海。事实上,我确信数组中存在的值。此外,我还使用了var_dump($_GET),它位于级别1。这就是我的观点。。。不应该这样。你是在通过跳圈来让它工作,而且做得也不对。相反,您应该在控制器中移动代码。除此之外,您直接使用的是PHP全局变量,这是一个否定号。请参阅为什么地狱选择必须用大写字母S书写?这一定是个bug什么的。无论如何,谢谢!
'options'=>['72'=>['Selected'=>true]]