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

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.

Please 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.

A really basic example is the following filter for a boolean column :published:

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.

Callbacks

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

The list of options for the select filter.