Skip Navigation

[Resolved] display blank field if item in wpv-loop equals previous item

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a table-style View that displays several columns of custom field values. I would like to use a conditional that hides the value in a specific column if the value is identical to the same column in a previous row.

Solution: When the loop is rendered, each iteration has no reference to a previous or future iteration. That means it's not possible to set up a conditional that compares a value from one iteration to a value in another iteration. Instead, you would have to do this with custom CSS and JavaScript. For example, add a custom class to each system-cf cell, and a wrapper span tag like this:

<td class="only-first-system-cf"><span>[wpv-post-field name="wpcf-system-cf"]</span></td>

Then in your custom CSS:

.only-first-system-cf > span {
  display:none;
}
 
.only-first-system-cf.first > span {
  display:inline;
}

Then in your custom JS:

jQuery(document).ready(function(){
  var firsts = [];
  var txt = '';
  jQuery('.only-first-system-cf').each(function(index, item) {
    txt = jQuery(item).text();
    if(firsts.indexOf(txt) == -1){
        firsts.push(txt);
        jQuery(item).addClass('first');
    }
  });
});

Relevant Documentation:
https://toolset.com/documentation/user-guides/digging-into-view-outputs/
https://toolset.com/documentation/user-guides/adding-custom-css-views/
https://toolset.com/documentation/user-guides/adding-custom-javascript-views/
https://api.jquery.com/jquery.each/

This support ticket is created 6 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by davidm-13 6 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1108854

Tell us what you are trying to do?

In a View Loop Editor I have:

                <wpv-loop>
			<tr>
				[wpv-post-body view_template="Loop item in Complaints list"]
			</tr>
		</wpv-loop>

and the Loop item in Complaints list is:

<td>[wpv-post-field name="wpcf-system-cf"]</td>
<td>[wpv-post-field name="wpcf-complaint-cf"]</td>
<td>[wpv-post-taxonomy type="action-medicinal" format="name"]</td>
<td>[types field="prep-admin"][/types]</td>
<td>[types field="parts-used"][/types]</td>
<td>[types field="source"][/types]</td>

the output is sorted by the 1st field - "wpcf-system-cf", if this value is repeated, I want only the 1st occurrence to appear and in the following rows with the same value, the 1st column should be blank. How do I do this?

Thanks for your help

#1109054

Hi, when the loop is rendered, each iteration has no reference to a previous or future iteration. That means it's not possible to set up a conditional that compares a value from one iteration to a value in another iteration. Instead, you would have to do this with custom CSS and JavaScript. For example, add a custom class to each system-cf cell, and a wrapper span tag like this:

...
<td class="only-first-system-cf"><span>[wpv-post-field name="wpcf-system-cf"]</span></td>
...

Then in your custom CSS:

.only-first-system-cf > span {
  display:none;
}

.only-first-system-cf.first > span {
  display:inline;
}

Then in your custom JS:

jQuery(document).ready(function(){
  var firsts = [];
  var txt = '';
  jQuery('.only-first-system-cf').each(function(index, item) {
    txt = jQuery(item).text();
    if(firsts.indexOf(txt) == -1){
    	firsts.push(txt);
        jQuery(item).addClass('first');
    }
  });
});
#1109081

Thanks Christian, much appreciated.

Works perfectly

David