Signature
About this widget
ThesortBy
widget displays a list of indices,
allowing a user to change the way hits are sorted (with replica indices).
Another common use case is to let users switch between different indices.
For this widget to work, you must define all indices that you pass down as options as replicas of the main index.
Examples
JavaScript
Options
The CSS Selector or
HTMLElement
to insert the widget into.The list of indices to search in, with each item:
label: string
. The label of the index to display.value: string
. The name of the index to target.
JavaScript
The CSS classes you can override:
root
. The root element of the widget.select
. Theselect
element.option
. Theoption
elements of theselect
.
JavaScript
A function that receives the list of items before they are displayed.
It should return a new array with the same structure.
Use this to transform, filter, or reorder the items.The function also has access to the full
results
data,
including all standard response parameters
and parameters from the helper,
such as disjunctiveFacetsRefinements
.JavaScript
HTML output
HTML
Customize the UI with connectSortBy
If you want to create your own UI of the sortBy
widget, you can use connectors.
To use connectSortBy
, 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 list of items the widget can display, with each item:
label: string
. The label of the index to display.value: string
. The name of the index to target.
JavaScript
The currently selected index.
JavaScript
Whether the search can be refined.
JavaScript
Switches indices and triggers a new search.
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
Instance options
The list of indices to search in, with each item:
label: string
. The label of the index to display.value: string
. The name of the index to target.
JavaScript
A function that receives the list of items before they are displayed.
It should return a new array with the same structure.
Use this to transform, filter, or reorder the items.The function also has access to the full
results
data,
including all standard response parameters
and parameters from the helper,
such as disjunctiveFacetsRefinements
.JavaScript