ais-infinite-hits
<ais-infinite-hits // Optional parameters :escapeHTML="boolean" :show-previous="boolean" :class-names="object" :transform-items="function" :cache="object" />
1
2
3
4
5
6
7
8
9
import { AisInfiniteHits } from 'vue-instantsearch';
// Use 'vue-instantsearch/vue3/es' for Vue 3
export default {
components: {
AisInfiniteHits
},
// ...
};
1. Follow additional steps in Optimize build size to ensure your code is correctly bundled.
2. This imports all the widgets, even the ones you don’t use. Read the Getting started guide for more information.
About this widget
The ais-infinite-hits
widget displays a list of results with a “Show more” button at the bottom of the list. As an alternative to this approach, the infinite scroll guide describes how to create an automatically-scrolling infinite hits experience.
To configure the number of hits to show, use the ais-hits-per-page
or the ais-configure
widget.
If there are no hits, you should display a message to users and clear filters so they can start over.
Examples
1
<ais-infinite-hits />
Props
Parameter | Description | ||
---|---|---|---|
escapeHTML
|
type: boolean
default: true
Optional
Whether to escape the raw HTML in the hits. |
||
Copy
|
|||
show-previous
|
type: boolean
default: false
Optional
Enable the button to load previous results.
The button is only displayed if the routing option is enabled in |
||
Copy
|
|||
class-names
|
type: object
default: {}
Optional
The CSS classes you can override:
|
||
Copy
|
|||
transform-items
|
type: function
default: items => items
Optional
Receives the items and is called before displaying them. It returns a new array with the same “shape” as the original. This is helpful when transforming, removing, or reordering items. The complete If you’re transforming an attribute with the When using an array, take steps to avoid creating infinite loops. When you use an array as a prop, it causes the widget to re-register on every render, and this can sometimes cause these infinite loops. |
||
Copy
|
|||
cache
|
type: object
default: in-memory cache object
Optional
The widget caches all loaded hits. By default, it uses its own internal in-memory cache implementation. Alternatively, use You can also implement your own cache object with |
||
Copy
|
Customize the UI
Parameter | Description | ||
---|---|---|---|
default
|
The slot to override the complete DOM output of the widget. Note that when you implement this slot, none of the other slots will change the output, as the default slot surrounds them. Scope
|
||
Copy
|
|||
loadPrevious
|
The slot to override the DOM output of the “Show previous” button. Scope
|
||
Copy
|
|||
item
|
The slot to override the DOM output of the item. Scope
|
||
Copy
|
|||
loadMore
|
The slot to override the DOM output of the “Show more” button. Scope
|
||
Copy
|
HTML output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<div class="ais-InfiniteHits">
<button class="ais-InfiniteHits-loadPrevious">
Show previous results
</button>
<ol class="ais-InfiniteHits-list">
<li class="ais-InfiniteHits-item">
...
</li>
<li class="ais-InfiniteHits-item">
...
</li>
<li class="ais-InfiniteHits-item">
...
</li>
</ol>
<button class="ais-InfiniteHits-loadMore">
Show more results
</button>
</div>
Click and conversion events
If the insights
option is true
, the ais-infinite-hits
widget automatically sends a click
event with the following “shape” to the Insights API when a user clicks on a hit.
1
2
3
4
5
6
7
8
9
{
eventType: 'click',
insightsMethod: 'clickedObjectIDsAfterSearch',
payload: {
eventName: 'Hit Clicked',
// …
},
widgetType: 'ais.infiniteHits',
}
To customize this event, use the sendEvent
function in your item
slot and send a custom click
event.
1
2
3
4
5
6
7
8
9
10
<ais-infinite-hits>
<template v-slot:item="{ item, sendEvent }">
<div @click="sendEvent('click', item, 'Product Clicked')">
<h2>
<ais-highlight attribute="name" :hit="item" />
</h2>
<p>{{ item.description }}</p>
</div>
</template>
</ais-infinite-hits>
The sendEvent
function also accepts an object as a fourth argument to send directly to the Insights API. You can use it, for example, to send special conversion
events with a subtype.
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
27
28
29
30
31
32
33
<ais-infinite-hits>
<template v-slot:item="{ item, sendEvent }">
<div>
<h2>
<ais-highlight attribute="name" :hit="item" />
</h2>
<p>{{ item.description }}</p>
<button
@click="sendEvent('conversion', hit, 'Added To Cart', {
// Special subtype
eventSubtype: 'addToCart',
// An array of objects representing each item added to the cart
objectData: [
{
// The discount value for this item, if applicable
discount: item.discount || 0,
// The price value for this item (minus the discount)
price: item.price,
// How many of this item were added
quantity: 2,
},
],
// The total value of all items
value: item.price * 2,
// The currency code
currency: 'USD',
})"
>
Add to cart
</button>
</div>
</template>
</ais-hits>
Fields representing monetary values accept both numbers and strings, in major currency units (for example, 5.45
or '5.45'
). To prevent floating-point math issues, use strings, especially if you’re performing calculations.