OL Learn

How can I filter the result set in a post pagination script?

I’m using the example post pagination script found in the How-To’s on OL Learn and it works great. My problem comes from the end user’s data. They want the index page to show a commodity and subclass in the TOC page, but these values repeat for every item in the data file. Is there a way to use .filter() so I can select only the unique text values and remove the duplicate text values?

My script:
// Build the TOC
var toc = ‘’;
merge.context.query("#commodity,#subclass").each(function() {
var pageNo = this.info().pageNo;
var text = this.text();
var level = this.tagName();
toc += ‘

’;
toc += ‘’ + text + ‘’;
toc += ‘’;
toc += ‘’ + pageNo + ‘

’;
});
results.html( toc );
// Repaginate the result
merge.section.paginate();
// Update the page numbers
var $numbers = query(’.number’);
merge.context.query(“td.toc”).each(function( index ) {
var pageNo = this.info().pageNo;
var entry = $numbers.get( index );
if( entry ) {
entry.text( pageNo );
}
});

Not quite sure how your template is structured, but from what I understand you should investigate the :first-child and/or the :first-of-type pseudo selectors. These pseudo selectors can be combined with other selectors (e.g. p.myClass:first-of-type will only return the first <p> element that has the class myClass).

Hopefully, that will put you on the right track.

Thanks, the end user decided to use a different data selection that doesn’t repeat, so problem solved!