Skip to content

Commit cf065cd

Browse files
committed
gpnf-sort-nested-form-entries.js: Added support to sort nested form entries.
1 parent 45ab4d6 commit cf065cd

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Gravity Perks // Nested Forms // Sort Nested Form Entries
3+
*
4+
* https://gravitywiz.com/documentation/gravity-forms-nested-forms/
5+
*
6+
* Instructions:
7+
*
8+
* 1. Install this snippet with our free Custom JavaScript plugin.
9+
* https://gravitywiz.com/gravity-forms-code-chest/
10+
*/
11+
// Change this value to the field ID of the child form you want to sort by.
12+
const sortByFieldId = "3";
13+
window.gform.addFilter('gpnf_sorted_entries', function (entries, formId, fieldId, gpnf) {
14+
// Check if entries exist and have the specified field.
15+
if ( !entries || !entries.length || !entries[0][sortByFieldId]) {
16+
console.warn(`GPNF Sort: Field ID ${sortByFieldId} not found in entries or entries are empty. Returning unsorted entries.`);
17+
return entries;
18+
}
19+
20+
// Sort entries by the specified field's label.
21+
return entries.sort((a, b) => {
22+
if ( !a[sortByFieldId] || !a[sortByFieldId].label) return 1;
23+
if ( !b[sortByFieldId] || !b[sortByFieldId].label) return -1;
24+
return a[sortByFieldId].label.localeCompare(b[sortByFieldId].label);
25+
});
26+
});
27+

0 commit comments

Comments
 (0)