API Reference / Vue InstantSearch / ais-sort-by
Signature
<ais-sort-by
  :items="object[]"
  // Optional parameters
  :transform-items="function"
  :class-names="object"
/>

About this widget

The ais-sort-by widget displays a list of indices, allowing a user to change the way hits are sorting (with replica indices). Another common use case for this widget is to let the user 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

1
2
3
4
5
6
7
<ais-sort-by
  :items="[
    { value: 'instant_search', label: 'Featured' },
    { value: 'instant_search_price_asc', label: 'Price asc.' },
    { value: 'instant_search_price_desc', label: 'Price desc.' },
  ]"
/>

Props

items
type: object[]
Required

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.
1
2
3
4
5
6
7
<ais-sort-by
  :items="[
    { value: 'instant_search', label: 'Featured' },
    { value: 'instant_search_price_asc', label: 'Price asc.' },
    { value: 'instant_search_price_desc', label: 'Price desc.' },
  ]"
/>
transform-items
type: function
default: items => items
Optional

Receives the items and is called before displaying them. Should return a new array with the same shape as the original array. Useful for transforming, removing, or reordering items.

In addition, the full results data is available, which includes all regular response parameters, as well as parameters from the helper (for example disjunctiveFacetsRefinements).

When using an array, take steps to avoid creating infinite loops. When you use an array as a prop, it causes the widget to re-register on every render, and this can sometimes cause these infinite loops.

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
<template>
  <!-- ... -->
  <ais-sort-by
    [...]
    :transform-items="transformItems"
  />
</template>

<script>
  export default {
    methods: {
      transformItems(items) {
        return items.map(item => ({
          ...item,
          label: item.label.toUpperCase(),
        }));
      },

      /* or, combined with results */
      transformItems(items, { results }) {
        return results?.nbPages < 4
          ? items.filter(item => item.value === 'instant_search')
          : items;
      },
    },
  };
</script>
class-names
type: object
default: {}
Optional

The CSS classes to override.

  • ais-SortBy: the root of the widget
  • ais-SortBy-select: the select element.
  • ais-SortBy-option: the option element of the select.
1
2
3
4
5
6
7
8
<ais-sort-by
  [...]
  :class-names="{
    'ais-SortBy': 'MyCustomSortBy',
    'ais-SortBy-select': 'MyCustomSortBySelect',
    // ...
  }"
/>

Customize the UI

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

  • items: object[]: the list of items the widget can display.
  • currentRefinement: string: the currently applied refinement.
  • hasNoResults: boolean: true if the search has results.
  • refine: (value: string) => void: the function to call with the next value of the index name.

Where each item is an object containing:

  • label: string: the label of the index to display.
  • value: string: the name of the index to target.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<ais-sort-by
  :items="[
    { value: 'instant_search', label: 'Featured' },
    { value: 'instant_search_price_asc', label: 'Price asc.' },
    { value: 'instant_search_price_desc', label: 'Price desc.' },
  ]"
>
  <template v-slot="{ items, currentRefinement, refine }">
    <ul>
      <li v-for="item in items" :key="item.value" :value="item.value">
        <a
          href="#"
          :style="{ fontWeight: item.value === currentRefinement ? 'bold' : '' }"
          @click.prevent="refine(item.value)"
        >
          {{ item.label }}
        </a>
      </li>
    </ul>
  </template>
</ais-sort-by>

HTML output

1
2
3
4
5
6
7
<div class="ais-SortBy">
  <select class="ais-SortBy-select">
    <option class="ais-SortBy-option" value="instant_search">Featured</option>
    <option class="ais-SortBy-option" value="instant_search_price_asc">Price asc.</option>
    <option class="ais-SortBy-option" value="instant_search_price_desc">Price desc.</option>
  </select>
</div>
Did you find this page helpful?