API Reference / Angular InstantSearch Widgets / ais-numeric-menu
Apr. 24, 2019

ais-numeric-menu

You are reading the documentation for Angular InstantSearch v3, which is in beta. You can find the v2 documentation here.

Widget signature
<ais-numeric-menu
  attribute="string"
  [items]="object[]"
  // Optional parameters
  [autoHideContainer]="boolean"
></ais-numeric-menu>

About this widget

The ais-numeric-menu component displays a menu that lets the user choose a single range for a specific numeric attribute.

Requirements

  • The attributes passed to the attributes prop must be declared as Attributes for faceting on the Algolia dashboard or configured as attributesForFaceting with the Algolia API.

  • The attribute passed to the attribute prop must be represented as a number in the index, not a string.

Examples

1
2
3
4
5
6
7
8
9
10
<ais-numeric-menu
  attribute="price"
  [items]="[
    { name: 'All' },
    { end: 4, name: 'less than 4' },
    { start: 4, end: 4, name: '4' },
    { start: 5, end: 10, name: 'between 5 and 10' },
    { start: 10, name: 'more than 10' }
  ]"
></ais-numeric-menu>

Properties

attribute
type: string
Required

The name of the attribute in the record.

1
<ais-numeric-menu attribute="price"></ais-numeric-menu>
items
type: object[]
Required

The list of ranges availables. Both start and end can be omitted inside the item. Only label is required.

1
2
3
4
5
6
7
8
9
<ais-numeric-menu
  [items]="[
    { name: 'All' },
    { end: 4, name: 'less than 4' },
    { start: 4, end: 4, name: '4' },
    { start: 5, end: 10, name: 'between 5 and 10' },
    { start: 10, name: 'more than 10' }
  ]"
></ais-numeric-menu>
autoHideContainer
type: boolean
default: true
Optional

Whether to hide the menu if there’s no item to display

1
<ais-numeric-menu [autoHideContainer]="false"></ais-numeric-menu>

Customize the UI - connectNumericMenu

If you want to create your own UI of the ais-numeric-menu widget, you can combine the connectNumericMenu connector with the BaseWidget class.

1. Extend the BaseWidget class

First of all, you will need to write some boilerplate code in order to initialize correctly the BaseWidget class. This happens in the constructor() of your class extending the BaseWidget class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { Component, Inject, forwardRef } from '@angular/core';
import { BaseWidget, NgAisInstantSearch } from 'angular-instantsearch';

@Component({
  selector: 'app-numeric-menu',
  template: '<p>It works!</p>'
})
export class NumericMenu extends BaseWidget {
  constructor(
    @Inject(forwardRef(() => NgAisInstantSearch))
    public instantSearchParent
  ) {
    super('NumericMenu');
  }
}

There are a couple of things happening in this boilerplate:

  • we create a NumericMenu class extending BaseWidget
  • we reference the <ais-instantsearch> parent component instance on the NumericMenu widget class
  • we set app-numeric-menu as a selector, so we can use our component as <app-numeric-menu></app-numeric-menu>

2. Connect your custom widget

The BaseWidget class has a method called createWidget() which takes two arguments: the connector to use and an object of options (instance options) for this connector. We call this method at ngOnInit. This component now implements OnInit.

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
32
33
import { Component, Inject, forwardRef } from '@angular/core';
import { BaseWidget, NgAisInstantSearch } from 'angular-instantsearch';
import { connectNumericMenu } from 'instantsearch.js/es/connectors';

@Component({
  selector: 'app-numeric-menu',
  template: '<p>It works!</p>'
})
export class NumericMenu extends BaseWidget {
  public state: {
    // render options
  };
  constructor(
    @Inject(forwardRef(() => NgAisInstantSearch))
    public instantSearchParent
  ) {
    super('NumericMenu');
  }
  ngOnInit() {
    this.createWidget(connectNumericMenu, {
      // instance options
      attribute: 'free_shipping',
      items: [
        { name: "All" },
        { end: 4, name: "less than 4" },
        { start: 4, end: 4, name: "4" },
        { start: 5, end: 10, name: "between 5 and 10" },
        { start: 10, name: "more than 10" }
      ],
    });
    super.ngOnInit();
  }
}

