Rearrange the Nodes in multifield in numberical order for all existing pages
Hi all,
Initial Problem
I have a multifield in pageproperty and saving the node value based on the highest index but i have the problem let us assume we have three nodes (contentOwner_0, contentOwner_1, contentOwner_2) at the time, we are removing the 2nd node and try to add one more node, at this time its saving as contentOwner_3 but i need as contentOwner_2.
Also when a node is deleted, it should rearrange or rename the nodes and provide it in a numerical order as contentOwner_0, contentOwner_1, contentOwner_2.
Solution for this issue we handle it throught JS
(function ($, document) {
"use strict";
function renumberMultifieldItems(multifield) {
multifield.children('.coral3-Multifield-item').each(function (index, element) {
var item = $(element);
var oldName = item.find(".contentOwner").attr("name");
if (oldName) {
var newName = oldName.replace(/contentOwner_\d+/, 'contentOwner_' + index);
item.find(".contentOwner").attr("name", newName);
item.find("input, textarea, select").each(function () {
var input = $(this);
var inputName = input.attr("name");
if (inputName) {
inputName = inputName.replace(/contentOwner_\d+/, 'contentOwner_' + index);
input.attr("name", inputName);
}
});
}
});
}
$(document).on('click', '[coral-multifield-add], .coral3-Multifield-remove, #shell-propertiespage-doneactivator', function () {
var multifield = $('.contentOwnerSelector');
renumberMultifieldItems(multifield);
});
})(jQuery, document);But this code only execute when the multifield is added, removed or when the field is modified but i have lacks of pages so i need to cleanup the existing nodes in the pages and rearrange the nodes in numerical order which is previously authored, created or save.
For this case how we can rearrange the nodes, any suggestion or ideas please
Thanks
Nandheswara