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

feat(Table): add pagination feature #2654

Draft
wants to merge 5 commits into
base: v3
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions playground/app/pages/components/table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ const columnPinning = ref({
right: ['actions']
})

const page = ref(1)

watch(page, () => {
tableApi.setPageIndex(page.value - 1)
})

function randomize() {
data.value = [...data.value].sort(() => Math.random() - 0.5)
}
Expand Down Expand Up @@ -317,13 +323,15 @@ onMounted(() => {
:columns="columns"
:column-pinning="columnPinning"
:loading="loading"
:pagination="{ pageSize: 10, pageIndex: 0 }"
sticky
class="border border-[var(--ui-border-accented)] rounded-[var(--ui-radius)] flex-1"
>
<template #expanded="{ row }">
<pre>{{ row.original }}</pre>
</template>
</UTable>
<UPagination v-if="table?.tableApi?.getPageCount() > 1" v-model:page="page" :items-per-page="1" :total="table?.tableApi?.getPageCount()" />

<div class="flex items-center justify-between gap-3">
<div class="text-sm text-[var(--ui-text-muted)]">
Expand Down
29 changes: 27 additions & 2 deletions src/runtime/components/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import type {
ExpandedOptions,
SortingOptions,
RowSelectionOptions,
PaginationOptions,
Updater,
CellContext,
HeaderContext
HeaderContext,
PaginationState
} from '@tanstack/vue-table'
import _appConfig from '#build/app.config'
import theme from '#build/ui/table'
Expand Down Expand Up @@ -86,6 +88,11 @@ export interface TableProps<T> {
* @link [Guide](https://tanstack.com/table/v8/docs/guide/row-selection)
*/
rowSelectionOptions?: Omit<RowSelectionOptions<T>, 'onRowSelectionChange'>
/**
* @link [API Docs](https://tanstack.com/table/v8/docs/api/features/pagination#table-options)
* @link [Guide](https://tanstack.com/table/v8/docs/guide/pagination)
*/
paginationOptions?: Omit<PaginationOptions, 'onPaginationChange'>
class?: any
ui?: Partial<typeof table.slots>
}
Expand All @@ -97,6 +104,7 @@ export type TableSlots<T> = {
expanded: (props: { row: Row<T> }) => any
empty: (props?: {}) => any
caption: (props?: {}) => any
pagination: (props?: {}) => any
} & DynamicHeaderSlots<T> & DynamicCellSlots<T>

</script>
Expand All @@ -109,7 +117,8 @@ import {
getFilteredRowModel,
getSortedRowModel,
getExpandedRowModel,
useVueTable
useVueTable,
getPaginationRowModel
} from '@tanstack/vue-table'
import { upperFirst } from 'scule'
import { useLocale } from '../composables/useLocale'
Expand All @@ -128,13 +137,23 @@ const ui = computed(() => table({
loadingAnimation: props.loadingAnimation
}))

// workaround to disable pagination when no pagination options are provided because passing
// getPaginationRowModel to useVueTable will automatically enable pagination
const paginationStateOverride = computed(() => !paginationState.value && !props.paginationOptions
? {
pageSize: data.value.length,
pageIndex: 0
}
: paginationState.value)

const globalFilterState = defineModel<string>('globalFilter', { default: undefined })
const columnFiltersState = defineModel<ColumnFiltersState>('columnFilters', { default: [] })
const columnVisibilityState = defineModel<VisibilityState>('columnVisibility', { default: {} })
const columnPinningState = defineModel<ColumnPinningState>('columnPinning', { default: {} })
const rowSelectionState = defineModel<RowSelectionState>('rowSelection', { default: {} })
const sortingState = defineModel<SortingState>('sorting', { default: [] })
const expandedState = defineModel<ExpandedState>('expanded', { default: {} })
const paginationState = defineModel<PaginationState>('pagination', { default: undefined })

const tableApi = useVueTable({
data,
Expand All @@ -157,6 +176,9 @@ const tableApi = useVueTable({
...(props.expandedOptions || {}),
getExpandedRowModel: getExpandedRowModel(),
onExpandedChange: updaterOrValue => valueUpdater(updaterOrValue, expandedState),
getPaginationRowModel: getPaginationRowModel(),
...(props.paginationOptions || {}),
onPaginationChange: updaterOrValue => valueUpdater(updaterOrValue, paginationState),
state: {
get globalFilter() {
return globalFilterState.value
Expand All @@ -178,6 +200,9 @@ const tableApi = useVueTable({
},
get sorting() {
return sortingState.value
},
get pagination() {
return paginationStateOverride.value
}
}
})
Expand Down