View Source Backpex.Filters.Boolean behaviour (Backpex v0.7.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.BooleanWhen you
use Backpex.Filters.Boolean, theBackpex.Filters.Booleanmodule will set@behavior Backpex.Filters.Boolean. In addition it will add arenderandrender_formfunction in order to display the corresponding filter. It will also implement theBackpex.Filter.queryfunction to define a boolean query.
Summary
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.