View Source Backpex.Filters.Boolean behaviour (Backpex v0.6.0)

The boolean filter renders one checkbox per given option, hence multiple options can apply at the same time. Instead of implementing a query callback, you need to define predicates for each option leveraging Ecto.Query.dynamic/2.

Warning

Note that only query elements will work as a predicate that also work in an Ecto.Query.where/3.

If none is selected, the filter does not change the query. If multiple options are selected they are logically reduced via orWhere.

See the following example for an implementation of a boolean filter for a published field.

defmodule MyAppWeb.Filters.EventPublished do
  use Backpex.Filters.Boolean

  @impl Backpex.Filter
  def label, do: "Published?"

  @impl Backpex.Filters.Boolean
  def options do
    [
      %{
        label: "Published",
        key: "published",
        predicate: dynamic([x], x.published)
      },
      %{
        label: "Not published",
        key: "not_published",
        predicate: dynamic([x], not x.published)
      }
    ]
  end
end

use Backpex.Filters.Boolean

When you use Backpex.Filters.Boolean, the Backpex.Filters.Boolean module will set @behavior Backpex.Filters.Boolean. In addition it will add a render and render_form function in order to display the corresponding filter. It will also implement the Backpex.Filter.query function to define a boolean query.

Summary

Callbacks

The list of options for the select filter.

Functions

Attributes

  • value (:any) (required)
  • options (:list) (required)

Attributes

  • form (:any) (required)
  • field (:atom) (required)
  • value (:any) (required)
  • options (:list) (required)

Callbacks

@callback options() :: [map()]

The list of options for the select filter.

Functions

Link to this function

find_option_label(options, key)

View Source
Link to this function

maybe_query(predicates, query)

View Source
Link to this function

option_value_to_label(options, values)

View Source
Link to this function

query(query, options, attribute, value)

View Source

Attributes

  • value (:any) (required)
  • options (:list) (required)

Attributes

  • form (:any) (required)
  • field (:atom) (required)
  • value (:any) (required)
  • options (:list) (required)