Skip to content

Custom rows and slots ​

These features let you wire the table into more complex application interfaces.

When the default column rendering is no longer enough but you do not want to give up sorting, filtering, alignment and the theme system, entry points such as customRow, customHeaderRow, headerCell, bodyCell and summary are a better fit.

Live demo ​

headerCell changes the header text, bodyCell turns the status into a badge, and customRow attaches a click event to the whole row.

Edit in Playground

Common entry points ​

vue
<script setup lang="ts">
import { ref } from 'vue'
import { VTable, type TableColumnsType, type TableProps } from '@vtable-guild/vtable-guild'

interface ReportRow {
  key: string
  status: 'active' | 'paused'
  score: number
}

const columns: TableColumnsType<ReportRow> = [
  { title: 'Status', key: 'status', dataIndex: 'status' },
  { title: 'Score', key: 'score', dataIndex: 'score' },
]
const dataSource: ReportRow[] = [{ key: '1', status: 'active', score: 95 }]
const clickedRowKey = ref('')
const rowClassName: NonNullable<TableProps<ReportRow>['rowClassName']> = (record) =>
  record.status === 'paused' ? 'paused-row' : ''
const customRow: NonNullable<TableProps<ReportRow>['customRow']> = (record) => ({
  onClick: () => {
    clickedRowKey.value = record.key
  },
})
const customHeaderRow: NonNullable<TableProps<ReportRow>['customHeaderRow']> = (
  _columns,
  index,
) => ({ 'data-header-row': index ?? 0 })
</script>

<template>
  <VTable
    row-key="key"
    :columns="columns"
    :data-source="dataSource"
    :row-class-name="rowClassName"
    :custom-row="customRow"
    :custom-header-row="customHeaderRow"
  >
    <template #headerCell="{ title, column }">
      <span v-if="column.key === 'score'">{{ title }} metric</span>
      <template v-else>{{ title }}</template>
    </template>

    <template #bodyCell="{ text, column }">
      <span v-if="column.key === 'status'" class="badge">{{ text }}</span>
      <template v-else>{{ text }}</template>
    </template>

    <template #summary>
      <tr>
        <td colspan="2">Summary</td>
      </tr>
    </template>
  </VTable>
  <p>Last clicked: {{ clickedRowKey }}</p>
</template>

When you declare callbacks separately, use the type of the corresponding property of TableProps<ReportRow>; for column callbacks, use ColumnType<ReportRow>. headerCell and bodyCell are declared by TableSlotsDecl<ReportRow>, and record keeps the application row type. bodyCell.text is unknown; when you need a concrete type, narrow it first or read a record field whose type is known.

What each entry point is for ​

  • rowClassName: returns a class based on the row data.
  • customRow: injects attributes, events and style into a row.
  • customHeaderRow: injects attributes into a header row.
  • customHeaderCell: adds attributes to a header cell.
  • headerCell / bodyCell: replace the content inside a cell while keeping the table's built-in interactions.
  • summary: appends a summary area after the body.
  • paginationItem: shows application content such as the selection count or a refresh button at the position specified by the pagination layout.

Application content in the pagination row ​

Place { slot: 'name' } with pagination.layout, then tell the content apart by the name of #paginationItem. The selection state is still maintained by the parent component:

vue
<VTable
  :columns="columns"
  :data-source="dataSource"
  :row-selection="rowSelection"
  :pagination="{
    layout: {
      left: [{ slot: 'selection' }],
      right: ['prev', 'pager', 'next'],
    },
  }"
>
  <template #paginationItem="{ name }">
    <span v-if="name === 'selection'">{{ selectedRowKeys.length }} selected</span>
  </template>
</VTable>

The slot also receives current, pageSize, total, pageCount, range, disabled, position and region, typed as TablePaginationSlotProps. Application buttons bind their own disabled state and call the load function themselves. When the pagination is hidden, the extension content is hidden too; put actions that must remain when there is no data in title, footer or an external toolbar. See Pagination layout and content slots for a complete example.

When to use ui and when to use slots ​

  • To change only styles, prefer ui and theme overrides.
  • Use slots and customRow only when you need to change the content structure or inject application events.
  • When you need to attach attributes dynamically per record, prefer customRow over manipulating the DOM.

Usage tips ​

  • Do structural extensions through the entry points the component provides wherever possible; do not query and rewrite the DOM by hand after mounted.
  • bodyCell is better suited to content-level enhancements such as status tags, icons and supplementary text.
  • Cell or whole-row editing is also composed through bodyCell; drafts, validation and submission are managed by the application. See Editing for complete examples.
  • If the same kind of style appears repeatedly, move it into the theme and ui first instead of repeating slot templates in every table.

API ​

TRecord is the application row type. See the API Reference for the full table configuration.

Props ​

