Creating a default filter

You will often want to create a list with a filter. However, often what you want is a default filter position when you show the list. In the following example we are filtering employees by country and we want the default view to show countries from USA only and from there the user can make their own section. Here is how you achieve creating a default filter. Thanks @adam for contributing this tip.

YAML Snippet for this tip follows below:

      query: |
        SELECT id
              FROM [default/employees]
              WHERE '$.Country' like @filter 
              order by '$.LastName'
        filter: |
          =$boolean(@ctx.components.empList.state.filter) ? @ctx.components.empList.state.filter : 'USA'

  - type: component.list
    instanceId: empList
      data: =@ctx.datasources.employees
        - title: USA
          value: USA
        - title: Canada
          value: Canada

The query parameter (filter) checks if the filter control exists and if it does we just use it. If not. Then we set the default which in this case is ‘USA’.