-
Notifications
You must be signed in to change notification settings - Fork 815
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
Support custom sort functions in 'DataTable' #2512
Conversation
b43a384
to
06b5293
Compare
This is somewhat related to issue Textualize#2261
06b5293
to
da2dc9f
Compare
@darrenburns Thoughts on this? |
I like the idea but I'm wondering if sorting by key should be a separate method. With the approach in this PR, "column" and "key" have an awkward dependency on each other. If you supply both, it's unclear what will happen without reading the docs. |
I could change the PR to follow that approach. I guess the alternative method ( |
Another option could be that the def sort(
self,
*columns: ColumnKey | str,
reverse: bool = False,
) -> Self To something like... def sort(
self,
by: Iterable[ColumnKey | str] | Callable,
reverse: bool = False,
) -> Self So you could either supply an iterable of columns or you can supply a function to |
The `DataTable` widget now takes the `by` argument instead of `columns`, allowing the table to also be sorted using a custom function (or other callable). This is a breaking change since it requires all calls to the `sort` method to include an iterable of key(s) (or a singular function/callable). Covers Textualize#2261 using [suggested function signature](Textualize#2512 (comment)) from @darrenburns on PR Textualize#2512.
* DataTable sort by function (or other callable) The `DataTable` widget now takes the `by` argument instead of `columns`, allowing the table to also be sorted using a custom function (or other callable). This is a breaking change since it requires all calls to the `sort` method to include an iterable of key(s) (or a singular function/callable). Covers #2261 using [suggested function signature](#2512 (comment)) from @darrenburns on PR #2512. * argument change and functionaloty update Changed back to orinal `columns` argument and added a new `key` argument which takes a function (or other callable). This allows the PR to NOT BE a breaking change. * better example for docs - Updated the example file for the docs to better show the functionality of the change (especially when using `columns` and `key` together). - Added one new tests to cover a similar situation to the example changes * removed unecessary code from example - the sort by clicked column function was bloat in my opinion * requested changes * simplify method and terminology * combine key_wrapper and default sort * Removing some tests from DataTable.sort as duplicates. Ensure there is test coverage of the case where a key, but no columns, is passed to DataTable.sort. * Remove unused import * Fix merge issues in CHANGELOG, update DataTable sort-by-key changelog PR link --------- Co-authored-by: Darren Burns <[email protected]> Co-authored-by: Darren Burns <[email protected]>
I stumbled across this open PR looking for something else. Sorting a |
Indeed, I'll close this... |
This is somewhat related to issue #2261
Please review the following checklist.