这里我想将动态页面数据保存到数据库中。供应商控制器我根据依赖关系显示来自两个不同控制器的列表数据并保存到第3个表中,供应商创建了带有动态表单的id包含ie在选择主类别之后,我们的子类别将是动态的更改。但是在单击“创建”按钮后,它将返回创建表单,我认为其他条件已执行。但我无法理解如何以及为什么。请帮助我,我被困在最后2个。我正在按照以下链接获取此动态表格在此先感谢。 我附上了页面以便更多理解 https://github.com/wbraganca/yii2-dynamicform
$model = new Vendors();//main controller In this controller i have save data dynamically
if($model->load(Yii::$app->request->post())) {
$modelsVendorsMoreCategories = Model::createMultiple(VendorsMoreCategories::classname());
Model::loadMultiple($modelsVendorsMoreCategories, Yii::$app->request->post());
$valid = $modelsBusinessMainCategories->validate();
$valid = Model::validateMultiple($modelsBusinessSubCategories) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
if ($flag = $model->save(false)) {
foreach ($modelsVendorsMoreCategories as $modelVendorsMoreCategories) {
$modelVendorsMoreCategories->vmc_ven_id = $model->ven_id;
if (!($flag = $modelVendorsMoreCategories->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['view', 'id' => $model->ven_id]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}else {
return $this->render('create', [
'model' => $model,
'modelsBusinessSubCategories' => (empty($modelsBusinessSubCategories)) ? [new BusinessSubCategories] : $modelsBusinessSubCategories
]);
}
现在它在供应商控制器中正常工作我已经为一个id做了动态注册。根据动态表单选择创建供应商ID。动态记录创建到" vendor-more-ategory "表与供应商表供应商ID和主要类别的动态选择&它的子类别
<div class="row">
<div class="col-sm-6">
<?= $form->field($modelVendorsMoreCategories, "[{$i}]vmc_bmc_id")->dropDownList( ArrayHelper::map(BusinessMainCategories::find()->all(), 'bmc_id', 'bmc_name'),
['prompt' => 'Select Main Category','onchange'=>' $.post("index.php?r=business-sub-categories/lists&id=' . '"+$(this).val(),function(data){ $("select#vendorsmorecategories-'.$i.'-vmc_bsc_id").html(data);
});']);//Here I want to increment id but it still 0 for all selection
?>
</div>
<div class="col-sm-6">
<?= $form->field($modelVendorsMoreCategories, "[{$i}]vmc_bsc_id")->dropDownList(
ArrayHelper::map(BusinessSubCategories::find()->all(), 'bsc_id', 'bsc_name'),
[
'prompt' => 'Select Sub Category'
]);?>
</div>
答案 0 :(得分:0)
<div class="row">
<div class="col-sm-6">
<?=
$this->registerJs('
window.hideButton=(function(e){
$(e).hide();
})
$(document).ready(function(){
$(".add-item").click(function(){
$(this).hide();
});
});', \yii\web\View::POS_READY);
echo $form->field($modelVendorsMoreCategories, "[{$i}]bmc_id")->dropDownList(
ArrayHelper::map(BusinessMainCategories::find()->where(['deleted' => 'N'])
->andwhere(['<>','bmc_name',strtolower(trim('Live Event'))])->all(), 'bmc_id', 'bmc_name'),
['prompt' => 'Select Main Category',
'onchange' => 'var valint=this.id;
var finVal=valint.replace("bmc_id", "bsc_id");
$.post("index.php?r=business-sub-categories/lists&id=' . '"+$(this).val(),function(data){
$("select#"+finVal).html(data);
});']);
?>
</div>
<div class="col-sm-6" id="c">
<?= $form->field($modelVendorsMoreCategories, "[{$i}]bsc_id")->dropDownList( ArrayHelper::map(BusinessSubCategories::find()->all(), 'bsc_id', 'bsc_name'),
'prompt' => 'Select Sub Category'
]);
?>
</div>
</div><!-- .row -->