ais-hierarchical-menu
<ais-hierarchical-menu :attributes="string[]" // Optional parameters :limit="number" :show-more="boolean" :show-more-limit="number" separator="string" root-path="string" :show-parent-level="boolean" :sort-by="string[]|function" :transform-items="function" :class-names="object" />
1
2
3
4
5
6
7
8
9
import { AisHierarchicalMenu } from 'vue-instantsearch';
// Use 'vue-instantsearch/vue3/es' for Vue 3
export default {
components: {
AisHierarchicalMenu
},
// ...
};
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-hierarchical-menu
widget is used to create a navigation menu based on a hierarchy of facet attributes. It’s commonly used for categories with subcategories.
By default, the count of the refined root level is updated to match the count of the actively refined parent level. You can choose to keep the root level count intact by setting persistHierarchicalRootCount
in ais-instant-search
.
Requirements
The objects to use in the hierarchical menu must follow this structure:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[
{
"objectID": "321432",
"name": "lemon",
"categories.lvl0": "products",
"categories.lvl1": "products > fruits"
},
{
"objectID": "8976987",
"name": "orange",
"categories.lvl0": "products",
"categories.lvl1": "products > fruits"
}
]
You can also provide more than one path for each level:
1
2
3
4
5
6
7
8
[
{
"objectID": "321432",
"name": "lemon",
"categories.lvl0": ["products", "goods"],
"categories.lvl1": ["products > fruits", "goods > to eat"]
}
]
The attributes passed to the attributes
prop must be added in attributes for faceting, either on the dashboard) or using attributesForFaceting
with the API. By default, the separator is >
(with spaces), but you can use a different one by using the separator option.
Examples
1
2
3
4
5
6
7
8
<ais-hierarchical-menu
:attributes="[
'categories.lvl0',
'categories.lvl1',
'categories.lvl2',
'categories.lvl3',
]"
/>
Props
Parameter | Description | ||
---|---|---|---|
attributes
|
type: string[]
Required
The name of the attributes to generate the menu with. To avoid unexpected behavior, you can’t use the same |
||
Copy
|
|||
limit
|
type: number
default: 10
Optional
The number of facet values to retrieve. When you enable the |
||
Copy
|
|||
show-more
|
type: boolean
default: false
Optional
Whether to display a button that expands the number of items. |
||
Copy
|
|||
show-more-limit
|
type: number
default: 20
Optional
The maximum number of displayed items (only used when |
||
Copy
|
|||
separator
|
type: string
default: >
Optional
The level separator used in the records. |
||
Copy
|
|||
root-path
|
type: string
Optional
The prefix path to use if the first level is not the root level. Make sure to also include the root path in your UI state—for example, by setting |
||
Copy
|
|||
show-parent-level
|
type: boolean
default: true
Optional
Whether to show the siblings of the selected parent level of the current refined value. This option doesn’t impact the root level. All root items are always visible. |
||
Copy
|
|||
sort-by
|
type: string[]|function
default: Uses facetOrdering if set, ["name:asc"]
How to sort refinements. Must be one or more of the following strings:
You can also give a function that receives items two by two, like JavaScript’s If this facet’s |
||
Copy
|
|||
transform-items
|
type: function
Optional
Receives the items and is called before displaying them. Should return a new array with the same shape as the original array. This is helpful when transforming, removing, or reordering items. In addition, the full |
||
Copy
|
|||
class-names
|
type: object
Optional
The CSS classes you can override:
|
||
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
Where each item is an
|
||
Copy
|
|||
showMoreLabel
|
The slot to override the show more label Scope
|
||
Copy
|
HTML output
Default
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
<div class="ais-HierarchicalMenu">
<ul class="ais-HierarchicalMenu-list ais-HierarchicalMenu-list--lvl0">
<li class="ais-HierarchicalMenu-item ais-HierarchicalMenu-item--parent ais-HierarchicalMenu-item--selected">
<a class="ais-HierarchicalMenu-link ais-HierarchicalMenu-link--selected" href="#">
<span class="ais-HierarchicalMenu-label">Appliances</span>
<span class="ais-HierarchicalMenu-count">4,306</span>
</a>
<ul class="ais-HierarchicalMenu-list ais-HierarchicalMenu-list--child ais-HierarchicalMenu-list--lvl1">
<li class="ais-HierarchicalMenu-item ais-HierarchicalMenu-item--parent">
<a class="ais-HierarchicalMenu-link" href="#">
<span class="ais-HierarchicalMenu-label">Dishwashers</span>
<span class="ais-HierarchicalMenu-count">181</span>
</a>
</li>
<li class="ais-HierarchicalMenu-item">
<a class="ais-HierarchicalMenu-link" href="#">
<span class="ais-HierarchicalMenu-label">Fans</span>
<span class="ais-HierarchicalMenu-count">91</span>
</a>
</li>
</ul>
</li>
<li class="ais-HierarchicalMenu-item ais-HierarchicalMenu-item--parent">
<a class="ais-HierarchicalMenu-link" href="#">
<span class="ais-HierarchicalMenu-label">Audio</span>
<span class="ais-HierarchicalMenu-count">1,570</span>
</a>
</li>
</ul>
<button class="ais-HierarchicalMenu-showMore">Show more</button>
</div>
Show more disabled
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
<div class="ais-HierarchicalMenu">
<ul class="ais-HierarchicalMenu-list ais-HierarchicalMenu-list--lvl0">
<li class="ais-HierarchicalMenu-item ais-HierarchicalMenu-item--parent ais-HierarchicalMenu-item--selected">
<a class="ais-HierarchicalMenu-link ais-HierarchicalMenu-link--selected" href="#">
<span class="ais-HierarchicalMenu-label">Appliances</span>
<span class="ais-HierarchicalMenu-count">4,306</span>
</a>
<ul class="ais-HierarchicalMenu-list ais-HierarchicalMenu-list--child ais-HierarchicalMenu-list--lvl1">
<li class="ais-HierarchicalMenu-item ais-HierarchicalMenu-item--parent">
<a class="ais-HierarchicalMenu-link" href="#">
<span class="ais-HierarchicalMenu-label">Dishwashers</span>
<span class="ais-HierarchicalMenu-count">181</span>
</a>
</li>
<li class="ais-HierarchicalMenu-item">
<a class="ais-HierarchicalMenu-link" href="#">
<span class="ais-HierarchicalMenu-label">Fans</span>
<span class="ais-HierarchicalMenu-count">91</span>
</a>
</li>
</ul>
</li>
<li class="ais-HierarchicalMenu-item ais-HierarchicalMenu-item--parent">
<a class="ais-HierarchicalMenu-link" href="#">
<span class="ais-HierarchicalMenu-label">Audio</span>
<span class="ais-HierarchicalMenu-count">1,570</span>
</a>
</li>
</ul>
<button class="ais-HierarchicalMenu-showMore ais-HierarchicalMenu-showMore--disabled" disabled>Show more</button>
</div>
If SEO is critical to your search page, your custom HTML markup needs to be parsable:
- use plain
<a>
tags withhref
attributes for search engines bots to follow them, - use semantic markup with structured data when relevant, and test it.
Refer to our SEO checklist for building SEO-ready search experiences.