Skip to content
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
48 changes: 48 additions & 0 deletions gp-auto-list-field/gpalf-count-only-non-blank-rows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Gravity Perks // Auto List Field // Dynamic Row Labels for List Fields
* https://gravitywiz.com/documentation/gravity-forms-auto-list-field/
*
* Count only non-blank rows in a List field.
*
* Instructions:
*
* 1. Install this snippet with our free Custom JavaScript plugin.
* https://gravitywiz.com/gravity-forms-code-chest/
*/
gform.addFilter('gform_merge_tag_value_pre_calculation', function (value, match, isVisible, formulaField, formId) {
if (typeof match[3] === 'undefined' || match[3].indexOf(':count') === -1) {
return value;
}

var inputId = match[1];
var fieldId = parseInt(inputId, 10);

var $fieldWrapper = $(`#gform_${formId} #field_${formId}_${fieldId}`);
if ($fieldWrapper.length === 0) {
return value;
}

var $rows = $fieldWrapper.find('.gfield_list_group');
if ($rows.length === 0) {
return value;
}

var nonBlankCount = 0;

$rows.each(function () {
var isNonBlank = false;

$(this).find('input').each(function () {
if ($(this).val().trim() !== '') {
isNonBlank = true;
return false;
}
});

if (isNonBlank) {
nonBlankCount++;
}
});
Comment on lines +30 to +45
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve non-blank row detection to include all form controls.

The current implementation only checks for input elements, but list fields might contain other form controls like select, textarea, etc. Additionally, the code uses jQuery's .each() with a return false to break the inner loop, which is correct but could be documented for clarity.

 	var nonBlankCount = 0;
 
 	$rows.each(function () {
 		var isNonBlank = false;
 
-		$(this).find('input').each(function () {
+		// Check all possible form controls (inputs, selects, textareas)
+		$(this).find('input, select, textarea').each(function () {
 			if ($(this).val().trim() !== '') {
 				isNonBlank = true;
+				// Break the .each() loop early since we found a non-blank value
 				return false;
 			}
 		});
 
 		if (isNonBlank) {
 			nonBlankCount++;
 		}
 	});
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
var nonBlankCount = 0;
$rows.each(function () {
var isNonBlank = false;
$(this).find('input').each(function () {
if ($(this).val().trim() !== '') {
isNonBlank = true;
return false;
}
});
if (isNonBlank) {
nonBlankCount++;
}
});
var nonBlankCount = 0;
$rows.each(function () {
var isNonBlank = false;
// Check all possible form controls (inputs, selects, textareas)
$(this).find('input, select, textarea').each(function () {
if ($(this).val().trim() !== '') {
isNonBlank = true;
// Break the .each() loop early since we found a non-blank value
return false;
}
});
if (isNonBlank) {
nonBlankCount++;
}
});


return nonBlankCount;
});
Loading