Create Autocomplete template
Attributes
The following data-* attributes are required by the library to handle attributions (click events) for products/keywords/history items rendered in the autocomplete result:
data-ns-hit
This attribute should be used on clickable keyword, product, history list elements. This attribute handles submit keyword/history as search, redirect to product, analytics (if enabled) request.
Following table shows value for this attribute depending on the rendering context.
keyword
value from response.data.search.keywords
Code example:
const { keywords } = response.data.search
const contentToRender = keywords.map(keyword =>
`
<div data-ns-hit="${JSON.stringify(keyword)}" ....>
....
....
</div>
`
)Value example:
{
"keyword": "midi dresses",
"_highlight": { "keyword": "midi <strong>dress</strong>es" }
}product
productId and url from response.data.search.products.hits
Code example:
const { hits } = response.data.search.products
const contentToRender = hits.map(({ productId, url }) =>
`
<div data-ns-hit="${JSON.stringify({ productId, url })}" ....>
....
....
</div>
`
)Value example:
{
"productId": 123456,
"url": "https://example.com/products/example-product-handle"
}history
historyEnabled & historySize config. Refer Initialization
data-ns-remove-history
This attribute should be used to delete history entries in the autocomplete
To make an element delete a single history entry when clicked, add data-ns-remove-history={hit.item} to an element. In order to delete all history entries, add data-ns-remove-history="all" to clear button.
Starter templates
This section provides links to default startup templates for different rendering frameworks. These templates can be copied and customized as needed.
Last updated
Was this helpful?