3. Render from the state

Your component instance has access to a this.state property which holds the rendering options of the widget.

public state: {
  items: object[];
  hasNoResults: boolean;
  refine: Function;
  createURL: Function;
  widgetParams: object;
}
1
2
3
4
5
<select (change)="state.refine(select.selectedOptions[0].value)" #select>
  <option *ngFor="let item of state.items; index as i" [value]="item.value">
    {{ state.widgetParams.items[i].name }}
  </option>
</select>

Rendering options

items
type: object[]

The list of available options, with each option:

  • label: string: the label for the option.
  • value: string: the encoded URL of the bounds object with the {start, end} form. This value can be used verbatim in the webpage and can be read by refine directly. If you want to inspect the value, you can do JSON.parse(window.decodeURI(value)) to get the object.
  • isRefined: boolean: whether or not the refinement is selected.
hasNoResults
type: boolean

Whether or not the search has results.

refine
type: function

Sets the selected value and triggers a new search.

createURL
type: function

Generates a URL for the next state.

widgetParams
type: object

All original widget options forwarded to the render function.

Instance options

attribute
type: string
Required

The name of the attribute in the record.

items
type: object[]
Required

A list of all the options to display, with:

  • label: string: label of the option
  • start: string: lower bound of the option (>=)
  • end: string: higher bound of the option (<=)
transformItems
type: function
default: x => x
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 mapping over the items to transform, and remove or reorder them.

Full example

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
32
33
34
35
36
37
38
39
40
41
42
43
import { Component, Inject, forwardRef } from '@angular/core';
import { BaseWidget, NgAisInstantSearch } from 'angular-instantsearch';
import { connectNumericMenu } from 'instantsearch.js/es/connectors';

@Component({
  selector: 'app-numeric-menu',
  template: `
<select (change)="state.refine(select.selectedOptions[0].value)" #select>
  <option *ngFor="let item of state.items; index as i" [value]="item.value">
    {{ state.widgetParams.items[i].name }}
  </option>
</select>
`
})
export class NumericMenu extends BaseWidget {
  public state: {
     items: object[];
     hasNoResults: boolean;
     refine: Function;
     createURL: Function;
     widgetParams: object;
  };
  constructor(
    @Inject(forwardRef(() => NgAisInstantSearch))
    public instantSearchParent
  ) {
    super('NumericMenu');
  }
  ngOnInit() {
    this.createWidget(connectNumericMenu, {
      // instance options
      attribute: 'free_shipping',
      items: [
        { name: "All" },
        { end: 4, name: "less than 4" },
        { start: 4, end: 4, name: "4" },
        { start: 5, end: 10, name: "between 5 and 10" },
        { start: 10, name: "more than 10" }
      ],
    });
    super.ngOnInit();
  }
}

HTML output

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
<div class="ais-NumericMenu">
  <ul class="ais-NumericMenu-list">
    <li class="ais-NumericMenu-item ais-NumericMenu-item--selected">
      <label class="ais-NumericMenu-label">
        <input
          class="ais-NumericMenu-radio"
          type="radio"
          name="NumericMenu"
          checked
        />
        <span class="ais-NumericMenu-labelText">All</span>
      </label>
    </li>
    <li class="ais-NumericMenu-item">
      <label class="ais-NumericMenu-label">
        <input
          class="ais-NumericMenu-radio"
          type="radio"
          name="NumericMenu"
        />
        <span class="ais-NumericMenu-labelText">Less than 500</span>
      </label>
    </li>
  </ul>
</div>

Did you find this page helpful?