Yapapaya / jquery-cloneya

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

Map cloned child form to particular parent form in nested clone #18

Closed sakthiemail closed 9 years ago

sakthiemail commented 9 years ago

How can I map the child clone form to a parent form. When It seems all the values of all the child forms are normal array. I could not find the parent of the child clone form. All the form values are displayed in single array. Any mapping functionality should be useful for finding the parent of child clone form. How can I achieve?. Any help is appreciated

gchokeen commented 9 years ago

We can find the parent using jquery closest

sakthiemail commented 9 years ago

Thank you for reply. Can you give some sample code for getting parent child combination for getting values. For example Name value - name1 --mobile value 1234 --mobile value 12345 Name Value - name2 --mobile value 123467 --mobile value123468

using php and jQuery cloneya Please, Can you give right direction to me with sample code for achieving this.

actual-saurabh commented 9 years ago

This is not a job for cloneya.

You have to set your indices correctly. Your example, in php is:

// Ref. 1
$name[] = 'name1';
$mobile[] = '1234';
$mobile[] = '12345';
$name[] = 'name2';
$mobile[] = '123467';
$mobile[] = '123468';

So, what you get is:

// Ref. 2
$name = array( 'name1', 'name2');
$mobile = array('1234', '12345', '123467', '123468');

What you want is:

// Ref. 3
$result = array(
    // name       phone numbers
    'name1' => array(
        0 => '1234',
        1 => '12345'
    ),
    'name2' => array(
        0 => '123467', 
        1 => '123468'
    )
);

To get that, your inputs would need to be:

// Ref. 4
$mobile['name1'] = '1234';
$mobile['name1'] = '12345';
$mobile['name2'] = '123467';
$mobile['name2'] = '123468';

This is complicated compared to this:

// Ref. 5
$name[0] = 'name1';
$mobile[0][0] = '1234';
$mobile[0][1] = '12345';
$name[1] = 'name2';
$mobile[1][0] = '123467';
$mobile[1][1] = '123468';

The array you'll get is:

// Ref. 6
$name = array(
    0 => 'name1',
    1 => 'name2'
);

$mobile = array(
    0 => array(
        0 => '1234',
        1 => '12345'
    ),
    1 => array(
        0 => '123467',
        1 => '123468'
);

I'm sure with a few lines of code, you can process this to get what you want.

Now, going back to Ref.5, we don't do such index management with cloneya because we don't know what you (the user) wants to do with the inputs. So, you'd have to mange them yourself. There are enough event hooks to do that. See if clone_form_input works for you. It is trigerred as soon as any input is cloned and will give you the new clone. You can check the current arrangement of indices and accordingly manipulate the clone's index.

How to do that is a javascript (or jQuery) question. Helping you with that is beyond the scope of the support that I can provide.

sakthiemail commented 9 years ago

Thank you very much for giving this explanation. Thanks for your help. I am sure, Its enough to my expectation. Thanks.