Skip to content

Commit 1ae5515

Browse files
authored
gpalf-count-only-non-blank-rows.js: Added support to calculate only non-blank rows in a list field.
1 parent a4c8c66 commit 1ae5515

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Gravity Perks // Auto List Field // Dynamic Row Labels for List Fields
3+
* https://gravitywiz.com/documentation/gravity-forms-auto-list-field/
4+
*
5+
* Count only non-blank rows in a List field.
6+
*
7+
* Instructions:
8+
*
9+
* 1. Install this snippet with our free Custom JavaScript plugin.
10+
* https://gravitywiz.com/gravity-forms-code-chest/
11+
*/
12+
gform.addFilter('gform_merge_tag_value_pre_calculation', function (value, match, isVisible, formulaField, formId) {
13+
if (typeof match[3] === 'undefined' || match[3].indexOf(':count') === -1) {
14+
return value;
15+
}
16+
17+
var inputId = match[1];
18+
var fieldId = parseInt(inputId, 10);
19+
20+
var $fieldWrapper = $(`#gform_${formId} #field_${formId}_${fieldId}`);
21+
if ($fieldWrapper.length === 0) {
22+
return value;
23+
}
24+
25+
var $rows = $fieldWrapper.find('.gfield_list_group');
26+
if ($rows.length === 0) {
27+
return value;
28+
}
29+
30+
var nonBlankCount = 0;
31+
32+
$rows.each(function () {
33+
var isNonBlank = false;
34+
35+
$(this).find('input').each(function () {
36+
if ($(this).val().trim() !== '') {
37+
isNonBlank = true;
38+
return false;
39+
}
40+
});
41+
42+
if (isNonBlank) {
43+
nonBlankCount++;
44+
}
45+
});
46+
47+
return nonBlankCount;
48+
});

0 commit comments

Comments
 (0)