Gravatar for evillarino@leviton.com

Question by EVillarino, Jan 21, 2019 7:25 PM

Hiding facets if only one value is available

Is it possible to hide a facet from the UI if only one facet value exists? If there is only one facet value in a facet our users feel it is not need to show the facet.

Gravatar for evillarino@leviton.com

Comment by EVillarino, Jan 22, 2019 9:49 PM

I was able to retrieve a facet via code using the jsui - however, there does not appear to be a getfacetvalues method. Everything seems to be dependent on the state of the facet. We have a requirement to keep the facets collapsed. I have tested where I know I have one value in a facet, but when collapsed getdisplayedvalues returns zero. when expanded it returns one. I understand displayed implies what is shown on screen, but there is no method to get the values regardless of facet state.

Is there a workaround for this?

2 Replies
Gravatar for olamothe@coveo.com

Answer by olamothe, Jan 23, 2019 2:48 PM

You can do

facetInstance.values.getAll()
Gravatar for evillarino@leviton.com

Comment by EVillarino, Jan 24, 2019 8:59 PM

i've tried this, and when the facet is collapsed, it returns one value in some cases, although there are many move values available. here is how my facet is defined.

<div data-title="Amperage" data-field="@tsa_amperage" id="tsa_amperage" data-allowed-values="3 A,5 A,10 A ..." data-number-of-values="0" data-tab="Products, All" data-enable-facet-search="false" data-enable-settings="false" data-page-size="100"></div>
Gravatar for jflheureux@coveo.com

Answer by Jean-François L'Heureux, Jan 23, 2019 2:50 PM

It is quite easy to know how many facet values are displayed (or hidden when collapsed) in a facet using JavaScript:

document.querySelectorAll('#YourFacetId .coveo-facet-value:not(.coveo-facet-search-button)').length

You could register a `deferredQuerySuccess` (doc) event handler to check the number of values and collapse/expand or hide/display the facets. Make sure to register your event handler inside an `afterInitialization` (doc) event handler. This ensures the facets update their DOM before your event handler is executed.

Gravatar for evillarino@leviton.com

Comment by EVillarino, Jan 24, 2019 8:58 PM

This seems to only work when expanded - this is how I am defining the markup of the coveofacet, could one of these be interfering?

<div class="CoveoFacet"  
 data-title="Amperage"  
 data-field="@tsa_amperage"  id="tsa_amperage"
 data-allowed-values="3 A,5 A,10 A ..."  
 data-number-of-values="0" 
 data-tab="Products, All" data-enable-facet-search="false" data-enable-settings="false" data-page-size="100"></div>
Ask a question