我创建了一个表单,它的下拉列表依赖于另一个表单(没有扩展),它运行得很快。
但是,当您尝试更改相同的表单时,第二个下拉列表的值将丢失(type_id
)。基本上,类型字段取决于所选的区域。请看我的代码:
_form.php
<?= $form->field($model, 'area_id')->dropDownList(ArrayHelper::map(Department::find()->where(['helpdesk' => 1])->all(),'id','name'),['prompt'=>'Selecione a Área',
'onchange' => '
$.post("index.php?r=helpdesk/solicitation/lists&id=' . '"+$(this).val(),function(data){
$("select#solicitation-type_id").html(data);
});']) ?>
<?php
echo $form->field($model, 'type_id')->dropDownList(['prompt'=>'Selecione a Área']);
?>
控制器
public function actionLists($id)
{
$countType = Type::find()
->where(['area_id' => $id])
->count();
$types = Type::find()
->where(['area_id' => $id])
->orderBy(['name' => SORT_ASC])
->all();
if($countType > 0 )
{
foreach($types as $type ){
echo "<option value='".$type->id."'>".$type->name."</option>";
}
}
else{
echo "<option> - </option>";
}
}
我认为这个topic也有同样的问题,但我不明白它是如何工作的。
https://stackoverflow.com/questions/50632059
复制相似问题