Gravatar for brhodes@coveo.com

Question by brhodes, Dec 11, 2017 2:58 PM

Automatically open the correct result tab depending on the results

Can Coveo automatically open the correct result tab depending on the results (e.g. if coverage for Term “X” is better for Tab A, then automatically show Tab A as the first Tab with results open instead of Tab B

1 Reply
Gravatar for flguillemette@coveo.com

Answer by François Lachance-Guillemette, Dec 11, 2017 9:51 PM

There is no automatic way to do that as far as I know.

It could be possible with a hook with an event like doneBuildingQuery though.

It requires you to manage by hand a list of keywords matching to tab, which is not optimal, but it could work.

function getSpecialTabForQuery(query) {
    // Handle your query here, and return the right tab ID.
    return "sometabid";
}
function mustChangeTab(searchInterface, targetTabId) {
    return targetTabId && Coveo.state(searchInterface, Coveo.QueryStateModel.attributesEnum.t) !== targetTabId;
}
function changeTab(searchInterface, targetTabId) {
    Coveo.state(searchInterface, Coveo.QueryStateModel.attributesEnum.t, targetTabId);
    // Defer it after the events are finished to fix some animation glitches.
    setTimeout(function() { Coveo.executeQuery(searchInterface); }, 0);
}
Coveo.$$(searchInterface).one(Coveo.QueryEvents.doneBuildingQuery, function(data) {
    const targetTabId = getSpecialTabForQuery(data.detail.queryBuilder.expression);
    if (mustChangeTab(searchInterface, targetTabId)) {
        data.detail.cancel = true;
        changeTab(searchInterface, targetTabId);
    }
});

Note that you must adapt this code to your needs:

It is only executed once since it is defined with `Coveo.$$.one`. If your page executes a query on load, `expression` will be empty, thus this code would be useless.

If you put `.on`, it completely overrides the current tab. Meaning that if I enter "fridge" and switch to the fridge tab, even if I click back to "All Content", this code will force a single tab for a given query, which I think is way more confusing than helping.

So it is not perfect, but it should give you the basic idea.

Ask a question