简体   繁体   English

Yii2:根据相关表中的另一个字段自动填充字段

[英]Yii2: auto fill fields based on another field from related table

I have a MySQL table and model patient_entry which contains fields patient_name , city and state . 我有一个MySQL表和模型patient_entry ,其中包含字段patient_namecitystate I also have another table/model health_card which also contains patient_name , city and state . 我也有另一个表/模型health_card其中还包含patient_namecitystate

Suppose the patient_entry table is already filled with patient_name , city and state . 假设patient_entry表已经填充了patient_namecitystate

When I am entering data in health_card form, when I select the patient_name via drop-down field related to patient_entry table, I want the related city and state fields to be auto-filled. 当我将数据输入health_card形式,当我选择了patient_name通过相关下拉字段patient_entry表,我想相关的citystate领域进行自动填充。

My _form.php for health_card looks like this: _form.phphealth_card看起来是这样的:

    <head>
<script>

        $this->registerJs("$('#healthcard-patient_name').on('change',function(){
    $.ajax({
        url: '".yii\helpers\Url::toRoute("HealthCard/patient")."',
        dataType: 'json',
        method: 'GET',
        data: {id: $(this).val()},
        success: function (data, textStatus, jqXHR) {
            $('#healthcard-city').val(data.city);
            $('#healthcard-pincode').val(data.pin);
        },
        beforeSend: function (xhr) {
            alert('loading!');
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log('An error occured!');
            alert('Error in ajax request');
        }
    });
});"); 

      </script>
</head>

And in the controller I have added, as per the suggestion, this: 在控制器中,我根据建议添加了:

public function actionPatient($id){
    // you may need to check whether the entered ID is valid or not
    $model= \app\models\PatientEntry::findOne(['id'=>$id]);
    return \yii\helpers\Json::encode([
        'city'=>$model->disrict_city,
        'pin'=>$model->pin_code
    ]);
}

All you need is calling an AJAX request to get needed fields. 您所需要的只是调用AJAX请求来获取所需的字段。 Just act like below: 就像下面这样:

  1. (I do not know your model name)take a look at your form and see what is the id of your patient_name field. (我不知道您的型号名称)查看您的表单,看看您的patient_name字段的idpatient_name It is usually modelname-fieldname . 它通常是modelname-fieldname I assume that your model name is Patient . 我假设您的型号名称是Patient So, the id of patient_name would be patient-patient_name . 因此, patient_name的id将是patient-patient_name

  2. Add an ajax request (in your view). 添加ajax请求(在您的视图中)。

The code for calling AJAX could look just like below: 调用AJAX的代码如下所示:

$this->registerJs("$('#patient-patient_name').on('change',function(){
    $.ajax({
        url: '".yii\helpers\Url::toRoute("controllerName/patient")."',
        dataType: 'json',
        method: 'GET',
        data: {id: $(this).val()},
        success: function (data, textStatus, jqXHR) {
            $('#patient-city').val(data.city);
            $('#patient-state').val(data.state);
        },
        beforeSend: function (xhr) {
            alert('loading!');
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log('An error occured!');
            alert('Error in ajax request');
        }
    });
});"); 

Notes: 笔记:

  • Change the ControllerName in above code with your own. 使用您自己的代码更改上述代码中的ControllerName
  • I assumed that the id of city and state fields has the following id(s): patient-city and state-city relatively. 我假设city state字段ID具有以下ID: patient-city state-city相对。
  • patient is an action in your controller 患者是您控制器中的一个动作
  • You may need to remove alerts|logs and do some customization on above code 您可能需要删除警报日志并对上述代码进行一些自定义
  • I didn't consider any conditions for code cleaning. 我没有考虑代码清理的任何条件。 Please make sure that user data is correct. 请确保用户数据正确无误。

    1. Finally, add action code into your controller. 最后,将操作代码添加到控制器中。

Action code: 行动代码:

public function actionPatient($id){
    // you may need to check whether the entered ID is valid or not
    $model=  \app\models\Patient::findOne(['id'=>$id]);
    return \yii\helpers\Json::encode([
        'city'=>$model->city,
        'state'=>$model->state
    ]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM