Yapapaya / jquery-cloneya

A jquery plugin to clone DOM elements
MIT License
76 stars 28 forks source link

Auto increment on a multi dimensional PHP sstsy #6

Closed projxit closed 9 years ago

projxit commented 10 years ago

Hi

There is a small issue when cloning form elements that have a PHP multi dimmensional array scheme, for example if I had a fieldset with 3 fields which I wanted to clone, its not renumbering the array correctly, to give an example, if I loaded a form for editing which was pre-populated with 2 sets of information

contact[0][name] contact[0][address] contact[0][phone]

contact[1][name] contact[1][address] contact[1][phone]

Then I clicked clone/add another copy of the fieldset, I'd expect the array to renumbered as

contact[0][name] contact[0][address] contact[0][phone]

contact[1][name] contact[1][address] contact[1][phone]

contact[2][name] contact[2][address] contact[2][phone]

Instead, the first array element number is being cleared from the first set, so we end up with

contact[][name] contact[][address] contact[][phone]

contact[1][name] contact[1][address] contact[1][phone]

contact[2][name] contact[2][address] contact[2][phone]

This is possible by design, but we tracked the issue down to the "j" being unset on line 212, commenting out this if statement fixed the issue for us. But perhaps you could make this behaviour optional.

Many Thanks

razuro commented 10 years ago

Thanks! This fix the cloning twice issue.

actual-saurabh commented 9 years ago

Actually, the plugin doesn't redo the input name attribute. It attempted doing so initially, but that failed in some use cases. It just expects the initial element to have [] in the name attribute and duplicates it (No 0, no 1 or any other numerical indices). It leaves it to your custom js or the server side processing to figure out the indexing. So, you would have to use one of the events to hook into and manage the indexing.

The line of code that you've mentioned is used for incrementing the id attribute so that each element has a unique id. It doesn't touch the name attribute and indexes, at all.

I'm not sure how you have observed this behaviour. Maybe you could point me to your implementation. This could be a different bug altogether, unrelated to the line of code you've mentioned.

actual-saurabh commented 9 years ago

Oops! A contributor added this sometime ago and I missed it. Will update in a while.

actual-saurabh commented 9 years ago

Fixed in 85008432b951ccb70f44c58e191e2f6cab1f18ed

florentsorel commented 9 years ago

It's possible to reset index after deleted clone ?

For example:

contact[0][name] contact[0][address] contact[0][phone]

contact[1][name] contact[1][address] contact[1][phone]

contact[2][name] contact[2][address] contact[2][phone]

If I delete index 0. 1 becomes 0 and 2 becomes 1 ?