Yii2-具有单个模型的动态表单

时间:2018-07-17 11:05:31

标签: yii2

我一直使用保存在多个模型中的动态表单,尤其是wbraganca。但是,我想创建一个动态表单,将所有内容保存在同一模型类CentreProgramme中。单击添加按钮后,将创建具有Programme_id属性的下拉列表。 我该如何实现。 我使用了下面显示的代码,但是下拉列表的宽度不足以容纳数据。当我从下拉列表中选择全部时,由于数据太大,它会分散在整个下拉列表中。谢谢

我有这个模型课

中心计划

    public function actionCreate()
    {
    $model = new StudyCentreProgramme();

    if ($model->load(Yii::$app->request->post()) && isset($_POST['StudyCentreProgramme'])) {

    $model->attributes = $_POST['StudyCentreProgramme'];
    for($i=0;$i<count($_REQUEST['StudyCentreProgramme']['programme_id']);$i++) :
        $model->id = NULL;
        $model->isNewRecord = true;
        $model->programme_id = $_POST['StudyCentreProgramme']['programme_id'][$i];
        $model->state_office_id = $_POST['StudyCentreProgramme']['state_office_id'];
        $model->study_centre_id = $_POST['StudyCentreProgramme']['study_centre_id'];

        if($model->save()) {
              Yii::$app->session->setFlash('green-'.$i, '<i class="fa fa-info-circle"></i> <b>Study Centre Programme: </b>  is created successfully');
        } else {
              Yii::$app->session->setFlash('red-'.$i, '<i class="fa fa-warning"></i><b>Study Centre Programme: </b> has already been taken.');
        }               
    endfor; 
        return $this->redirect(['index']);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
    }

控制器

    <?php $form = ActiveForm::begin([
        'id' => 'study-centre-programme-form',
        'fieldConfig' => [
            'template' => "{label}{input}{error}",
        ],
]); ?>
<div class="col-xs-12 col-sm-6 col-lg-6">
                        <?= $form->field($model, 'state_office_id')->dropDownList(ArrayHelper::map(\common\models\StateOffice::find()->where(['is_status' => 0])->all(),'id','state_name'),
                        [
                                'prompt'=>Yii::t('app', '--- Select State Officee ---'),
                                'onchange'=>'
                                    $.get( "'.Url::toRoute('dependent/getprogrammestudycentre').'", { id: $(this).val() } )
                                        .done(function( data ) {
                                            $( "#'.Html::getInputId($model, 'state_office_id').'" ).html( data );
                                        }
                                    );'    
                            ]       
                        );   ?> 
</div>

<div class="col-xs-12 col-sm-6 col-lg-6">
                        <?= $form->field($model, 'state_office_id')->dropDownList([''=>Yii::t('app', '--- Select Study Centre ---')]); ?>
</div>

<div class="col-xs-12 col-sm-12 col-lg-12">
<?php
if(Yii::$app->controller->action->id === 'create' ) {
        echo $form->field($model, 'programme_id[]')->dropDownList(ArrayHelper::map(common\models\Programme::find()->where(['is_status' => 0])->all(),'id','programme_name'),['multiple' => "multiple", 'class'=>'form-control', 'placeholder' => $model->getAttributeLabel('programme_id')]);
} else {
    echo $form->field($model, 'programme_id')->dropDownList(ArrayHelper::map(common\models\Programme::find()->where(['is_status' => 0])->all(),'id','programme_name'),['class'=>'form-control', 'placeholder' => $model->getAttributeLabel('programme_id')]);
}
?>
</div>

<div class="form-group col-xs-12 col-sm-6 col-lg-4 no-padding jkkesrmsArLangCss">
<div class="col-xs-6">
    <?= Html::submitButton($model->isNewRecord ? Yii::t('centre', 'Create') : Yii::t('centre', 'Update'), ['class' => $model->isNewRecord  ? 'btn btn-block btn-success' : 'btn btn-block btn-info']) ?>
</div>
<div class="col-xs-6">
    <?= Html::a(Yii::t('centre', 'Cancel'), ['index'], ['class' => 'btn btn-default btn-block']); ?>
</div>
</div>

<?php ActiveForm::end(); ?>

查看

function playlist(state=[]){
return state;
}

const store = createStore(playlist);

console.log(store.getState());

Dynamic Form with One Model

[Dynamic Form2[2]

enter image description here

0 个答案:

没有答案