自动完成先前值的字段 [英] Auto complete fields of a previous values
问题描述
我正在 Yii Framework 中做一个小型应用程序,因为我的数据库是这样的
I am doing a small application in Yii Framework for that my database is something like this
=== Invoices ===
id (PK)
customer_id
invoice_title
order_no
invoice_issue_date
due_date
description
=== Customers ===
id (PK)
email_address
customer_name
address
city
state
postal_code
description
我已经在Invoice model
中绘制了Customer model
,以便可以在single Invoice form
中输入两个模型的所有值.但是有一个问题,让我们假设我有一个客户名称xyz
,我有saved before
.现在当我要进入again fill the Customer name with xyz
时,它应该显示all the fields of both models like invoice_title,order_no,invoice_issue_date,due_date,description,email_address,customer_name,address etc. in that input fields of the form
,这样我就不必re-enter all the fields again
.因此如何在Yii框架中实现此目标.任何帮助和建议都将是高度赞赏.如果需要,可以分享我所做代码的更多说明.
请帮帮我.我完全被困在这里.
I have rendered the Customer model
in Invoice model
so that I can enter all the values for both models in a single Invoice form
.But there is one problem,let us assume that I have a customer name xyz
which I had saved before
.Now when I am going to again fill the Customer name with xyz
,it should show all the fields of both models like invoice_title,order_no,invoice_issue_date,due_date,description,email_address,customer_name,address etc. in that input fields of the form
so that I don't have to re-enter all the fields again
.So how this can be achive in Yii framework.Any help and suggestions will be highly appreciable.More clarification on codes that I have done can be shared if needed.
Please help me out.I am totally stuck here.
推荐答案
要做到这一点,因为每个人都已经提到过,您需要ajax和一些javascript.逻辑是这样的:
To do this as everyone has already mentioned you need ajax, and some javascript. The logic is something like this:
-
在客户名称的下拉列表中选择一个值时,触发ajax调用以检索有关该用户的信息.这可以通过ajax选项轻松完成,它是上述有关ajax选项的文档可以在
The documentation for the above options for ajax can be seen in jquery's ajax documentation.
然后在服务器端找到特定的客户,并将响应发送到浏览器.示例:
Then in the server side find the particular customer, and send a response to the browser. Example:
// in the controllername code an action that will return the values as json public function actionCustomerdetails($id){ $var=Customers::model()->findByPk($id); echo CJSON::encode($var); }
-
收到服务器响应后,将填写相应的字段.这可以在ajax的成功函数回调中完成,在上面的代码中是updateFields:
When you receive the server response populate the respective fields. This can be done in the success function callback for ajax, in the above code it was updateFields:
Yii::app()->clientScript->registerScript('update',' function updateFields(data, textStatus, jqXHR){ // select each input field by id, and update its value $("#Customers_postal_code").val(data.postal_code); $("#Customers_city").val(data.city); $("#Customers_address").val(data.address); // similarly update the fields for the other inputs } ');
注意: 您的客户可以有许多发票,因此问题是给定客户名称时应选择哪张发票.那是您必须处理的事情,我认为我的答案有足够的代码来帮助您前进.
Notes: Your customer can have many invoices, so the question will be which invoice to select given a customer name. That's something you'll have to handle, i think my answer has enough code to get you going.
要了解输入字段ID,只需检查生成的html.
To know the input field ids, you can simply check the generated html.
这篇关于自动完成先前值的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!