Allow sortable field to start in descending
l-ray opened this issue · 2 comments
Mostly when ordering a table by a column/field, intention by clicking the table header is to start ascending.
In some occasions (e.g. amount of user using a product), it is preferred to sort descending first.
Currently, we work around the issue by checking the incoming order_by
parameter during handle_param
and after comparing with the state from meta
decide to potentially flip the direction for the given column/field. This is ... well ... a work-around and quite fragile/frustrating way, as also LiveView rerender runs into issues.
A good solution could be to modify the derive sortable
in a way like below to indicate the intented initial order direction
@derive {Flop.Schema,
...
sortable: [:title, user_count: :desc],
...
}
Thanks for opening the issue. Flop.push_order/3
already allows to modify the directions that are used. I just made a small change in #432 that allows you to use a descending sort order as an initial direction.
You can use this in the table component like this:
<Flop.Phoenix.table items={@pets} meta={@meta} path={~p"/pets"}>
<:col :let={pet} label="Name" field={:name} directions={{:desc, :asc}}>
<%= pet.name %>
</:col>
</Flop.Phoenix.table>
This also allows you to control whether to sort with null values first or last. For more examples, refer to https://hexdocs.pm/flop/Flop.html#push_order/3.
This is amazing - we basically already upgraded to Flop 0.24.1 and have the needed changes on staging. Thank you!