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

Yii2:基于相关表中的另一个字段自动填充字段,第1张

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

您所需要的只是调用

AJAX
请求以获取必填字段。就像下面这样:

  1. (我不知道您的型号名称)查看您的表格,看看

    id
    您的
    patient_name
    字段是什么。通常是这样
    modelname-fieldname
    。我认为您的型号名称是
    Patient
    。因此,的id
    patient_name
    patient-patient_name

  2. 添加一个ajax请求(在您的视图中)。

调用AJAX的代码如下所示:

$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
  • 我假设的ID
    city
    state
    领域具有以下ID(S):
    patient-city
    state-city
    相对。
  • 病人 是您控制器中的一个动作
  • 您可能需要删除警报|日志并对以上代码进行一些自定义
  • 我没有考虑任何清理代码的条件。请确保用户数据正确。

    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::enpre([        'city'=>$model->city,        'state'=>$model->state    ]);}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4974607.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-13
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存