Hide Empty Columns in Summary Table

Instructions

Code

Filename: gpnf-hide-empty-columns.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
 * Gravity Perks // Nested Forms // Hide Empty Columns in Summary Table
 * https://gravitywiz.com/documentation/gravity-forms-nested-forms/
 *
 * Instruction Video: https://www.loom.com/share/84e6d9c5c4334fd79d41e68942fae0d5
 */
add_filter( 'gp_template_output_nested-entries', function( $markup, $located_template, $load, $args ) {
	ob_start();
	/**
	 * @var array  $nested_fields      An array of GF_Field objects.
	 * @var array  $nested_form        The form object of the nested form.
	 * @var array  $nested_field_ids   An array of nested field IDs.
	 * @var array  $entries            An array of child entries submitted from the current Nested Form field.
	 * @var array  $labels             An array of labels used in this template.
	 * @var array  $aria_labels        An array of labels used for screen readers.
	 * @var array  $actions            An array of HTML strings used to display field actions.
	 * @var bool   $enable_duplication Can child entries be duplicated?
	 * @var int    $column_count       The number of columns.
	 * @var string $add_button         The markup for the "Add Entry" button.
	 * @var string $add_button_message The markup for situational messages related to the "Add Entry" button.
	 */
	extract( $args );
	if ( ! has_action( 'wp_footer', 'gpnf_hec_output_script' ) ) {
		add_action( 'wp_footer', 'gpnf_hec_output_script' );
		add_action( 'gform_preview_footer', 'gpnf_hec_output_script' );
	}
	?>
	<div class="gpnf-nested-entries-container ginput_container">
		<table class="gpnf-nested-entries">

			<thead>
			<tr>
				<?php foreach ( $nested_fields as $nested_field ) : ?>
					<th class="gpnf-field-<?php echo $nested_field['id']; ?>" data-bind="visible: window.gpnfHecColumnHasValue( '<?php echo $nested_field['id']; ?>', entries() );">
						<?php echo GFCommon::get_label( $nested_field ); ?>
					</th>
				<?php endforeach; ?>
				<th class="gpnf-row-actions">&nbsp;</th>
			</tr>
			</thead>

			<tbody data-bind="visible: entries().length, foreach: entries">
			<tr data-bind="attr: { 'data-entryid': id }">
				<?php foreach ( $nested_fields as $nested_field ) : ?>
					<td class="gpnf-field" data-bind="html: f<?php echo $nested_field['id']; ?>.label,visible: window.gpnfHecColumnHasValue( '<?php echo $nested_field['id']; ?>', $parent.entries() );">&nbsp;</td>
				<?php endforeach; ?>
				<td class="gpnf-row-actions">
					<ul>
						<li class="edit"><a href="#" data-bind="click: $parent.editEntry"><?php echo $labels['edit_entry']; ?></a></li>
						<li class="delete"><a href="#" data-bind="click: $parent.deleteEntry"><?php echo $labels['delete_entry']; ?></a></li>
					</ul>
				</td>
			</tr>
			</tbody>

			<tbody data-bind="visible: entries().length <= 0">
			<tr class="gpnf-no-entries" data-bind="visible: entries().length <= 0">
				<td colspan="<?php echo $column_count; ?>">
					<?php echo $labels['no_entries']; ?>
				</td>
			</tr>
			</tbody>

		</table>

		<?php echo $add_button; ?>

	</div>
	<?php
	return ob_get_clean();
}, 10, 5 );

add_filter( 'gp_template_output_nested-entries-detail-simple', function( $markup, $located_template, $load, $args ) {
	ob_start();
	/**
	 * @var array  $nested_fields      An array of GF_Field objects.
	 * @var array  $nested_form        The form object of the nested form.
	 * @var array  $nested_field_ids   An array of nested field IDs.
	 * @var array  $entries            An array of child entries submitted from the current Nested Form field.
	 * @var array  $labels             An array of labels used in this template.
	 * @var array  $aria_labels        An array of labels used for screen readers.
	 * @var array  $actions            An array of HTML strings used to display field actions.
	 * @var bool   $enable_duplication Can child entries be duplicated?
	 * @var int    $column_count       The number of columns.
	 * @var string $add_button         The markup for the "Add Entry" button.
	 * @var string $add_button_message The markup for situational messages related to the "Add Entry" button.
	 */
	extract( $args );
	?>
	<?php
	/**
	 * @var \GF_Field $field            The current Nested Form field.
	 * @var array     $nested_fields    An array of GF_Field objects.
	 * @var array     $nested_form      The form object of the nested form.
	 * @var array     $nested_field_ids An array of nested field IDs.
	 * @var string    $actions          Generated HTML for displaying related entries link.
	 */
	?>
	<div class="gpnf-nested-entries-container-<?php echo $field->formId; ?>-<?php echo $field->id; ?> gpnf-nested-entries-container gpnf-entry-view ginput_container">

		<table class="gpnf-nested-entries gpnf-nested-entries-simple">

			<thead>
			<tr>
				<?php
				foreach ( $nested_fields as $nested_field ) :
					if ( ! gpnf_hec_column_has_value( $nested_field->id, $entries ) ) :
						continue;
					endif;
					?>
					<th class="gpnf-field-<?php echo $nested_field['id']; ?>">
						<?php echo GFCommon::get_label( $nested_field ); ?>
					</th>
				<?php endforeach; ?>
			</tr>
			</thead>

			<tbody>
			<?php foreach ( $entries as $entry ) : ?>
				<?php $field_values = gp_nested_forms()->get_entry_display_values( $entry, $nested_form, $nested_field_ids ); ?>
				<tr>
					<?php
					foreach ( $nested_fields as $nested_field ) :
						if ( ! gpnf_hec_column_has_value( $nested_field->id, $entries ) ) :
							continue;
						endif;
						$field_value = rgars( $field_values, "{$nested_field['id']}/label" );
						?>
						<td class="gpnf-field"
							data-heading="<?php echo GFCommon::get_label( $nested_field ); ?>"
							data-value="<?php echo esc_attr( $field_value ); ?>">
							<?php echo $field_value; ?>
						</td>
					<?php endforeach; ?>
				</tr>
			<?php endforeach; ?>
			</tbody>

		</table>

	</div>

	<?php
	return ob_get_clean();
}, 10, 5 );

function gpnf_hec_output_script() {
	?>
	<script>
		function gpnfHecColumnHasValue( fieldId, entries ) {
			var hasValue = false;
			jQuery.each( entries, function( i, entry ) {
				if( ( entry[ fieldId ] && entry[ fieldId ].label ) || ( entry[ `f${fieldId}` ] && entry[ `f${fieldId}` ].label ) ) {
					hasValue = true;
				}
			} );
			return hasValue;
		}
	</script>
	<?php
}

function gpnf_hec_column_has_value( $field_id, $entries ) {
	foreach ( $entries as $entry ) {
		foreach ( $entry as $key => $value ) {
			// Match $field_id or subfield IDs (For fields like Name, Address, etc).)
			if ( preg_match( '/^' . preg_quote( $field_id, '/' ) . '(\.\d+)?$/', $key ) ) {
				if ( ! rgblank( $value ) ) {
					return true;
				}
			}
		}
	}
	return false;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.