Signature
Import
About this widget
TheratingMenu
widget lets users refine search results by clicking on stars.
The stars are based on the selected attribute.
Requirements
Theattribute
provided to the widget must be in attributes for faceting,
either on the dashboard or using the attributesForFaceting
parameter with the API.
Attribute values must be integers, not strings or floats.
The widget only returns exact numerical matches.
For example, if a user selects “4 [stars] & Up”,
only return records with values such as 4 or 5 are returned—
not those with values like 4.5 or 4.7.
If your attribute is a float, index a new attribute with the rounded integer value to use in this widget.
Examples
JavaScript
Options
The CSS Selector or
HTMLElement
to insert the widget into.The name of the attribute in the record.
JavaScript
The maximum value for the rating.
This value is exclusive, which means the number of stars will be the provided value, minus one.
JavaScript
The CSS classes you can override:
root
. The root element of the widget.noRefinementRoot
. Container class without results.list
. The list of results.item
. The list items.selectedItem
. The selected item in the list.disabledItem
. The disabled item in the list.starIcon
. The default class of each star (when using the default template).fullStarIcon
. The class of each full star (when using the default template).emptyStarIcon
. The class of each empty star (when using the default template).label
. The label of each item.count
. The count of each item.
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 used for displaying each item, with:
count: number
. The number of results that match this refinement.isRefined: boolean
. Whether the refinement is selected.name: string
. The name corresponding to the number of stars.value: string
. The number of stars with a string form.url: string
. The value of the URL with the applied refinement.labels: object
. The value of the other templates.cssClasses: object
. The CSS classes provided to the widget.stars: boolean[]
. The list of stars to generate with:true
. Represents a filled starfalse
. Represents an empty star
HTML output
HTML
Customize the UI with connectRatingMenu
If you want to create your own UI of the ratingMenu
widget, you can use connectors.
To use connectRatingMenu
, 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
Render options
This list of stars to display, with:
count: number
. The number of results that match this refinement.isRefined: boolean
. Whether the refinement is selected.name: string
. The name corresponding to the number of stars.value: string
. The number of stars with a string form.stars: boolean[]
. The list of stars to generate with:true
. Represents a filled starfalse
. Represents an empty star
JavaScript
Indicates if search state can be refined.
JavaScript
Selects a rating to filter the results.
Takes the value of an item as parameter.
JavaScript
The function to send
click
events.
The click
event is automatically sent when refine
is called.
To learn more, see the insights
middleware.eventType: 'click'
facetValue: string
JavaScript
Generates a URL for the next state.
Takes the value of an item as parameter.
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
Instance options
The name of the attribute in the record.
JavaScript
The maximum value for the rating.
JavaScript