trendingItems
trendingItems({ container: string|HTMLElement, // Optional parameters facetName: string, facetValue: string, limit: number, threshold: number, queryParameters: object, fallbackParameters: object, escapeHTML: boolean, templates: object, cssClasses: object, transformItems: function, });
1
import { trendingItems } from 'instantsearch.js/es/widgets';
About this widget
Use the trendingItems
widget to display a list of trending items.
If you’re using the deprecated recommend-js
UI library, please refer to the API reference for the trendingItems
function.
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
trendingItems({
container: '#trendingItems',
facetName: 'category',
facetValue: 'Book',
templates: {
item(recommendation, { html }) {
return html`
<h2>${recommendation.name}</h2>
<p>${recommendation.description}</p>
`;
},
},
});
With a carousel layout
Display the trendingItems
widget as a scrollable carousel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { carousel } from 'instantsearch.js/es/templates';
// or
const { carousel } = instantsearch.templates;
trendingItems({
container: '#trendingItems',
facetName: 'category',
facetValue: 'Book',
templates: {
item(recommendation, { html }) {
return html`
<h2>${recommendation.name}</h2>
<p>${recommendation.description}</p>
`;
},
layout: carousel(),
},
});
Options
container
|
type: string|HTMLElement
Required
The CSS Selector or |
||
Copy
|
|||
facetName
|
type: string
Optional
The facet attribute to get recommendations for. This parameter should be used along with |
||
Copy
|
|||
facetValue
|
type: string
Optional
The value for the targeted facet name. This parameter should be used along with |
||
Copy
|
|||
limit
|
type: number
The number of recommendations to retrieve. Depending on the available recommendations and the other request parameters, the actual number of items may be lower than that.
If |
||
Copy
|
|||
threshold
|
type: number
The threshold for the recommendations confidence score (between 0 and 100). Only recommendations with a greater score are returned. |
||
Copy
|
|||
queryParameters
|
type: Omit<SearchParameters, 'page' | 'hitsPerPage' | 'offset' | 'length'>
List of search parameters to send. |
||
Copy
|
|||
fallbackParameters
|
type: Omit<SearchParameters, 'page' | 'hitsPerPage' | 'offset' | 'length'>
List of search parameters to send as additional filters when there aren’t enough recommendations. |
||
Copy
|
|||
escapeHTML
|
type: boolean
default: true
Optional
Escapes HTML entities from recommendations string values. |
||
Copy
|
|||
templates
|
type: object
Optional
The templates to use for the widget. |
||
Copy
|
|||
cssClasses
|
type: object
default: {}
Optional
The CSS classes you can override:
|
||
Copy
|
|||
transformItems
|
type: function
default: items => items
Optional
Receives the recommendations and is called before displaying them. It returns a new array with the same “shape” as the original. This is helpful when transforming or reordering items. The entire |
||
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.
empty
|
type: string|function
Optional
The template to use when there are no recommendations. |
||
Copy
|
|||
header
|
type: string|function
Optional
The template to use for the recommendations header. This template receives the recommendations as well as the |
||
Copy
|
|||
item
|
type: string|function
Optional
The template to use for each recommendation. This template receives an object containing a single record. |
||
Copy
|
|||
layout
|
since: v4.74.0
type: function
Optional
The template to use to wrap all items. InstantSearch.js provides an out-of-the-box carousel layout template with the following options:
|
||
Copy
|
HTML output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<section class="ais-TrendingItems">
<h3 class="ais-TrendingItems-title">Trending Items</h3>
<div class="ais-TrendingItems-container">
<ol class="ais-TrendingItems-list">
<li class="ais-TrendingItems-item">
...
</li>
<li class="ais-TrendingItems-item">
...
</li>
<li class="ais-TrendingItems-item">
...
</li>
</ol>
</div>
</section>
Customize the UI with connectTrendingItems
If you want to create your own UI of the trendingItems
widget, you can use connectors.
To use connectTrendingItems
, you can import it with the declaration relevant to how you installed InstantSearch.js.
1
import { connectTrendingItems } from 'instantsearch.js/es/connectors';
Then it’s a 3-step process:
// 1. Create a render function
const renderTrendingItems = (renderOptions, isFirstRender) => {
// Rendering logic
};
// 2. Create the custom widget
const customTrendingItems = connectTrendingItems(
renderTrendingItems
);
// 3. Instantiate
search.addWidgets([
customTrendingItems({
// 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 renderTrendingItems = (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 matched recommendations from the Algolia API. |
||
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 customTrendingItems = connectTrendingItems(
renderTrendingItems
);
search.addWidgets([
customTrendingItems({
// Optional parameters
facetName: string,
facetValue: string,
limit: number,
threshold: number,
queryParameters: object,
fallbackParameters: object,
escapeHTML: boolean,
transformItems: function,
})
]);
Instance options
facetName
|
type: string
Optional
The facet attribute to get recommendations for. This parameter should be used along with |
||
Copy
|
|||
facetValue
|
type: string
Optional
The value for the targeted facet name. This parameter should be used along with |
||
Copy
|
|||
limit
|
type: number
The number of recommendations to retrieve. Depending on the available recommendations and the other request parameters, the actual number of items may be lower than that.
If |
||
Copy
|
|||
threshold
|
type: number
The threshold for the recommendations confidence score (between 0 and 100). Only recommendations with a greater score are returned. |
||
Copy
|
|||
queryParameters
|
type: Omit<SearchParameters, 'page' | 'hitsPerPage' | 'offset' | 'length'>
List of search parameters to send. |
||
Copy
|
|||
fallbackParameters
|
type: Omit<SearchParameters, 'page' | 'hitsPerPage' | 'offset' | 'length'>
List of search parameters to send as additional filters when there aren’t enough recommendations. |
||
Copy
|
|||
escapeHTML
|
type: boolean
default: true
Optional
Escapes HTML entities from recommendations string values. |
||
Copy
|
|||
transformItems
|
type: function
default: items => items
Optional
Receives the recommendations and is called before displaying them. It returns a new array with the same “shape” as the original. This is helpful when transforming or reordering items. The entire |
||
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
// Create the render function
const renderTrendingItems = (renderOptions, isFirstRender) => {
const { items, widgetParams } = renderOptions;
widgetParams.container.innerHTML = `
<ul>
${items
.map(item => `<li>${item.name}</li>`)
.join('')}
</ul>
`;
};
// Create the custom widget
const customTrendingItems = connectTrendingItems(
renderTrendingItems
);
// Instantiate the custom widget
search.addWidgets([
customTrendingItems({
container: document.querySelector('#trendingItems'),
})
]);