Yii2中的相关下拉列表在更新时丢失了值

时间:2018-05-31 20:11:54

标签: yii2

我创建了一个带有下拉列表的表单,该表单依赖于另一个(没有扩展名)并且运行速度很快。

但是当您尝试更改相同的表单时,第二个下拉列表的值将丢失(Preparing to unpack .../ebtables_2.0.10.4-3.5ubuntu2.18.04.1_amd64.deb ... invoke-rc.d: could not determine current runlevel * Error: insufficient privileges to access the ebtables rulesets. invoke-rc.d: initscript ebtables, action "stop" failed. dpkg: warning: old ebtables package pre-removal script subprocess returned error exit status 1 dpkg: trying script from the new package instead ... invoke-rc.d: could not determine current runlevel * Error: insufficient privileges to access the ebtables rulesets. invoke-rc.d: initscript ebtables, action "stop" failed. dpkg: error processing archive /var/cache/apt/archives/ebtables_2.0.10.4-3.5ubuntu2.18.04.1_amd64.deb (--unpack): new ebtables package pre-removal script subprocess returned error exit status 1 update-rc.d: warning: start and stop actions are no longer supported; falling back to defaults invoke-rc.d: could not determine current runlevel Errors were encountered while processing: /var/cache/apt/archives/ebtables_2.0.10.4-3.5ubuntu2.18.04.1_amd64.deb E: Sub-process /usr/bin/dpkg returned an error code (1))。基本上,类型字段取决于所选的区域。看我的代码:

_form.php这个

type_id

控制器

<?= $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']);
?>   

我认为这个topic也是同样的问题,但我不明白它是如何运作的。

1 个答案:

答案 0 :(得分:1)

您需要提供默认选项集,从中选择已保存的选项,而您不提供任何数据

<?php
$types = Type::find()
            ->where(['area_id' => $model->area_id])
            ->orderBy(['name' => SORT_ASC])
            ->all();

echo $form->field($model, 'type_id')->dropDownList(ArrayHelper::map($types,'id','name'),['prompt'=>'Selecione a Área']);
?>   

现在,当您使用findOne()并选择模型时,系统会自动选择type_id的已保存值。

您可以在controller/action中移动上述查询,并将$types传递给视图。

您也可以使用actionLists为方法count($types) D.R.Y删除第一个查询

public function actionLists($id)
{

    $types = Type::find()
            ->where(['area_id' => $id])
            ->orderBy(['name' => SORT_ASC])
            ->all();

    if(count($types) > 0 )
    {
        foreach($types as $type ){
            echo "<option value='".$type->id."'>".$type->name."</option>";
        }
    }
    else{
        echo "<option> - </option>";
    }

}
相关问题