Backpex.Filters.Boolean behaviour (Backpex v0.16.2)
View SourceThe 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
enduse 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.