Signature
Import
About this widget
Thestats
widget displays the total number of matching hits and the time it took to get them (time spent in the Algolia server).
Examples
JavaScript
Options
The CSS Selector or
HTMLElement
to insert the widget into.The CSS classes you can override:
root
. The root element of the widget.text
. The text element.
JavaScript
Templates
You can customize parts of a widget’s UI using the Templates API. Each template includes anhtml
function,
which you can use as a tagged template.
This function safely renders templates as HTML strings and works directly in the browser—no build step required.
For details, see Templating your UI.
The
html
function is available in InstantSearch.js version 4.46.0 or later.The template to use to customize the text. It exposes:
hasManyResults: boolean
. Indicates whether the search has more than one result.hasNoResults: boolean
. Indicates whether the search has no results.hasOneResult: boolean
. Indicates whether the search has one result.areHitsSorted: boolean
. Indicates whether Relevant sort is applied to the result.nbSortedHits: number
. The number of sorted hits from Relevant sort.hasManySortedResults: boolean
. Indicates whether the search has more than one sorted result.hasNoSortedResults: boolean
. Indicates whether the search has no sorted results.hasOneSortedResults: boolean
. Indicates whether the search has one sorted result.hitsPerPage: number
. Maximum number of hits returned per page.nbHits: number
. The number of hits matched by the query.nbPages: number
. The number of returned pages. Calculation is based on the total number of hits (nbHits
) divided by the number of hits per page (hitsPerPage
), rounded up to the nearest integer.page: number
. The position of the current page (zero-based).processingTimeMS: number
. The time the server took to process the request, in milliseconds. This doesn’t include network time.query: string
. The query sent to the server.cssClasses: object
. Same option as thecssClasses
parameter provided to the widget.
HTML output
HTML
Customize the UI with connectStats
If you want to create your own UI of the stats
widget, you can use connectors.
To use connectStats
, you can import it with the declaration relevant to how you installed InstantSearch.js.
JavaScript
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).
JavaScript
Rendering options
The maximum number of hits returned per page.
JavaScript
The number of hits matched by the query.
JavaScript
Indicates whether relevant sort is applied to the result.
JavaScript
The number of sorted hits from relevant sort.
JavaScript
The number of returned pages.
The calculation is based on the total number of hits (
nbHits
) divided by the number of hits per page (hitsPerPage
),
rounded up to the nearest integer.JavaScript
The position of the current page (zero-based).
JavaScript
The time the server took to process the request, in milliseconds.
This doesn’t include network time.
JavaScript
The query sent to the server.
JavaScript
All original widget options forwarded to the render function.
JavaScript
Create and instantiate the custom widget
First, create your custom widgets using a rendering function. Then, instantiate them with parameters. There are two kinds of parameters you can pass:- Instance parameters. Predefined options that configure Algolia’s behavior.
- Custom parameters. Parameters you define to make the widget reusable and adaptable.
renderFunction
, both instance and custom parameters are accessible through connector.widgetParams
.
JavaScript