formex_ecto
formex_ecto copied to clipboard
SelectAssoc options custom sort
Looks like the option labels are sorted alphabetically.
https://github.com/jakub-zawislak/formex_ecto/blob/aa8a6ef78fb29d0fea59c2f37e2cef0324c42e22/lib/custom_fields/select_assoc.ex#L290
How do I sort by a column?
** Context
I have a multi level/nested set of categories. These can be selected at any level using a SelectAssoc
field.
Data
Deserts
-- Gluten Free
-- Vegetarian
Mains
-- Chicken
Form
|> add(
:categories,
SelectAssoc,
label: "Categories",
choice_label: fn category ->
if category.parent_id do
"-- #{category.name}"
else
category.name
end
end,
query: fn query ->
from c in query,
order_by: [asc: :sort_value]
end,
validation: [:required]
)
However, it displays like this:
-- Gluten Free
-- Vegetarian
-- Chicken
Deserts
Mains
Maybe you can make use of group_by
option?
Anyway, custom sorting in selects is the must have in this library
I did not need many to many relation, thus I could replace it with a simple select field with choices.