jquery.repeater icon indicating copy to clipboard operation
jquery.repeater copied to clipboard

Using setList causes repeater to add 2 data-repeater-items at a time instead of 1

Open maparaschivei opened this issue 8 years ago • 8 comments

Hello,

I've noticed that when I use setList to load my form elements subsequently adding a repeater item via the "add" button (data-repeater-create) creates 2 additional items instead of one.

This only happens when I use setList. Any ideas what could be causing this to happen?

Thanks.

maparaschivei avatar Feb 19 '17 22:02 maparaschivei

Okay,

It appears for some reason the click event for [data-repeater-create] is fired twice so it adds 2 additional repeater items per click of the "add" button. I am not sure why this happens only when setList is used. Could be related to "event bubbling" due to how setList works.

In any case I have a solution that fixes this issue for me. It shouldn't break anything but I haven't done any exhaustive testing.

In jquery.repeater.js we have to make the following changes:

Before:

$filterNested($self.find('[data-repeater-create]'), fig.repeaters).click(function () {
	addItem();
});

After:

+ $filterNested($self.find('[data-repeater-create]'), fig.repeaters).click(function (event) {
	addItem();
+	event.stopImmediatePropagation();	
});

maparaschivei avatar Feb 20 '17 14:02 maparaschivei

again, thanks.

fluctusz avatar Mar 30 '17 09:03 fluctusz

Thank you, you save my life 💃

agengdp avatar May 19 '17 13:05 agengdp

DIG DOWN THE PROBLEM

I found the issue happened when we do something like below:

// Initiate repeater
$('.my-repeater').repeater({
      // whatever
});

// Set the data
var my_repeater = $('.my-repeater').repeater();
my_repeater.setList([
      { 'input_name': 'foo-1' },
      { 'input_name': 'foo-2' }
]);

PROBLEM

The problem is on this line: var my_repeater = $('.my-repeater').repeater(); We're re-instantiating the same .repeater().

SOLUTION

I found the correct method of calling setList() is:

// Initiate the repeater and set a variable
var my_repeater = $('.my-repeater').repeater({
      // whatever
});

// Set the data on the previous repeater on edit mode
if( true === edit_mode ) {
      my_repeater.setList([
            { 'input_name': 'foo-1' },
            { 'input_name': 'foo-2' }
      ]);
}

So, in this way, there's no need to edit the library core file.

mayeenulislam avatar Sep 17 '17 13:09 mayeenulislam

var repeater = $('.repeater').repeater(); repeater.setList([ { 'text-input': 'set-a', 'inner-group': [{ "outer-group": [ { "text-input": "A", "service-type": "food", "inner-group": [ { "inner-text-input": "B", "vendor-type": "conduct", "vendor-name": "fathima", "vendor-cost": "25" } ] } ] }] }, { 'text-input': 'set-foo' } ]);

killerkrish avatar May 21 '20 14:05 killerkrish

I used nested so i am not getting the value

killerkrish avatar May 21 '20 14:05 killerkrish

Is this code setList to set data which are fetched from the server? I have a case where I need to use repeater where it should show multiple added data in the repeater, the reverse of the form.

jigapate avatar Feb 02 '21 10:02 jigapate

how do you set a variable here

DIG DOWN THE PROBLEM

I found the issue happened when we do something like below:

// Initiate repeater
$('.my-repeater').repeater({
      // whatever
});

// Set the data
var my_repeater = $('.my-repeater').repeater();
my_repeater.setList([
      { 'input_name': 'foo-1' },
      { 'input_name': 'foo-2' }
]);

PROBLEM

The problem is on this line: var my_repeater = $('.my-repeater').repeater(); We're re-instantiating the same .repeater().

SOLUTION

I found the correct method of calling setList() is:

// Initiate the repeater and set a variable
var my_repeater = $('.my-repeater').repeater({
      // whatever
});

// Set the data on the previous repeater on edit mode
if( true === edit_mode ) {
      my_repeater.setList([
            { 'input_name': 'foo-1' },
            { 'input_name': 'foo-2' }
      ]);
}

So, in this way, there's no need to edit the library core file.

how do you set a new variable here please help me

chikhalifa avatar Oct 27 '21 23:10 chikhalifa