Gravatar for dsinclair@horizontalintegration.com

Question by Dan Sinclair, Nov 21, 2018 3:47 PM

Coveo for Sitecore keep page size when returning to results

When a user searches, changes the page size, clicks on a result, and then hits the back button to return to the search results, the page size is reset to the default.

Is there any way to persist the page size?

2 Replies
Gravatar for flguillemette@coveo.com

Answer by François Lachance-Guillemette, Nov 21, 2018 4:29 PM

The ResultsPerPage component does not allow this functionality.

But it is not impossible to do. I see two options for you, both applying the same principles:

  1. Forking the repository
  2. Creating a simple extension

If you have a look at how the Pager component handles its state, it adds an event handler on the model with:

this.bind.onQueryState(MODELEVENTS.CHANGEONE, QUERYSTATEATTRIBUTES.FIRST, (data: IAttributeChangedEventArg) =>
    this.handleQueryStateModelChanged(data)
);

And then changes the state inside the component to handle this change.

You could create a new attribute that would handle the results per page, say rpp.

This attribute needs to be registered in the QueryStateModel and can be done in the component's constructor:

const defaultAttributeValue = this.getInitialChoice();
this.queryStateModel.registerNewAttribute("rpp", defaultAttributeValue);

Then you can listen to this attribute using rpp instead of QUERY_STATE_ATTRIBUTES.FIRST in the first code block:

this.bind.onQueryState(MODELEVENTS.CHANGEONE, "rpp", …)

In your handleQueryStateModelChanged method, you can then use the attribute value to set the currentResultsPerPage property.

You would also need to update the query state when the new value is selected:

this.queryStateModel.set("rpp", this.currentResultPerPage");

----

If you go the extension way, the steps have to be executed using only public methods from the library.

You would need to register the attribute in a beforeInitialization event to ensure that the Search Interface understands your new attribute.

And then use a similar event handler to detect changes in the model for the rpp attribute.

You could also read the hash on page load, extract the rpp value and call setResultsPerPage directly on your ResultPerPage instance.

And maybe use a querySuccess event to get back the current results per page and set it back in the state.

----

Let me know if you decided to implement it!

Gravatar for flguillemette@coveo.com

Comment by François Lachance-Guillemette, Nov 26, 2018 6:58 PM

Quick update on this, there is currently an implementation being worked on with this pull request:

https://github.com/coveo/search-ui/pull/969

Gravatar for flguillemette@coveo.com

Answer by François Lachance-Guillemette, Dec 12, 2018 9:41 PM

The `2.4710.19` version now includes the change described in my previous answer.

You can update your Coveo JavaScript Search Framework CDN version by following these steps:

Using the CDN in Coveo for Sitecore Hive Framework

Instead of step 5 in the `Switching Between the CDN and Local Files` tutorial, change the `CDN version` attribute to `2.4710` and it will automatically use the latest hotfix for this version.

Ask a question