Instantsearch.js: hits-per-page-selector as buttons

Created on 5 Mar 2018  路  2Comments  路  Source: algolia/instantsearch.js

Hi!
I need to create 2 buttons switching between 2 values of hits per page. Is it even possible? And if yes, how to make buttons like that?

I want to have 2 buttons. One with 24 hits per page and one with 96 hits per page. Not <select>.

Question

Most helpful comment

Wow. Thank you so much. It works great!

All 2 comments

// custom `renderFn` to render the custom HitsPerPage widget
function renderFn(HitsPerPageRenderingOptions, isFirstRendering) {
  var containerNode = HitsPerPageRenderingOptions.widgetParams.containerNode;
  var items = HitsPerPageRenderingOptions.items;
  var refine = HitsPerPageRenderingOptions.refine;

  const itemsHTML = items.map(({ value, label, isRefined }) => {
    const button = document.createElement("button");
    button.addEventListener("click", () => refine(value));
    button.className = isRefined ? "refined" : "";
    button.textContent = label + (isRefined ? " (selected)" : "");
    return button;
  });

  containerNode.innerText = "";
  itemsHTML.forEach(item => containerNode.appendChild(item));
}

// connect `renderFn` to HitsPerPage logic
var customHitsPerPage = instantsearch.connectors.connectHitsPerPage(renderFn);

// mount widget on the page
search.addWidget(
  customHitsPerPage({
    containerNode: document.getElementById("custom-hits-per-page-container"),
    items: [
      { value: 24, label: "24 per page", default: true },
      { value: 96, label: "96 per page" },
    ],
  })
);

see also the docs (especially the example at the bottom) here. I also made this into a demo: here

Wow. Thank you so much. It works great!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

samouss picture samouss  路  3Comments

jvreeken picture jvreeken  路  3Comments

francoischalifour picture francoischalifour  路  3Comments

Spone picture Spone  路  3Comments

ChristopherDosin picture ChristopherDosin  路  4Comments