Skip to content

Commit f94e9f5

Browse files
committed
Fix issues with replacing numbers in repeater in repeaters
1 parent 7dcba8d commit f94e9f5

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

src/assets/js/properties/repeater.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,19 +223,25 @@ class Repeater {
223223
}
224224

225225
/**
226-
* Replace first number in name array.
226+
* Replace array number in name array.
227227
*
228228
* @param {string} name
229229
* @param {int} j
230+
* @param {int} p
231+
*
230232
* @return {string}
231233
*/
232-
replaceFirstNumber(name, j) {
234+
replaceArrayNumber(name, j, p) {
233235
name = name.split('[');
234236

237+
if (typeof p === 'undefined') {
238+
p = 0;
239+
}
240+
235241
for (var i = 0, l = name.length; i < l; i++) {
236242
var part = name[i];
237243

238-
if (/\d+$/.test(part.replace(']', ''))) {
244+
if (/\d+$/.test(part.replace(']', '')) && (p === 0 || p === i)) {
239245
name[i] = j + ']';
240246
break;
241247
}
@@ -301,37 +307,58 @@ class Repeater {
301307

302308
$tbody.find('> tr').each((i, el) => {
303309
let $el = $(el);
310+
let $rpt = $el.closest('.papi-property-repeater-top');
311+
let position = 0;
312+
let count = 0
313+
314+
if ($rpt.length) {
315+
while ($rpt.length) {
316+
if (!$rpt.parentsUntil('.papi-property-repeater-top').length) {
317+
break;
318+
}
319+
320+
$rpt = $rpt.closest('.papi-property-repeater-top').prev();
321+
322+
if ($rpt.length) {
323+
count++;
324+
325+
if (count > 1) {
326+
position += 3;
327+
}
328+
}
329+
}
330+
}
304331

305332
$el.find('> td:first-child .count').text(i + 1);
306333

307334
// Replace `data-slug` attribute.
308335
$el.find('[data-replace-slug="true"]').each(function () {
309336
let $prop = $(this);
310-
$prop.attr('data-slug', self.replaceFirstNumber($prop.attr('data-slug'), i));
337+
$prop.attr('data-slug', self.replaceArrayNumber($prop.attr('data-slug'), i, position));
311338
});
312339

313340
// Replace `data-papi-rule` attribute.
314341
$el.find('[data-papi-rule*="papi_"]').each(function () {
315342
let $prop = $(this);
316-
$prop.attr('data-papi-rule', self.replaceFirstNumber($prop.attr('data-papi-rule'), i));
343+
$prop.attr('data-papi-rule', self.replaceArrayNumber($prop.attr('data-papi-rule'), i, position));
317344
});
318345

319346
// Replace `data-papi-json` attribute.
320347
$el.find('[data-papi-json*="papi_"]').each(function () {
321348
let $prop = $(this);
322-
$prop.attr('data-papi-json', self.replaceFirstNumber($prop.attr('data-papi-json'), i));
349+
$prop.attr('data-papi-json', self.replaceArrayNumber($prop.attr('data-papi-json'), i, position));
323350
});
324351

325352
// Replace id attribute.
326353
$el.find('[id*="papi_"]').each(function () {
327354
let $prop = $(this);
328-
$prop.attr('id', self.replaceFirstNumber($prop.attr('id'), i));
355+
$prop.attr('id', self.replaceArrayNumber($prop.attr('id'), i, position));
329356
});
330357

331358
// Replace name attribute.
332359
$el.find('[name*="papi_"]').each(function () {
333360
let $prop = $(this);
334-
$prop.attr('name', self.replaceFirstNumber($prop.attr('name'), i));
361+
$prop.attr('name', self.replaceArrayNumber($prop.attr('name'), i, position));
335362
});
336363
});
337364

0 commit comments

Comments
 (0)