Handling multiple records and models trought ajax in a form using Yii

半城伤御伤魂 提交于 2019-12-13 04:41:02

问题


This must be a common scenario somehow:

On a form called Subscription we have, among other subscription related fields, this:

Select How many foo you need: Select box with 5 options from [1] to [5]

If the user selects 2 then, via Ajax the following should appear:

<!-- for 1 -->
 <input type="text" id="name1">Name of thing</input>  
 <input type="text" id="age1">Age of thing</input>  

<!-- for 2 -->
 <input type="text" id="name2">Name of thing</input>  
 <input type="text" id="age2">Age of thing</input> 

So, we have two tables (Subscription, foo), several records to be inserted on one of those tables (foo), all this on a single form.

Does anyone is kind enough to sketch a quick example on how should we properly deal with scenarios like the described above?

The closer that I found was this, but, doesn't cover multiple models nor ajax. :/ http://www.yiiframework.com/doc/guide/1.1/en/form.table

Really hope someone could give me a push here.


回答1:


Have look at the chapter Handling variable number of inputs in the book Yii 1.1 Application Development Cookbook




回答2:


I'm struggling with an analog problem at the moment. In general, I think you should be able to loop over the models. Every form, will have field names like: Object[$i]['number'] ($i = counter). The ajax request can be a render partial, I think, no?

If I find it tonight, I'll post some code here.



来源:https://stackoverflow.com/questions/10785015/handling-multiple-records-and-models-trought-ajax-in-a-form-using-yii

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!