Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow sortable field to start in descending #431

Closed
l-ray opened this issue Nov 17, 2023 · 2 comments · Fixed by #432
Closed

Allow sortable field to start in descending #431

l-ray opened this issue Nov 17, 2023 · 2 comments · Fixed by #432
Assignees

Comments

@l-ray
Copy link

l-ray commented Nov 17, 2023

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],
           ...
  }
@woylie
Copy link
Owner

woylie commented Nov 18, 2023

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.

@l-ray
Copy link
Author

l-ray commented Nov 21, 2023

This is amazing - we basically already upgraded to Flop 0.24.1 and have the needed changes on staging. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants