Cloning inputs was annoying me because JQrelcopy does not automatically handle the ‘name’ attributes of the inputs. For example, if you were to clone not just a field but a model field you would want your data like this:
0
field1 #part of modelA
field2 #part of modelA
field3 #part of modelA
1
field1 #part of modelA
field2 #part of modelA
field2 #part of modelA
The default implementation does not provide for that. You would instead get:
0
field1
1
field2
2
field3
and so on…
For many reasons I did not want to deal with that in my app.
Bottom line, this patched version will produce output like the following:
The attached file is a drop-in replacement for the default JS file with no further modifications needed. Props to Ianare for the idea. His fork (which is non working for my purposes) is here:
Thanks for this! I’m still using the jappendo extension with a controller that handles sets of “model[]” inputs…hardly a robust solution, but has been working…looking forward to trying this method.
My form currently uses autocomplete fields, so I don’t know if I’ll be able to implement this anytime soon…
Check out addAnother.js by Jeremiah something. He has solved the autocomplete issue (but his extension still has the model [] thingy problem). You may be able to hack that into relcopy.
I tried replacing the javascript file with yours and it did not work. After replacement, it works the same as the original jqrelcopy extension before replacement. The textfield name is still the same for all the duplicated textfields, only the textfields ids are incremental.
Is there any other things I have to delete, replace or change? What about JQRelcopy.php file? Do I need to replace it?
Any idea why this happens and how I can solve this problem?
Thanks very much for the solution. May I check how the ‘view’ should be setup based on the updated JS file? I tried clearing the /assets folder but using the ‘view’ definition below.
I should have mentioned that. I am not a strong jquery coder and I thought it would be simplest to just start with 0 index.
If you display existing ‘rows’ (fetch from DB) this will still work to increment the last value, but if you are starting new you want to use 0 for your first row.
Each time the copy action is triggered the Javascript searches for all "name" attributes and replaces [new] with [new_1], [new_2]. The key [new_x] increments using the JS "counter" variable used in jqrelcopy Javascript.