Skip to content

Commit

Permalink
feat(VDatatable): to solve #11117
Browse files Browse the repository at this point in the history
  • Loading branch information
berrywhj committed Oct 24, 2024
1 parent 8c5a2b9 commit 8513b15
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ export const VDataIterator = genericComponent<new <T> (
const { items } = useDataIteratorItems(props)
const { filteredItems } = useFilter(props, items, search, { transform: item => item.raw })

const { sortBy, multiSort, mustSort } = createSort(props)
const { sortBy, multiSort, mustSort, sortDescendingFirst } = createSort(props)
const { page, itemsPerPage } = createPagination(props)

const { toggleSort } = provideSort({ sortBy, multiSort, mustSort, page })
const { toggleSort } = provideSort({ sortBy, multiSort, mustSort, sortDescendingFirst, page })
const { sortByWithGroups, opened, extractRows, isGroupOpen, toggleGroup } = provideGroupBy({ groupBy, sortBy })

const { sortedItems } = useSortedItems(props, filteredItems, sortByWithGroups, { transform: item => item.raw })
Expand Down
4 changes: 2 additions & 2 deletions packages/vuetify/src/components/VDataTable/VDataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export const VDataTable = genericComponent<new <T extends readonly any[], V>(

setup (props, { attrs, slots }) {
const { groupBy } = createGroupBy(props)
const { sortBy, multiSort, mustSort } = createSort(props)
const { sortBy, multiSort, mustSort, sortDescendingFirst } = createSort(props)
const { page, itemsPerPage } = createPagination(props)
const { disableSort } = toRefs(props)

Expand All @@ -152,7 +152,7 @@ export const VDataTable = genericComponent<new <T extends readonly any[], V>(
customKeyFilter: filterFunctions,
})

const { toggleSort } = provideSort({ sortBy, multiSort, mustSort, page })
const { toggleSort } = provideSort({ sortBy, multiSort, mustSort, sortDescendingFirst, page })
const { sortByWithGroups, opened, extractRows, isGroupOpen, toggleGroup } = provideGroupBy({ groupBy, sortBy, disableSort })

const { sortedItems } = useSortedItems(props, filteredItems, sortByWithGroups, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const makeVDataTableHeadersProps = propsFactory({
sticky: Boolean,
disableSort: Boolean,
multiSort: Boolean,
sortDescendingFirst: Boolean,
sortAscIcon: {
type: IconValue,
default: '$sortAsc',
Expand Down Expand Up @@ -103,9 +104,13 @@ export const VDataTableHeaders = genericComponent<VDataTableHeadersSlots>()({
function getSortIcon (column: InternalDataTableHeader) {
const item = sortBy.value.find(item => item.key === column.key)

if (!item) return props.sortAscIcon
const firstIcon = props.sortDescendingFirst ? props.sortDescIcon : props.sortAscIcon
const secondIcon = props.sortDescendingFirst ? props.sortAscIcon : props.sortDescIcon
const firstOrder = props.sortDescendingFirst ? 'desc' : 'asc'

return item.order === 'asc' ? props.sortAscIcon : props.sortDescIcon
if (!item) return firstIcon

return item.order === firstOrder ? firstIcon : secondIcon
}

const { backgroundColorClasses, backgroundColorStyles } = useBackgroundColor(props, 'color')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const VDataTableServer = genericComponent<new <T extends readonly any[],

setup (props, { attrs, slots }) {
const { groupBy } = createGroupBy(props)
const { sortBy, multiSort, mustSort } = createSort(props)
const { sortBy, multiSort, mustSort, sortDescendingFirst } = createSort(props)
const { page, itemsPerPage } = createPagination(props)
const { disableSort } = toRefs(props)
const itemsLength = computed(() => parseInt(props.itemsLength, 10))
Expand All @@ -80,7 +80,7 @@ export const VDataTableServer = genericComponent<new <T extends readonly any[],

const { items } = useDataTableItems(props, columns)

const { toggleSort } = provideSort({ sortBy, multiSort, mustSort, page })
const { toggleSort } = provideSort({ sortBy, multiSort, mustSort, sortDescendingFirst, page })

const { opened, isGroupOpen, toggleGroup, extractRows } = provideGroupBy({ groupBy, sortBy, disableSort })

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const VDataTableVirtual = genericComponent<new <T extends readonly any[],

setup (props, { attrs, slots }) {
const { groupBy } = createGroupBy(props)
const { sortBy, multiSort, mustSort } = createSort(props)
const { sortBy, multiSort, mustSort, sortDescendingFirst } = createSort(props)
const { disableSort } = toRefs(props)

const {
Expand All @@ -106,7 +106,7 @@ export const VDataTableVirtual = genericComponent<new <T extends readonly any[],
customKeyFilter: filterFunctions,
})

const { toggleSort } = provideSort({ sortBy, multiSort, mustSort })
const { toggleSort } = provideSort({ sortBy, multiSort, mustSort, sortDescendingFirst })
const { sortByWithGroups, opened, extractRows, isGroupOpen, toggleGroup } = provideGroupBy({ groupBy, sortBy, disableSort })

const { sortedItems } = useSortedItems(props, filteredItems, sortByWithGroups, {
Expand Down
20 changes: 13 additions & 7 deletions packages/vuetify/src/components/VDataTable/composables/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const makeDataTableSortProps = propsFactory({
customKeySort: Object as PropType<Record<string, DataTableCompareFunction>>,
multiSort: Boolean,
mustSort: Boolean,
sortDescendingFirst: Boolean,
}, 'DataTable-sort')

const VDataTableSortSymbol: InjectionKey<{
Expand All @@ -34,23 +35,28 @@ type SortProps = {
'onUpdate:sortBy': ((value: any) => void) | undefined
mustSort: boolean
multiSort: boolean
sortDescendingFirst: boolean
}

export function createSort (props: SortProps) {
const sortBy = useProxiedModel(props, 'sortBy')
const mustSort = toRef(props, 'mustSort')
const multiSort = toRef(props, 'multiSort')
const sortDescendingFirst = toRef(props, 'sortDescendingFirst')

return { sortBy, mustSort, multiSort }
return { sortBy, mustSort, multiSort, sortDescendingFirst }
}

export function provideSort (options: {
sortBy: Ref<readonly SortItem[]>
mustSort: Ref<boolean>
multiSort: Ref<boolean>
sortDescendingFirst: Ref<boolean>
page?: Ref<number>
}) {
const { sortBy, mustSort, multiSort, page } = options
const { sortBy, mustSort, multiSort, sortDescendingFirst, page } = options
const firstOrder = sortDescendingFirst.value ? 'desc' : 'asc'
const secondOrder = sortDescendingFirst.value ? 'asc' : 'desc'

const toggleSort = (column: InternalDataTableHeader) => {
if (column.key == null) return
Expand All @@ -59,16 +65,16 @@ export function provideSort (options: {
const item = newSortBy.find(x => x.key === column.key)

if (!item) {
if (multiSort.value) newSortBy = [...newSortBy, { key: column.key, order: 'asc' }]
else newSortBy = [{ key: column.key, order: 'asc' }]
} else if (item.order === 'desc') {
if (multiSort.value) newSortBy = [...newSortBy, { key: column.key, order: firstOrder }]
else newSortBy = [{ key: column.key, order: firstOrder }]
} else if (item.order === secondOrder) {
if (mustSort.value) {
item.order = 'asc'
item.order = firstOrder
} else {
newSortBy = newSortBy.filter(x => x.key !== column.key)
}
} else {
item.order = 'desc'
item.order = secondOrder
}

sortBy.value = newSortBy
Expand Down

0 comments on commit 8513b15

Please sign in to comment.