Instructor: Time to add our first refinement widget. Just before doing so, we have a little layout work to do so that everything displays nicely on the page.
On the index.html page, we'll split the main area into two parts, the left column that will contain the refinement widgets and the right column for the hits and pagination. We add a div with an ID of left-column and a div with an ID of right-column, and copy-pasting the hits div in the right column.
Now, back to the left column, we can add our new widget container, a div with a class of filter widgets and an ID of brand. This is done. Let's head back to the other JS file to set up our new widget.
Same process as the previous widget. Search @widget, instant search widgets. We'll add a refinement list widget. Refinement list allows us to filter on a given attribute, but unlike the menu widget, you can select multiple facets. This widget is typically displayed with check boxes.
The container is the brand ID. Now, if we refresh the page, we'll get an error. Our refinement widget needs an attribute to filter on. We configure it with the attribute name key. Adding the attribute name for the value, you might have guessed it already, it's brand. Reloading.
We now have a nice refinement list widget that allows us to select one or multiple brands. It's missing a header to let the user know what the refinement is about, so templates, header, and brand. That's better.
Another nice option of this widget is the ability it gives us to search for facet values. This comes very handy when a specific facet can have a large amount of different values, which makes it unpractical to display all of them.
To solve that, when the search for a facet value is enabled, it will display a small search field in the widget itself. Let's add it, search for facet value key, and we set it to true. Saving and reloading.
We can now see the new search input. Let's type in A-M-A-Z, for instance. We now have facet values matching the query and even have some nice highlighting on them. Pretty cool. To make it even more user friendly, we can specify a placeholder for the input. Let's add search for brands.
A final point to cover for this widget is how the facets are sorted and displayed. By default, when none are selected, we can see that they are ordered by the number of results that would match this filter in descending order. If we refine on one value, it is then put at the top of the list.
If we had facets with a similar count, then the two would be ordered alphabetically. To make changes to that order, we can use the "Sort by" option, passing it an array of strings. Here, to reproduce the default behavior, we would add the following.
First, on top, the refine value, so isRefined. Then the count in descending order. Finally, name in descending order from A to Z. When we reload the page, nothing changes.
Now, let's tweak that setting to see how it behaves. If we remove the isRefined and reload the page, when we select a facet, it now stays in place. It's not pushed anymore to the top of the list. Adding the name in first position, the values are now ordered alphabetically.