queryRuleCustomData
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 |
||
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
Transforms the items passed to the templates. |
||
Copy
|
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.
Copy
|
||
Copy
|
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. |
||
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 = instantsearch.connectors.connectQueryRules(
renderQueryRuleCustomData
);
search.addWidgets([
customQueryRuleCustomData({
// Optional parameters
transformItems: function,
})
]);
Instance options
transformItems
|
type: function
Optional
Transforms the items passed to the templates. |
||
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 = instantsearch.connectors.connectQueryRules(
renderQueryRuleCustomData
);
// Instantiate the custom widget
search.addWidgets([
customQueryRuleCustomData({
container: document.querySelector('#queryRuleCustomData'),
})
]);