问题
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