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

[Feature] Custom sorting on column based on value #1038

Closed
Kaexzr opened this issue Apr 3, 2024 · 3 comments
Closed

[Feature] Custom sorting on column based on value #1038

Kaexzr opened this issue Apr 3, 2024 · 3 comments
Labels
enhancement New feature or request stale This issue is stale because it has been open for 30 days with no activity.

Comments

@Kaexzr
Copy link

Kaexzr commented Apr 3, 2024

I have some values that are members of an enum on the server side, I send them to the client as the string representation, so flutter can simply display it on a PlutoGrid, but sorting is broken, since I don't want them to be sorted alphabetically,

I could use a PlutoColumnValueFormatter and change my underlying data type to int and convert to text on the client side, but I would love to have a way to simply override how sorting is applied to a column.
I can think of many reasons this would be useful.

One way this could be done is: take a delegate the will be called with the cell value, and must return an int, then the sorting will be applied on the int value behind the scenes.

i.e.
image

@Kaexzr Kaexzr added the enhancement New feature or request label Apr 3, 2024
@Kaexzr
Copy link
Author

Kaexzr commented Apr 5, 2024

This might be far from a proper patch, but it works.
Is there anything I should look into before attempting a pull request?

For starters, Im passing the value to the delegate, not valueForSorting, as callers would expect data in the same format they set in the grid, is this desired?
The value passed to the delegate can be null, but the returned int must not be null, so we dont have to deal with null values in library code.
We might want to pass the current PlutoRow to the delegate, instead of the value, so callers can break ties on the value of the current cell (i.e. sort by this then that then....)

Any comments, suggestions?

pluto_column.dart

typedef PlutoSortDelegate = int Function(dynamic? value);
  
/// A callback that returns a value for sorting.
PlutoSortDelegate? sortDelegate;

  PlutoColumn({
    ...
    this.sortDelegate,

column_state.dart

 @override
  void sortAscending(PlutoColumn column, {bool notify = true}) {
    ....
    compare(a, b) {
      if (column.sortDelegate != null) {       
          var v1 = column.sortDelegate!(a.cells[column.field]!.value);
          var v2 = column.sortDelegate!(b.cells[column.field]!.value);
          return v1.compareTo(v2);  
      } else {
        return column.type.compare(
          a.cells[column.field]!.valueForSorting,
          b.cells[column.field]!.valueForSorting,
        );
      }
    }

    // compare(a, b) => column.type.compare(
    //       a.cells[column.field]!.valueForSorting,
    //       b.cells[column.field]!.valueForSorting,
    //     );
    ...

  @override
  void sortDescending(PlutoColumn column, {bool notify = true}) {
    ...
    compare(b, a) {
      if (column.sortDelegate != null) {
        var v1 = column.sortDelegate!(a.cells[column.field]!.value);
        var v2 = column.sortDelegate!(b.cells[column.field]!.value);
        return v1.compareTo(v2);
      } else {
        return column.type.compare(
          a.cells[column.field]!.valueForSorting,
          b.cells[column.field]!.valueForSorting,
        );
      }
    }

    // compare(b, a) => column.type.compare(
    //       a.cells[column.field]!.valueForSorting,
    //       b.cells[column.field]!.valueForSorting,
    //     );
    ...

Now, I can do :

image

Copy link

github-actions bot commented May 9, 2024

This issue is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale This issue is stale because it has been open for 30 days with no activity. label May 9, 2024
Copy link

This issue was closed because it has been inactive for 14 days since being marked as stale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request stale This issue is stale because it has been open for 30 days with no activity.
Projects
None yet
Development

No branches or pull requests

1 participant