1. <legend id='tMPRH'><style id='tMPRH'><dir id='tMPRH'><q id='tMPRH'></q></dir></style></legend>

    2. <i id='tMPRH'><tr id='tMPRH'><dt id='tMPRH'><q id='tMPRH'><span id='tMPRH'><b id='tMPRH'><form id='tMPRH'><ins id='tMPRH'></ins><ul id='tMPRH'></ul><sub id='tMPRH'></sub></form><legend id='tMPRH'></legend><bdo id='tMPRH'><pre id='tMPRH'><center id='tMPRH'></center></pre></bdo></b><th id='tMPRH'></th></span></q></dt></tr></i><div id='tMPRH'><tfoot id='tMPRH'></tfoot><dl id='tMPRH'><fieldset id='tMPRH'></fieldset></dl></div>
      <tfoot id='tMPRH'></tfoot>
        <bdo id='tMPRH'></bdo><ul id='tMPRH'></ul>

    3. <small id='tMPRH'></small><noframes id='tMPRH'>

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

      时间:2023-10-16

        <bdo id='dJu3l'></bdo><ul id='dJu3l'></ul>
          <tbody id='dJu3l'></tbody>

            <i id='dJu3l'><tr id='dJu3l'><dt id='dJu3l'><q id='dJu3l'><span id='dJu3l'><b id='dJu3l'><form id='dJu3l'><ins id='dJu3l'></ins><ul id='dJu3l'></ul><sub id='dJu3l'></sub></form><legend id='dJu3l'></legend><bdo id='dJu3l'><pre id='dJu3l'><center id='dJu3l'></center></pre></bdo></b><th id='dJu3l'></th></span></q></dt></tr></i><div id='dJu3l'><tfoot id='dJu3l'></tfoot><dl id='dJu3l'><fieldset id='dJu3l'></fieldset></dl></div>
              • <legend id='dJu3l'><style id='dJu3l'><dir id='dJu3l'><q id='dJu3l'></q></dir></style></legend>
              • <small id='dJu3l'></small><noframes id='dJu3l'>

                <tfoot id='dJu3l'></tfoot>
                本文介绍了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 看起来像这样:

                My _form.php for health_card looks like this:

                    <head>
                <script>
                
                        $this->registerJs("$('#healthcard-patient_name').on('change',function(){
                    $.ajax({
                        url: '".yiihelpersUrl::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= appmodelsPatientEntry::findOne(['id'=>$id]);
                    return yiihelpersJson::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: '".yiihelpersUrl::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(s):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=  appmodelsPatient::findOne(['id'=>$id]);
                    return yiihelpersJson::encode([
                        'city'=>$model->city,
                        'state'=>$model->state
                    ]);
                }
                

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

                上一篇:Yii2 CORS with Auth 不适用于非 CRUD 操作 下一篇:Yii2 中的动态表名

                相关文章

                <legend id='KqrRu'><style id='KqrRu'><dir id='KqrRu'><q id='KqrRu'></q></dir></style></legend>

                • <bdo id='KqrRu'></bdo><ul id='KqrRu'></ul>
              • <small id='KqrRu'></small><noframes id='KqrRu'>

                  <i id='KqrRu'><tr id='KqrRu'><dt id='KqrRu'><q id='KqrRu'><span id='KqrRu'><b id='KqrRu'><form id='KqrRu'><ins id='KqrRu'></ins><ul id='KqrRu'></ul><sub id='KqrRu'></sub></form><legend id='KqrRu'></legend><bdo id='KqrRu'><pre id='KqrRu'><center id='KqrRu'></center></pre></bdo></b><th id='KqrRu'></th></span></q></dt></tr></i><div id='KqrRu'><tfoot id='KqrRu'></tfoot><dl id='KqrRu'><fieldset id='KqrRu'></fieldset></dl></div>
                    <tfoot id='KqrRu'></tfoot>