PropTypeDefaultDescription
rowClassNamestring | RowClassName<TRecord>-Adds a class to body rows.
customRowGetComponentProps<TRecord>-Injects attributes, events and styles into body rows.
customHeaderRow(columns: (ColumnType<TRecord> | ColumnGroupType<TRecord>)[], index?: number) => CellAdditionalProps-Injects attributes, events and styles into header rows.
uiSlotProps<{ slots: Record<TableSlots, string> }>-Theme slot class overrides for this table instance. For the full slot list, see the ui slot reference.

Column fields ​

FieldTypeDefaultDescription
customRender(ctx: CustomRenderContext<TRecord>) => VNodeChild | RenderedCell-Custom body cell content. Returning a RenderedCell sets both the content and the cell attributes.
customCellGetComponentProps<TRecord, TRecord>-Injects attributes, events and styles into body cells.
customHeaderCellGetComponentProps<ColumnType<TRecord> | ColumnGroupType<TRecord>, TRecord>-Injects attributes, events and styles into header cells.

Slots ​

SlotPayload typeDescription
bodyCellTableBodyCellSlotProps<TRecord>Custom cell content.
headerCellTableHeaderCellSlotProps<TRecord>Custom header cell content.
summary()Custom summary area.
empty()Custom empty state.
loading()Custom loading state.
customFilterDropdownCustomFilterDropdownSlotProps<TRecord>Table-level custom filter panel; the column must set customFilterDropdown: true.
customFilterIcon{ column: ColumnType<TRecord>; filtered: boolean }Table-level custom filter icon.
paginationItemTablePaginationSlotPropsRenders application content for the pagination at { slot: 'name' } in pagination.layout.

Theming ​

Custom slots provide the content, while the classes of the table's outer structure can still be overridden through the per-instance ui or the global theme.table.slots. To adjust colors or sizes, use the CSS variables below. See Three-layer theme overrides for the override order.

ui slots ​

SlotDescription
headerCellInnerFlex wrapper inside the header cell.
emptyEmpty state root container.
emptyWrapperEmpty state content wrapper.
emptyIconEmpty state icon area.
emptyTextEmpty state text.
loadingLoading mask covering the whole table.
loadingSpinnerLoading icon.

Preset defaults ​

Use the tabs below to switch between the default classes of the two presets. For how to override utilities in the default precompiled mode, see Prefix and override rules.

ts
createVTableGuild({
  theme: {
    table: {
      slots: {
        empty: 'text-center text-[color:var(--color-text-disabled)]',
        loading: 'absolute inset-0 z-[4] flex items-center justify-center bg-[color:var(--vtg-table-loading-overlay-bg)]',
        headerCellInner: "flex min-w-0 items-center [&[data-align=center]]:before:content-[''] [&[data-align=center]]:before:shrink-0 [&[data-align=center]]:before:w-[var(--vtg-header-title-spacer,0px)]",
        emptyWrapper: 'my-[var(--vtg-table-empty-margin-block)] flex flex-col items-center justify-center text-center',
        emptyIcon: 'mb-[var(--vtg-table-empty-image-margin-bottom)] h-[var(--vtg-table-empty-image-height)] opacity-[var(--vtg-table-empty-image-opacity)] [&>svg]:mx-auto [&>svg]:h-full [&>svg]:w-auto',
        emptyText: 'm-0 text-[length:var(--vtg-table-font-size)] leading-[var(--vtg-table-line-height)] text-[color:var(--color-text-disabled)]',
        loadingSpinner: 'inline-flex items-center justify-center text-[length:20px] leading-none text-[color:var(--color-primary)]',
      },
    },
  },
})
ts
createVTableGuild({
  themePreset: 'element-plus',
  theme: {
    table: {
      slots: {
        empty: 'py-5 text-center text-[color:var(--color-muted)]',
        loading: 'absolute inset-0 z-[2] flex items-center justify-center bg-[color:var(--vtg-table-loading-overlay-bg)]',
        headerCellInner: 'flex min-w-0 items-center [&[data-align=center]]:justify-center [&[data-align=right]]:justify-end',
        emptyWrapper: 'flex flex-col items-center justify-center',
        emptyIcon: 'mb-2',
        emptyText: 'm-0 text-[color:var(--color-muted)] text-sm leading-[60px]',
        loadingSpinner: 'inline-flex items-center justify-center text-[length:42px] text-[color:var(--color-primary)] animate-spin',
      },
    },
  },
})

CSS variables ​

The table lists the defaults in light mode. Dark mode can be overridden separately under .dark.

