I am using the following client script to sort a child table based on the trial_start date. It works fine and does what it is supposed to, but the child table index isn’t updated accordingly. The numbering may be something like 19, 4, 24, 2, 1, 22 etc. How can I refresh the child table index?
Thanks in advance!
frappe.ui.form.on(‘doctype’, {
refresh: function(frm) {
console.log(“Child table data before sorting:”, frm.doc.xxx_subject_list_at);
if (frm.doc.child_table && frm.doc.child_table.length > 0) {
// Sort the child table based on the ‘trial_start’ date field
frm.doc.child_table.sort((a, b) => {
console.log(“Comparing”, a.trial_start, “with”, b.trial_start);
if (a.trial_start && b.trial_start) {
return new Date(a.trial_start) - new Date(b.trial_start); // Sort ascending
} else if (a.trial_start) {
return -1; // If only a has a date, it should come before b
} else if (b.trial_start) {
return 1; // If only b has a date, it should come before a
} else {
return 0; // If neither have dates, their order remains unchanged
}
});
console.log(“Child table data after sorting:”, frm.doc.child_table);
// Refresh the child table to reflect the new order
frm.refresh_field(“child_table”);
}
}
});