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

[UTable] Support for custom sort function for specific columns #1071

Closed
Ragura opened this issue Dec 6, 2023 · 0 comments · Fixed by #1075
Closed

[UTable] Support for custom sort function for specific columns #1071

Ragura opened this issue Dec 6, 2023 · 0 comments · Fixed by #1075
Labels
enhancement New feature or request

Comments

@Ragura
Copy link
Contributor

Ragura commented Dec 6, 2023

Description

I'm trying to change the sorting algorithm for a particular column for use in a UTable component. The use-case is implementing localeCompare for string comparison so that lowercase letters don't appear at the very bottom of a formatted column of strings. Unless I'm missing something from the docs, this doesn't seem to be supported currently? I've tried making a computed variable that sorts the rows that are fed to the table's rows prop and taking into account the currently sorted column with v-model:sort binding. When doing so it appears to calculate the sorting correctly, but then the sorting of the table still uses its own algorithm to do the sorting after that.

In other words, this does not seem to work:

const sortedColumn = ref({
  column: 'title',
  direction: 'asc',
})

const sortedRows = computed(() => {
  if (sortedColumn.value.column === 'title') {
    const sorted = [...rows.value].sort((a, b) => a.title.localeCompare(b.title, { sensitivity: 'base' }))
    return sorted
  }
  return rows.value
})

<UTable :rows="sortedRows" v-model:sort="sortedColumn">

I'd like to suggest adding a property to the Column object called sortFn which takes a function and is passed the compared items.

const columns = [
    {
      key: 'title',
      label: 'Title',
      sortable: true,
      sortFn: mySortingAlgo, 
    },
]

Any alternative solutions would be much appreciated as well!

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant