Sorting child records within repeatable
E
Erin Zhang EAM
We wish to add a feature to sort the child records within each repeatable. Currently it can sort all the child records within the app in an overall way on web app, and permits hold n drag to move around the child records on mobile app. It will be great to have custom sorting feature (alphabetical or user defined, etc) within repeatable on both web and mobile apps. This way helps user to navigate and verify the entered data and improve efficiency in both data entry onsite and verification via desktop.
Pete Zagorski
ON('change', 'repeatable_field_name', (event) => {
sortRepeats();
});
function sortRepeats() {
let reps = $repeatable_field_name || [];
let sorted = reps.sort((a, b) => {
let valueA = CHOICEVALUE(a.form_values[FIELD('sortfield_name').key]) || '';
let valueB = CHOICEVALUE(b.form_values[FIELD('sortfield_name').key]) || '';
return valueA.localeCompare(valueB);
});
rawSetValue('repeatable_field_name', sorted);
}
function rawSetValue(dataname, value) {
const field_key = FIELD(dataname).key;
var result = {
type: "set-value",
key: field_key,
value: JSON.stringify(value)
};
CONFIG().results.push(result);
}
Try out this script in your data events to sort repeatable records in alphabetical order of a CHOICEVALUE. replace data names with your own. Unfortunenatly it only fires when you exit and reenter the repeatable field. I have not been able to get it to fire when saving a repeatable record.
An option in the app editor to sort repeatable records based on a field value would be very useful.