Yii2:基于相关表中的另一个字段自动填充字段 [英] Yii2: auto fill fields based on another field from related table

查看:368
本文介绍了Yii2:基于相关表中的另一个字段自动填充字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MySQL表和模型patient_entry,其中包含字段patient_namecitystate.我还有另一个表/模型health_card,其中也包含patient_namecitystate.

I have a MySQL table and model patient_entry which contains fields patient_name, city and state. I also have another table/model health_card which also contains patient_name, city and state.

假设patient_entry表已经用patient_namecitystate填充.

Suppose the patient_entry table is already filled with patient_name, city and state.

当我以health_card形式输入数据时,当我通过与patient_entry表相关的下拉字段选择patient_name时,我希望自动填充相关的citystate字段

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_form.php看起来像这样:

    <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
    ]);
}

推荐答案

您所需要的只是调用AJAX请求以获取所需的字段.就像下面这样:

All you need is calling an AJAX request to get needed fields. Just act like below:

  1. (我不知道您的型号名称)来查看您的表格,并查看您的patient_name字段的id是什么.通常是modelname-fieldname.我假设您的型号名称为Patient.因此,patient_name的ID为patient-patient_name.

  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. It is usually modelname-fieldname. I assume that your model name is Patient. So, the id of patient_name would be patient-patient_name.

添加ajax请求(在您的视图中).

Add an ajax request (in your view).

调用AJAX的代码如下所示:

The code for calling AJAX could look just like below:

$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');
        }
    });
});"); 

注意:

  • 使用您自己的代码更改上述代码中的 ControllerName .
  • 我假设citystate字段的ID具有以下ID:相对地patient-citystate-city.
  • 患者是您控制者中的一项操作
  • 您可能需要删除警报|日志并对上面的代码进行一些自定义
  • 我没有考虑任何清理代码的条件.请确保用户数据正确.

  • Change the ControllerName in above code with your own.
  • I assumed that the id of city and state fields has the following id(s): patient-city and state-city relatively.
  • 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. 最后,将动作代码添加到您的控制器中.

操作代码:

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
    ]);
}

这篇关于Yii2:基于相关表中的另一个字段自动填充字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