VariablePurposeantdvelement-plus
--vtg-table-bgMain table background, used by areas such as table and tdvar(--color-surface)var(--color-surface)
--vtg-table-text-colorBody text colorvar(--color-on-surface)var(--color-text-regular)
--vtg-table-loading-overlay-bgLoading mask backgroundrgb(255 255 255 / 40%)rgb(255 255 255 / 90%)
--vtg-table-empty-margin-blockEmpty state vertical margin32pxnot read
--vtg-table-empty-image-heightEmpty state image height40pxnot read
--vtg-table-empty-image-margin-bottomGap between the empty state image and text8pxnot read
--vtg-table-empty-image-opacityEmpty state image opacity1not read
--vtg-table-empty-border-colorantdv empty state illustration border color#d9d9d9not read
--vtg-table-empty-shadow-colorantdv empty state illustration shadow color#f0f0f0not read
--vtg-table-empty-content-colorantdv empty state illustration fill color#f5f5f5not read

Type Declarations ​

Table-level callbacks belong to TableProps<TRecord> and column-level callbacks belong to ColumnType<TRecord>; all Vue slots are declared together by TableSlotsDecl<TRecord>. CellAdditionalProps describes the return value for attributes and mouse events. The props / column configuration are excerpts of the relevant fields; CustomStructureFields and MergedColumnFields are documentation helper interfaces, not package exports. See the Type reference for the full declarations and Pagination for the full context of the pagination slot.

View type declarations
ts
/** TableProps 节选;文档辅助接口,不是包导出。 */
export interface CustomStructureFields<TRecord extends object = Record<string, any>> {
  rowClassName?: string | RowClassName<TRecord>
  customRow?: GetComponentProps<TRecord>
  customHeaderRow?: (
    columns: Array<ColumnType<TRecord> | ColumnGroupType<TRecord>>,
    index?: number,
  ) => CellAdditionalProps
  transformCellText?: TransformCellText<TRecord>
}
ts
/** ColumnType 节选;文档辅助接口,不是包导出。 */
export interface MergedColumnFields<TRecord extends object = Record<string, any>> {
  key?: Key
  title?: ColumnTitle<TRecord>
  dataIndex?: DataIndex
  colSpan?: number
  customRender?: (ctx: CustomRenderContext<TRecord>) => VNodeChild | RenderedCell
  customCell?: GetComponentProps<TRecord, TRecord>
  customHeaderCell?: GetComponentProps<ColumnType<TRecord> | ColumnGroupType<TRecord>, TRecord>
}
ts
export type RowClassName<TRecord = Record<string, any>> = (
  record: TRecord,
  index: number,
  indent: number,
) => string
ts
export type GetComponentProps<TData, TRecord extends object = Record<string, any>> = (
  data: TData,
  index: number,
  column?: ColumnType<TRecord>,
) => CellAdditionalProps | undefined
ts
export interface CellAdditionalProps {
  class?: string
  className?: string
  style?: CSSProperties
  colSpan?: number
  rowSpan?: number
  colspan?: number
  rowspan?: number
  onClick?: (event: MouseEvent) => void
  onMouseenter?: (event: MouseEvent) => void
  onMouseleave?: (event: MouseEvent) => void
  [key: string]: unknown
}
ts
export interface TableSlotsDecl<TRecord extends object = Record<string, any>> {
  bodyCell?: (props: TableBodyCellSlotProps<TRecord>) => VNodeChild
  headerCell?: (props: TableHeaderCellSlotProps<TRecord>) => VNodeChild
  empty?: () => VNodeChild
  loading?: () => VNodeChild
  customFilterDropdown?: (props: CustomFilterDropdownSlotProps<TRecord>) => VNodeChild
  customFilterIcon?: (props: { column: ColumnType<TRecord>; filtered: boolean }) => VNodeChild
  title?: (props: TableDataSlotProps<TRecord>) => VNodeChild
  footer?: (props: TableDataSlotProps<TRecord>) => VNodeChild
  paginationItem?: (props: TablePaginationSlotProps) => VNodeChild
  summary?: () => VNodeChild
  expandColumnTitle?: () => VNodeChild
}
ts
export interface TableBodyCellSlotProps<TRecord extends object = Record<string, any>> {
  text: unknown
  record: TRecord
  index: number
  column: ColumnType<TRecord>
}
ts
export interface TableHeaderCellSlotProps<TRecord extends object = Record<string, any>> {
  title: VNodeChild | undefined
  column: ColumnType<TRecord> | ColumnGroupType<TRecord>
  index: number
}
ts
export interface TableDataSlotProps<TRecord extends object = Record<string, any>> {
  data: TRecord[]
}
ts
export interface CustomFilterDropdownSlotProps<TRecord extends object = Record<string, any>> {
  column: ColumnType<TRecord>
  selectedKeys: (string | number | boolean)[]
  setSelectedKeys: (keys: (string | number | boolean)[]) => void
  confirm: (options?: { closeDropdown?: boolean }) => void
  clearFilters: (options?: { confirm?: boolean; closeDropdown?: boolean }) => void
  filters: ColumnFilterItem[]
  visible: boolean
  close: () => void
}