queryRuleCustomData
queryRuleCustomData({ container: string|HTMLElement, // Optional parameters templates: object, cssClasses: object, transformItems: function, });
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 |
||
Copy
|
|||
templates
|
type: object
Optional
The templates to use for the widget. |
||
Copy
|
|||
cssClasses
|
type: object
Optional
The CSS classes to override.
|
||
Copy
|
|||
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 |
||
Copy
|
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.
Copy
|
||
Copy
|
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. |
||
Copy
|
|||
widgetParams
|
type: object
All original widget options forwarded to the render function. |
||
Copy
|
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 |
||
Copy
|
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'),
})
]);