Filters with custom types
I am trying to create a filter with custom type definition. My custom type is an enum powered by ecto_enum.
I checked the code for filters and it seems that it doesn't take in the scope field_type_matching and I din't find a way to define value => labels to have a combobox.
Is there any chance to extend the functionality to specify a map with values and titles to have a combobox at the end?
@smpallen99 Since I am not yet strong with Elixir/Phoenix I didn't make a PR since it also requires to write tests 🙃 Here is my snippet to show what I want to achieve.
def build_field({name, type}, q, %{:index_filters => index_filters} = defn) do
filters = case index_filters do
[list] -> list
_ -> []
end
if filters[:values] && filters[:values][name] do
id = "q_#{name}"
name_label = field_label(name, defn)
name_field = name
resources = filters[:values][name]
selected_key = case q["#{name}_eq"] do
nil -> nil
val -> val
end
div ".form-group" do
title = name_label
label ".label #{title}", for: "q_#{name}"
select "##{id}.form-control", [name: "q[#{name}_eq]"] do
option "Any", value: ""
for r <- resources do
id = r
name = ExAdmin.Utils.humanize r #ExAdmin.Helpers.display_name(r)
selected = if "#{id}" == selected_key, do: [selected: :selected], else: []
option name, [{:value, "#{id}"} | selected]
end
end
end
else
Logger.debug "ExAdmin.Filter: unknown type: #{inspect type} for field: #{inspect name}"
nil
end
end
Then I can specify in my admin resource definition next
filter only: [:inserted_at, :gender], values: [gender: [:male, :female]]
I can confirm that field_type_matching is not working in the filter panel.
As a quick fix, this works but a real fix should be done more top level, I guess?
diff --git a/lib/ex_admin/themes/admin_lte2/filter.ex b/lib/ex_admin/themes/admin_lte2/filter.ex
index b0f8679..4c619ec 100644
--- a/lib/ex_admin/themes/admin_lte2/filter.ex
+++ b/lib/ex_admin/themes/admin_lte2/filter.ex
@@ -19,7 +19,12 @@ defmodule ExAdmin.Theme.AdminLte2.Filter do
if scope do
input type: :hidden, name: :scope, value: scope
end
- for field <- fields(defn), do: build_field(field, q, defn)
+ for field <- fields(defn) do
+ field_type_matching = Application.get_env(:ex_admin, :field_type_matching) || %{}
+ {name, type} = field
+ type = Map.get(field_type_matching, type, type)
+ build_field({name, type}, q, defn)
+ end
for field <- associations(defn), do: build_field(field, q, defn)
end
div ".box-footer" do
@@ -31,7 +36,6 @@ defmodule ExAdmin.Theme.AdminLte2.Filter do
end
end
end
-
end
def build_field({name, :string}, q, defn) do