Skip to content

gpnf-sort-nested-form-entries.js: Added support to sort nested form entries. #1060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions gp-nested-forms/gpnf-sort-nested-form-entries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Gravity Perks // Nested Forms // Sort Nested Form Entries
*
* https://gravitywiz.com/documentation/gravity-forms-nested-forms/
*
* Instructions:
*
* 1. Install this snippet with our free Custom JavaScript plugin.
* https://gravitywiz.com/gravity-forms-code-chest/
*/
// Change this value to the field ID of the child form you want to sort by.
const sortByFieldId = "3";
window.gform.addFilter('gpnf_sorted_entries', function (entries, formId, fieldId, gpnf) {
// Check if entries exist and have the specified field.
if ( !entries || !entries.length || !entries[0][sortByFieldId]) {
console.warn(`GPNF Sort: Field ID ${sortByFieldId} not found in entries or entries are empty. Returning unsorted entries.`);
return entries;
}

// Sort entries by the specified field's label.
return entries.sort((a, b) => {
if ( !a[sortByFieldId] || !a[sortByFieldId].label) return 1;
if ( !b[sortByFieldId] || !b[sortByFieldId].label) return -1;
return a[sortByFieldId].label.localeCompare(b[sortByFieldId].label);
});
});

Loading