如何自动输入数据从一个表格到另一个表格yii2

1bqhqjot  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(188)

我有一个MySQL表和模型resume,其中包含字段

'resume_id', 'lao', 'persen', 'eom', 'tgt_perpetugas' and 'tgt_pergeseran'

我还有另一个表/模型resumes,其中也包含

'resumes_id', 'lao', 'persen', 'eom', 'tgt_perpetugas' and 'tgt_pergeseran'

假设resume表中已经填充了

'resume_id', 'lao', 'persen', 'eom', 'tgt_perpetugas','tgt_pergeseran'

当我在resumes表单中输入数据时,当我通过下拉字段选择与resume表相关的'lao'时,我希望相关的
'persen', 'eom', 'tgt_perpetugas' and 'tgt_pergeseran'
字段将被自动填充。我的reference,但“ AJAX 请求中的错误”。如何自动输入数据从一个表单到另一个表单yii 2如何修复错误或有建议?
我的_form. php看起来像这样

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\select2\Select2;
use yii\helpers\ArrayHelper;
use app\models\Resume;

    $routeAjax = \yii\helpers\Url::toRoute("resume/resumes");

$js=<<<JS
function getRelatedFields(lao_id){
    $.ajax({
        url: '$routeAjax',
        dataType: 'json',
        method: 'GET',
        data: {id: lao_id},
        success: function (data, textStatus, jqXHR) {
             $('#resumes-persen').val(data.persen);
             $('#resumes-eom').val(data.eom);
             $('#resumes-tgt_perpetugas').val(data.tgt_perpetugas);
             $('#resumes-tgt_pergeseran').val(data.tgt_pergeseran);
        },
        beforeSend: function (xhr) {
            alert('loading!');
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log('An error occured!');
            alert('Error in ajax request'+textStatus'\n'+errorThrown);
        }
    });
}

JS;
$this->registerJs($js, \yii\web\View::POS_READY); 

?>

<div class="resumes-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'lao')->widget(Select2::classname(), [
    'data' => ArrayHelper::map(Resume::find()->all(),'lao','lao'),
    'language' => 'en',
    'options' => ['placeholder' => 'Pilih LAO'],
    'pluginOptions' => [
        'allowClear' => true
    ],
    'pluginEvents'=>[
        'select2:select' => 'function(e){getRelatedFields(e.params.data.id);}',
    ]
]); ?>

    <?= $form->field($model, 'persen')->textInput(['type' => 'number','maxlength' => true, 'readOnly'=>false])->label('Perpetugas %') ?>
    <?= $form->field($model, 'eom')->textInput(['maxlength' => true])->label('EOM') ?>

    <?= $form->field($model, 'tgt_perpetugas')->textInput(['maxlength' => true, 'readOnly'=>true])->label('Target Perpetugas') ?>

    <?= $form->field($model, 'tgt_pergeseran')->textInput(['maxlength' => true, 'readOnly'=>true])->label('Target Pergeseran') ?>

    <center>
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>
    </center>

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

</div>

在ResumesController中,我添加了以下内容:

public function actionResumes($id){
    // you may need to check whether the entered ID is valid or not
    $model =  \app\models\Resume::findOne(['lao'=>$id]);
    return \yii\helpers\Json::encode([
            'eom'=>$model->eom,
            'persen'=>$model->persen,
            'tgt_pergeseran'=>$model->tgt_pergeseran,
            'tgt_perpetugas'=>$model->tgt_perpetugas
        ]);
    }
zrfyljdw

zrfyljdw1#

我所理解的是你想让田地
'persen', 'eom', 'tgt_perpetugas', 'tgt_pergeseran'
Resumes表单lao字段中的dropdown()匹配的Resume模型中保存的lao进行比较。
如果这是正确的,那么首先你需要使用Select2事件select2:select而不是使用传统的onchange事件。对你的Select2和视图顶部的脚本做一些修改。
将您的select2替换为以下内容

<?= $form->field($model, 'lao')->widget(Select2::classname(), [
    'data' => ArrayHelper::map(Lao::find()->all(),'lao_id','lao_id'),
    'language' => 'en',
    'options' => ['placeholder' => 'Pilih LAO'],
    'pluginOptions' => [
        'allowClear' => true
    ],
    'pluginEvents'=>[
        'select2:select' => 'function(e){getRelatedFields(e.params.data.id);}',
    ]
]); ?>

将视图顶部的脚本替换为

$routeAjax = \yii\helpers\Url::toRoute("resume/resumes");

$js=<<<JS
function getRelatedFields(lao_id){
    $.ajax({
        url: '$routeAjax',
        dataType: 'json',
        method: 'GET',
        data: {id: lao_id},
        success: function (data, textStatus, jqXHR) {
             $('#resumes-persen').val(data.persen);
             $('#resumes-eom').val(data.eom);
             $('#resumes-tgt_perpetugas').val(data.tgt_perpetugas);
             $('#resumes-tgt_pergeseran').val(data.tgt_pergeseran);
        },
        beforeSend: function (xhr) {
            alert('loading!');
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log('An error occured!');
            alert('Error in ajax request'+textStatus'\n'+errorThrown);
        }
    });
}

JS;
$this->registerJs($js, \yii\web\View::POS_READY);

然后,当您在lao下拉式清单中选取任何选项时,您想要将类似的字段从**Resume模型填入Resumes**ActiveForm。您应该撷取Resume模型中的lao,而不是Resumes模型中的id
因此,请将您的操作替换为以下内容

public function actionResumes($id)
{
    // you may need to check whether the entered ID is valid or not
    $model = \app\models\Resume::findOne(['lao' => $id]);
    return \yii\helpers\Json::encode([
        'eom' => $model->eom,
        'persen' => $model->persen,
        'tgt_pergeseran' => $model->tgt_pergeseran,
        'tgt_perpetugas' => $model->tgt_perpetugas
    ]);
}

相关问题