Skip to content

[Datagrid] Make it possible to accept custom sorter for EuiDataGrid column #6867

Closed

Description

@dej611

Is your feature request related to a problem? Please describe.

It would be nice to be able to extend the sorting behaviour of EuiDataGrid for specific types with a custom sorter.
An example of this would be the ip sorting: not just a text comparison, rather providing a sorter which supports both IPv4 and IPv6 would be nice.
Another example would be a sorter for a version field.

Describe the solution you'd like
An additional property for the sorting like customSorters: { [columnIdA]: (a, b) => ..., [columnIdB]: (a, b) => ... }

Describe alternatives you've considered
Sorting the data before submitting to the Eui DataGrid, but this will completely disable all inMemory enhancements.

Activity

  1. tkajtoch commented on Jun 22, 2023

    @tkajtoch
    Contributor

    Hi Marco! We'll look into that, triage the work needed and get back to you shortly

  2. cee-chen commented on Jun 22, 2023

    @cee-chen
    Contributor

    Custom sorting is supported via the schemaDetectors prop. Take a look at the demo JS for the first example on this page:

    https://elastic.github.io/eui/#/tabular-content/data-grid-schema-columns#schemas

    The basic necessary logic is:

    const columns = [
      {
        id: 'columnWithCustomSchema',
        schema: 'schemaWithCustomSorting',
      },
    ];
    
    const schemaDetectors = [
      {
        type: 'schemaWithCustomSorting',
        comparator: (a, b, direction: 'asc' | 'desc') => {
          // ... write your sort function here - see the above EUI demo link
        },
        sortTextAsc: 'Sort ascending',
        sortTextDesc: 'Sort descending',
      },
    ];
    
    <EuiDataGrid
      columns={columns}
      schemaDetectors={schemaDetectors}
      // ... other required props
    />
  3. added
    answered
    on Jun 22, 2023
  4. github-actions commented on Jun 26, 2023

    @github-actions

    👋 This issue hasn't seen activity in 3 days, so we're automatically closing this issue as answered. Please leave a comment if that's not the case, or if you have any remaining questions or issues.

  5. dej611 commented on Dec 7, 2023

    @dej611
    ContributorAuthor

    I had a go using that, but the big problem is the string type for the comparable values which is very limiting: for some entries I need to access the raw object in order to perform required transformations to compare.
    Or at least provide the index for both entries, so that we can access the raw objects from the table itself.

  6. cee-chen commented on Dec 7, 2023

    @cee-chen
    Contributor

    Ahh gotcha. We could likely add a 4th param to the comparator callback with the full set of values here:

    const result = comparator(aValue, bValue, column.direction);

    that feels like it'd be a pretty quick/easy change since it's an enhancement and not a breaking change. would you be interested in contributing that to EUI? 👀 (no worries if not, totally not trying to peer pressure you! - if you'd rather have EUI pick up this work, that's totally valid)

  7. dej611 commented on Dec 7, 2023

    @dej611
    ContributorAuthor

    I think I can have a go with that.

    Edit => #7406

  8. removed
    answered
    on Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions