diff --git a/docs/app/components/content/examples/table/TableGlobalFilterExample.vue b/docs/app/components/content/examples/table/TableGlobalFilterExample.vue index ac78465145..059b044807 100644 --- a/docs/app/components/content/examples/table/TableGlobalFilterExample.vue +++ b/docs/app/components/content/examples/table/TableGlobalFilterExample.vue @@ -90,8 +90,6 @@ const columns: TableColumn[] = [{ } }] -const table = useTemplateRef('table') - const globalFilter = ref('45') diff --git a/docs/app/components/content/examples/table/TablePaginationExample.vue b/docs/app/components/content/examples/table/TablePaginationExample.vue new file mode 100644 index 0000000000..a8b4926988 --- /dev/null +++ b/docs/app/components/content/examples/table/TablePaginationExample.vue @@ -0,0 +1,174 @@ + + + diff --git a/docs/content/3.components/table.md b/docs/content/3.components/table.md index 82eb61ebf2..5615f636c3 100644 --- a/docs/content/3.components/table.md +++ b/docs/content/3.components/table.md @@ -336,7 +336,7 @@ You can use the `column-pinning` prop to control the pinning state of the column ### With column visibility -You can add use [DropdownMenu](/components/dropdown-menu) component to toggle the visibility of the columns using the TanStack Table [Column Visibility APIs](https://tanstack.com/table/latest/docs/api/features/column-visibility). +You can use a [DropdownMenu](/components/dropdown-menu) component to toggle the visibility of the columns using the TanStack Table [Column Visibility APIs](https://tanstack.com/table/latest/docs/api/features/column-visibility). ::component-example --- @@ -391,6 +391,25 @@ class: '!p-0' You can use the `global-filter` prop to control the global filter state (can be binded with `v-model`). :: +### With pagination + +You can use a [Pagination](/components/pagination) component to control the pagination state using the [Pagination APIs](https://tanstack.com/table/latest/docs/api/features/pagination). + +There are different pagination approaches as explained in [Pagination Guide](https://tanstack.com/table/latest/docs/guide/pagination#pagination-guide). In this example, we use client-side pagination so we need to manually pass `getPaginationRowModel()`{lang="ts-type"} function. + +::component-example +--- +prettier: true +collapse: true +name: 'table-pagination-example' +class: '!p-0' +--- +:: + +::tip +You can use the `pagination` prop to control the pagination state (can be binded with `v-model`). +:: + ### With fetched data You can fetch data from an API and use them in the Table. diff --git a/playground/app/pages/components/table.vue b/playground/app/pages/components/table.vue index 8cdc3f3225..77e548aff8 100644 --- a/playground/app/pages/components/table.vue +++ b/playground/app/pages/components/table.vue @@ -2,6 +2,7 @@ import { h, resolveComponent } from 'vue' import { upperFirst } from 'scule' import type { TableColumn } from '@nuxt/ui' +import { getPaginationRowModel } from '@tanstack/vue-table' const UButton = resolveComponent('UButton') const UCheckbox = resolveComponent('UCheckbox') @@ -269,6 +270,11 @@ const columnPinning = ref({ right: ['actions'] }) +const pagination = ref({ + pageIndex: 0, + pageSize: 10 +}) + function randomize() { data.value = [...data.value].sort(() => Math.random() - 0.5) } @@ -322,11 +328,15 @@ onMounted(() => { :columns="columns" :column-pinning="columnPinning" :loading="loading" - sticky + :pagination="pagination" + :pagination-options="{ + getPaginationRowModel: getPaginationRowModel() + }" :ui="{ tr: 'divide-x divide-[var(--ui-border)]' }" - class="border border-[var(--ui-border-accented)] rounded-[var(--ui-radius)] flex-1" + sticky + class="border border-[var(--ui-border-accented)] rounded-[var(--ui-radius)]" > diff --git a/src/runtime/components/Table.vue b/src/runtime/components/Table.vue index 3edf586cd0..9e2375d19c 100644 --- a/src/runtime/components/Table.vue +++ b/src/runtime/components/Table.vue @@ -3,26 +3,38 @@ import type { Ref } from 'vue' import type { VariantProps } from 'tailwind-variants' import type { AppConfig } from '@nuxt/schema' -import type { RowData } from '@tanstack/table-core' import type { - Row, + CellContext, ColumnDef, + ColumnFiltersOptions, ColumnFiltersState, + ColumnOrderState, + ColumnPinningOptions, ColumnPinningState, - RowSelectionState, - SortingState, + ColumnSizingInfoState, + ColumnSizingOptions, + ColumnSizingState, + CoreOptions, + ExpandedOptions, ExpandedState, - VisibilityState, + FacetedOptions, GlobalFilterOptions, - ColumnFiltersOptions, - ColumnPinningOptions, - VisibilityOptions, - ExpandedOptions, - SortingOptions, + GroupingOptions, + GroupingState, + HeaderContext, + PaginationOptions, + PaginationState, + Row, + RowData, + RowPinningOptions, + RowPinningState, RowSelectionOptions, + RowSelectionState, + SortingOptions, + SortingState, Updater, - CellContext, - HeaderContext + VisibilityOptions, + VisibilityState } from '@tanstack/vue-table' import _appConfig from '#build/app.config' import theme from '#build/ui/table' @@ -44,13 +56,17 @@ const table = tv({ extend: tv(theme), ...(appConfigTable.ui?.table || {}) }) type TableVariants = VariantProps -export type TableColumn = ColumnDef +export type TableData = RowData + +export type TableColumn = ColumnDef -export interface TableData { - [key: string]: any +export interface TableOptions extends Omit, 'data' | 'columns' | 'getCoreRowModel' | 'state' | 'onStateChange' | 'renderFallbackValue'> { + state?: CoreOptions['state'] + onStateChange?: CoreOptions['onStateChange'] + renderFallbackValue?: CoreOptions['renderFallbackValue'] } -export interface TableProps { +export interface TableProps extends TableOptions { /** * The element or component this component should render as. * @defaultValue 'div' @@ -83,6 +99,11 @@ export interface TableProps { * @link [Guide](https://tanstack.com/table/v8/docs/guide/column-pinning) */ columnPinningOptions?: Omit + /** + * @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-sizing#table-options) + * @link [Guide](https://tanstack.com/table/v8/docs/guide/column-sizing) + */ + columnSizingOptions?: Omit /** * @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-visibility#table-options) * @link [Guide](https://tanstack.com/table/v8/docs/guide/column-visibility) @@ -93,6 +114,11 @@ export interface TableProps { * @link [Guide](https://tanstack.com/table/v8/docs/guide/sorting) */ sortingOptions?: Omit, 'getSortedRowModel' | 'onSortingChange'> + /** + * @link [API Docs](https://tanstack.com/table/v8/docs/api/features/grouping#table-options) + * @link [Guide](https://tanstack.com/table/v8/docs/guide/grouping) + */ + groupingOptions?: Omit /** * @link [API Docs](https://tanstack.com/table/v8/docs/api/features/expanding#table-options) * @link [Guide](https://tanstack.com/table/v8/docs/guide/expanding) @@ -103,6 +129,21 @@ export interface TableProps { * @link [Guide](https://tanstack.com/table/v8/docs/guide/row-selection) */ rowSelectionOptions?: Omit, 'onRowSelectionChange'> + /** + * @link [API Docs](https://tanstack.com/table/v8/docs/api/features/row-pinning#table-options) + * @link [Guide](https://tanstack.com/table/v8/docs/guide/row-pinning) + */ + rowPinningOptions?: Omit, 'onRowPinningChange'> + /** + * @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 + /** + * @link [API Docs](https://tanstack.com/table/v8/docs/api/features/column-faceting#table-options) + * @link [Guide](https://tanstack.com/table/v8/docs/guide/column-faceting) + */ + facetedOptions?: FacetedOptions class?: any ui?: Partial } @@ -120,9 +161,10 @@ export type TableSlots = {