0
我已经使用来自Kartik的扩展,用于创建依赖下拉菜单,称为DepDrop。Yii2 - 如何在操作中显示选定的值更新?
我的依赖模式Regencies->区 - >村
当创建行动,一切都还好吧,运行水井,但是当更新行动,在儿童下拉不显示选定值。
这里我查看代码:
<?= $form->field($model, 'fk_regencies_id')->dropDownList(ArrayHelper::map(Regencies::find()->all(),'id','name'), ['id'=>'regency_id']);?>
<?= Html::hiddenInput($model->fk_districs_id, $model->fk_districs_id, ['id'=>$model->fk_districs_id]) ?>
<?= $form->field($model, 'fk_districs_id')->widget(DepDrop::classname(), [
'options'=>['id'=>'district-id'],
'pluginOptions'=>[
'depends'=>['regency_id'],
'placeholder'=>'Select...',
'url'=>Url::to(['/calonpegawai/district']),
'params'=>[$model->fk_districs_id]
]
]) ?>
,在这里我的控制器:
public function actionDistrict() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$regency_id = $parents[0];
$param1 = null;
if (!empty($_POST['depdrop_params'])) {
$params = $_POST['depdrop_params'];
$param1 = $params[0]; // get the value of input-type-1
}
$out = Districts::getDistrictList($regency_id);
//$out[1] = ['id'=>$regency_id, 'name'=>$param1];
$selected = Districts::getDefaultDistrict($param1);
//$selected[1] = ['id'=>$regency_id, 'name'=>$param1];
// the getDefaultSubCat function will query the database
// and return the default sub cat for the cat_id
echo Json::encode(['output'=>$out, 'selected'=>$selected]);
return;
}
}
echo Json::encode(['output'=>'', 'selected'=>'']);
}
我的模型:
public function getDistrictList($regency_id)
{
$data=\backend\models\Districts::find()
->where(['regency_id'=>$regency_id])
->select(['id','name' ])->asArray()->all();
return $data;
}
public function getDefaultDistrict($param1)
{
$data=\backend\models\Districts::find()
->where(['id'=>$param1])
->select(['id','name' ])->asArray()->all();
return $data;
}
你可以发布检查员的浏览器请求日志吗? – meysam
@meysam我已添加图片,请回复ajax –
这是Yii2调试面板,请发送浏览器控制台的日志。在Firefox中按Ctrl + Shift + K,在Chrome中按F12显示控制台。 – meysam