formex_ecto icon indicating copy to clipboard operation
formex_ecto copied to clipboard

SelectAssoc options custom sort

Open jaysson opened this issue 7 years ago • 2 comments

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

jaysson avatar Feb 14 '18 08:02 jaysson

Maybe you can make use of group_by option? Anyway, custom sorting in selects is the must have in this library

jakub-zawislak avatar Feb 15 '18 21:02 jakub-zawislak

I did not need many to many relation, thus I could replace it with a simple select field with choices.

jaysson avatar Feb 19 '18 09:02 jaysson