Skip to content

Commit 688fc31

Browse files
authored
Hide empty filters through configuration (#117)
* Hide empty filters through configuration * Remove this from compact() call
1 parent 11bc8e5 commit 688fc31

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ Responsible for defining global configuration. Look for full example here - [con
187187
- **`show_facet_stats`** `true` | `false` (Default) to retrieve the min, max, avg, sum rating values from the whole filtered dataset
188188
- **`conjunction`** `true` (Default) stands for an _AND_ query (results have to fit all selected facet-values), `false` for an _OR_ query (results have to fit one of the selected facet-values)
189189
- **`chosen_filters_on_top`** `true` (Default) Filters that have been selected will appear above those not selected, `false` for filters displaying in the order set out by `sort` and `order` regardless of selected status or not
190+
- **`hide_zero_doc_count`** `true` | `false` (Default) Hide filters that have 0 results returned
190191

191192
- **`sortings`** you can configure different sortings like `tags_asc`, `tags_desc` with options and later use it with one key.
192193

docs/configuration.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ var itemsjs = require('itemsjs')(data, {
3939
// If you want to retrieve the min, max, avg, sum rating values from the whole filtered dataset
4040
show_facet_stats: true,
4141
// If you don't want selected filters to be positioned at the top of the filter list
42-
chosen_filters_on_top: false
42+
chosen_filters_on_top: false,
43+
// If you don't want to show filters with no results returned
44+
hide_zero_doc_count: true
4345
}
4446
},
4547
searchableFields: ['name', 'tags'],

src/helpers.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ const getBuckets = function(data, input, aggregations) {
333333
let title;
334334
let show_facet_stats;
335335
let chosen_filters_on_top;
336+
let hide_zero_doc_count;
336337

337338
if (aggregations[k]) {
338339
order = aggregations[k].order;
@@ -341,6 +342,7 @@ const getBuckets = function(data, input, aggregations) {
341342
title = aggregations[k].title;
342343
show_facet_stats = aggregations[k].show_facet_stats || false;
343344
chosen_filters_on_top = aggregations[k].chosen_filters_on_top !== false;
345+
hide_zero_doc_count = aggregations[k].hide_zero_doc_count || false;
344346
}
345347

346348
let buckets = _.chain(v)
@@ -352,12 +354,19 @@ const getBuckets = function(data, input, aggregations) {
352354
filters = input.filters[k];
353355
}
354356

357+
let doc_count = v2[1].array().length;
358+
359+
if (hide_zero_doc_count && doc_count === 0) {
360+
return;
361+
}
362+
355363
return {
356364
key: v2[0],
357-
doc_count: v2[1].array().length,
365+
doc_count: doc_count,
358366
selected: filters.indexOf(v2[0]) !== -1
359367
};
360368
})
369+
.compact()
361370
.value();
362371

363372
let iteratees;

tests/facetSortingSpec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,5 +193,25 @@ describe('facet sorting', function() {
193193

194194
done();
195195
});
196+
197+
it('excludes filters with zero doc_count if hide_zero_doc_count is true', function test(done) {
198+
199+
const result = require('./../index')(items, {
200+
aggregations: {
201+
genres: {
202+
hide_zero_doc_count: true
203+
}
204+
}
205+
}).aggregation({
206+
name: 'genres',
207+
filters: {
208+
genres: ['Western']
209+
}
210+
});
211+
212+
assert.deepEqual(result.data.buckets.map(v => v.key), ['Western']);
213+
214+
done();
215+
});
196216
});
197217

0 commit comments

Comments
 (0)