API Reference / InstantSearch.js / queryRuleCustomData
Signature
queryRuleCustomData({
  container: string|HTMLElement,
  // Optional parameters
  templates: object,
  cssClasses: object,
  transformItems: function,
});
Import
1
import { queryRuleCustomData } from 'instantsearch.js/es/widgets';

About this widget

The queryRuleCustomData widget displays custom data from Rules.

You may want to use this widget to display banners or recommendations returned by Rules, and that match search parameters.

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
queryRuleCustomData({
  container: '#queryRuleCustomData',
  templates: {
    default({ items }, { html }) {
      return html`
        ${items.map(item => {
          const { title, banner, link } = item;

          if (!banner) {
            return;
          }

          return `
            <div>
              <h2>${title}</h2>
              <a href="${link}">
                <img src="${banner}" alt="${title}">
              </a>
            </div>
          `;
        }).join('')}
      `;
    },
  }
});

Options

container
type: string|HTMLElement
Required

The CSS Selector or HTMLElement to insert the widget into.

1
2
3
queryRuleCustomData({
  container: '#queryRuleCustomData',
});
templates
type: object
Optional

The templates to use for the widget.

1
2
3
4
5
6
queryRuleCustomData({
  // ...
  templates: {
    // ...
  }
});
cssClasses
type: object
Optional

The CSS classes to override.

  • root: the root element of the widget.
1
2
3
4
5
6
queryRuleCustomData({
  // ...
  cssClasses: {
    root: 'MyCustomQueryRuleCustomData',
  },
});
transformItems
type: function
Optional

Receives the items and is called before displaying them. Should return a new array with the same shape as the original array. Useful for transforming, removing, or reordering items.

In addition, the full results data is available, which includes all regular response parameters, as well as parameters from the helper (for example disjunctiveFacetsRefinements).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
queryRuleCustomData({
  // ...
  transformItems(items) {
    return items.filter(item => typeof item.banner !== 'undefined');
  },
});

/* or, combined with results */
queryRuleCustomData({
  // ...
  transformItems(items, { results }) {
    return items.map(item => ({
      ...item,
      visible: results.page === 0,
    }));
  },
});

Templates

You can customize parts of the widget’s UI using the Templates API.

Every template provides an html function you can use as a tagged template. Using html lets you safely provide templates as an HTML string. It works directly in the browser without a build step. See Templating your UI for more information.

The html function is available starting from v4.46.0.

default
type: function
Optional

The template to use for the custom data. It exposes the items returned by the Rules.

The following example assumes a Rule returned this custom data.

1
2
3
4
5
{
  "title": "This is an image",
  "banner": "image.png",
  "link": "https://website.com/"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
queryRuleCustomData({
  // ...
  templates: {
    default({ items }, { html }) {
      return html`
        ${items.map(item => {
          const { title, banner, link } = item;

          if (!banner) {
            return null;
          }

          return `
            <div>
              <h2>${title}</h2>
              <a href="${link}">
                <img src="${banner}" alt="${title}">
              </a>
            </div>
          `;
        }).join('')}
      `;
    },
  },
});

HTML output

1
<div class="ais-QueryRuleCustomData"></div>

Customize the UI with connectQueryRules

If you want to create your own UI of the queryRuleCustomData widget, you can use connectors.

This connector is also used to build other widgets: QueryRuleContext

To use connectQueryRules, you can import it with the declaration relevant to how you installed InstantSearch.js.

1
import { connectQueryRules } from 'instantsearch.js/es/connectors';

Then it’s a 3-step process:

// 1. Create a render function
const renderQueryRuleCustomData = (renderOptions, isFirstRender) => {
  // Rendering logic
};

// 2. Create the custom widget
const customQueryRuleCustomData = connectQueryRules(
  renderQueryRuleCustomData
);

// 3. Instantiate
search.addWidgets([
  customQueryRuleCustomData({
    // instance params
  })
]);

Create a render function

This rendering function is called before the first search (init lifecycle step) and each time results come back from Algolia (render lifecycle step).

const renderQueryRuleCustomData = (renderOptions, isFirstRender) => {
  const {
    object[] items,
    object widgetParams,
  } = renderOptions;

  if (isFirstRender) {
    // Do some initial rendering and bind events
  }

  // Render the widget
}

Rendering options

items
type: object[]

The items that matched the Rule.

1
2
3
4
5
6
7
8
9
const renderQueryRuleCustomData = (renderOptions, isFirstRender) => {
  const { items } = renderOptions;

  document.querySelector('#queryRuleCustomData').innerHTML = `
    <ul>
      ${items.map(item => `<li>${item.title}</li>`).join('')}
    </ul>
  `;
};
widgetParams
type: object

All original widget options forwarded to the render function.

1
2
3
4
5
6
7
8
9
10
11
12
13
const renderQueryRuleCustomData = (renderOptions, isFirstRender) => {
  const { widgetParams } = renderOptions;

  widgetParams.container.innerHTML = '...';
};

// ...

search.addWidgets([
  customQueryRuleCustomData({
    container: document.querySelector('#queryRuleCustomData'),
  })
]);

Create and instantiate the custom widget

We first create custom widgets from our rendering function, then we instantiate them. When doing that, there are two types of parameters you can give:

  • Instance parameters: they are predefined parameters that you can use to configure the behavior of Algolia.
  • Your own parameters: to make the custom widget generic.

Both instance and custom parameters are available in connector.widgetParams, inside the renderFunction.

const customQueryRuleCustomData = connectQueryRules(
  renderQueryRuleCustomData
);

search.addWidgets([
  customQueryRuleCustomData({
    // Optional parameters
    transformItems: function,
  })
]);

Instance options

transformItems
type: function
Optional

Receives the items and is called before displaying them. Should return a new array with the same shape as the original array. Useful for transforming, removing, or reordering items.

In addition, the full results data is available, which includes all regular response parameters, as well as parameters from the helper (for example disjunctiveFacetsRefinements).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
customQueryRuleCustomData({
  transformItems(items) {
    return items.filter(item => typeof item.banner !== 'undefined');
  },
});

/* or, combined with results */
customQueryRuleCustomData({
  transformItems(items, { results }) {
    return items.map(item => ({
      ...item,
      visible: results.page === 0,
    }));
  },
});

Full example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Create the render function
const renderQueryRuleCustomData = (renderOptions, isFirstRender) => {
  const { items, widgetParams } = renderOptions;

  if (isFirstRender) {
    return;
  }

  widgetParams.container.innerHTML = `
    <ul>
      ${items.map(item => `<li>${item.title}</li>`).join('')}
    </ul>
  `;
};

// Create the custom widget
const customQueryRuleCustomData = connectQueryRules(
  renderQueryRuleCustomData
);

// Instantiate the custom widget
search.addWidgets([
  customQueryRuleCustomData({
    container: document.querySelector('#queryRuleCustomData'),
  })
]);
Did you find this page helpful?