API Reference / InstantSearch.js Widgets / queryRuleCustomData
Widget signature
instantsearch.widgets.queryRuleCustomData({
  container: string|HTMLElement,
  // Optional parameters
  templates: object,
  cssClasses: object,
  transformItems: function,
});

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
instantsearch.widgets.queryRuleCustomData({
  container: '#queryRuleCustomData',
  templates: {
    default: `
      {{#items}}
        {{#banner}}
          <section>
            <h2>{{title}}</h2>
            <a href="{{link}}">
              <img src="{{banner}}" alt="{{title}}">
            </a>
          </section>
        {{/banner}}
      {{/items}}
    `,
  }
});

Options

container
type: string|HTMLElement
Required

The CSS Selector or HTMLElement to insert the widget into.

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

The templates to use for the widget.

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

The CSS classes to override.

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

Transforms the items passed to the templates.

1
2
3
4
5
6
instantsearch.widgets.queryRuleCustomData({
  // ...
  transformItems(items) {
    return items.filter(item => typeof item.banner !== 'undefined');
  },
});

Templates

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
instantsearch.widgets.queryRuleCustomData({
  // ...
  templates: {
    default: `
      {{#items}}
        {{#banner}}
          <section>
            <h2>{{title}}</h2>
            <a href="{{link}}">
              <img src="{{banner}}" alt="{{title}}">
            </a>
          </section>
        {{/banner}}
      {{/items}}
    `,
  },
});

HTML output

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

Customize the UI - 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

It’s a 3-step process:

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

// 2. Create the custom widget
const customQueryRuleCustomData = instantsearch.connectors.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 query 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 = instantsearch.connectors.connectQueryRules(
  renderQueryRuleCustomData
);

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

Instance options

transformItems
type: function
Optional

Transforms the items passed to the templates.

1
2
3
4
5
customQueryRuleCustomData({
  transformItems(items) {
    return items.filter(item => typeof item.banner !== 'undefined');
  },
});

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 = instantsearch.connectors.connectQueryRules(
  renderQueryRuleCustomData
);

// Instantiate the custom widget
search.addWidgets([
  customQueryRuleCustomData({
    container: document.querySelector('#queryRuleCustomData'),
  })
]);

Did you find this page helpful?