Gravatar for abhishek.shrivastava@towerswatson.com

Question by abhisfortitude, May 11, 2015 2:19 PM

How to capture the Facet Range selection event?

I have a requirement where in if a Facet Range (Unique ID: TWDisplayDateComponent) values are used to filter search results, the sorting should change from Relevancy to Field Sort which we have configured.

Below is the code that I am using within CoveoSearch.ascx. I have looked at the view source and verified that the field name rendered is correct (<%= ToCoveoFieldName("display-date") %>')

    Coveo.$(function() {
        Coveo.$('#TWDisplayDateComponent').on('state:change:f:<%= ToCoveoFieldName("display-date") %>', function (e, data)           
     {             
            alert('q has changed, the new value is: ' + data.value);
            Coveo.$('.CoveoSort').eq(1).coveo('select');
        });

        //Coveo.$('#search').on('state:change:q', function (e, data) {                
        //    alert('q has changed, the new value is: ' + data.value);
        //    Coveo.$('.CoveoSort').eq(1).coveo('select');
        //});
        Coveo.$('#search').coveoForSitecore('init', CoveoForSitecore.componentsOptions);            
    });

However the state change is not triggered when I select the "TWDisplayDateComponent" facet values. What am I doing wrong here?

P.S. For my verification, I used the commented code, I am able to change the sorting component on-the-fly when the query changes, so changing the sorting works, however the event that is supposed to the trigger state change on the Facet Range component is not being triggered.

1 Reply
Gravatar for abhishek.shrivastava@towerswatson.com

Answer by abhisfortitude, May 11, 2015 3:13 PM

I was able to resolve this on my own. Instead of capturing the state change of Facet Range component, I was instead capturing the state change event on the field configured to Facet Range component. Below is what I used:

  Coveo.$('#search').on('state:change:f:TWDisplayDateComponent', function (e, data) {                  
                    alert('Facet State has changed, the new value is: ' + data.value);
                    Coveo.$('.CoveoSort').eq(1).coveo('select');         //eq(1) means that the 2nd sort component that is configured       
            });

Hope this helps for someone who has similar issue.

Ask a question