Skip to content

Fixed columns ​

Fixed columns suit wide tables with many fields and noticeable horizontal scrolling. They rely on two things holding at the same time:

  • The table provides scroll.x, which forms a horizontal scroll area
  • The columns to fix declare fixed: 'left' or fixed: 'right'; fixed: true is shorthand for fixed: 'left' (aligned with ant-design-vue)

Live demo ​

A nine-column wide table with the two left columns and the "Status" column on the right fixed. Drag the horizontal scrollbar to see the fixed columns and their shadows.

Edit in Playground

Basic example ​

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

interface UserRow {
  key: string
  name: string
  age: number
  status: string
  address: string
}

const columns: TableColumnsType<UserRow> = [
  { title: 'Name', dataIndex: 'name', key: 'name', width: 160, fixed: 'left' },
  { title: 'Age', dataIndex: 'age', key: 'age', width: 96, align: 'right' },
  { title: 'Status', dataIndex: 'status', key: 'status', width: 140 },
  { title: 'Address', dataIndex: 'address', key: 'address', width: 220, fixed: 'right' },
]

const dataSource: UserRow[] = [
  { key: '1', name: 'Ada', age: 28, status: 'Active', address: 'Shanghai' },
]
</script>

<template>
  <VTable row-key="key" :columns="columns" :data-source="dataSource" :scroll="{ x: 720 }" />
</template>

Common combinations ​

  • Fixed columns + horizontal scrolling
  • Fixed columns + fixed header
  • Fixed columns + row selection / expand column

Key boundaries ​

  • Fixed columns should provide an explicit width, otherwise the sticky layout is more prone to misalignment
  • If there are also grouped headers, keep the column widths stable and use tableLayout="fixed" when necessary
  • Fixed columns can be combined with column resizing, but keep the width boundaries of the fixed columns stable first

When to use ​

  • Users need to keep the key primary columns in view while scrolling horizontally
  • The table has status columns, action columns and a large number of content columns at the same time
  • The page does not suit being split into several tables, yet carries a lot of horizontal information

API ​

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

Props ​

PropTypeDefaultDescription
scrollobject-Horizontal and vertical scrolling; see options. Providing y creates a scroll area with a fixed header.
stickyboolean | TableStickyfalseSticky header and summary row; for the object form, see options.
tableLayout'auto' | 'fixed'-Table layout mode.

Column fields ​

FieldTypeDefaultDescription
fixed'left' | 'right' | true-Fixed column position; true is equivalent to 'left'.
widthnumber | string-Column width; numbers are treated as px.

Theming ​

The classes of the fixed edges, shadows and scroll area can 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
headerWrapperHeader scroll wrapper in fixed-header mode.
bodyWrapperBody scroll wrapper in fixed-header mode.
fixedCellFixed column cell.
fixedDividerLeftLeft fixed column divider.
fixedDividerRightRight fixed column divider.
fixedShadowLeftLeft fixed column shadow layer.
fixedShadowRightRight fixed column shadow layer.
fixedShadowLeftHiddenHidden state of the left shadow.
fixedShadowRightHiddenHidden state of the right shadow.

Preset defaults ​

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

ts
createVTableGuild({
  theme: {
    table: {
      slots: {
        headerWrapper: 'overflow-hidden',
        bodyWrapper: 'overflow-auto',
        fixedCell: 'sticky z-[2] bg-inherit',
        fixedDividerLeft: '',
        fixedDividerRight: '[background-image:linear-gradient(var(--vtg-table-header-split-color),var(--vtg-table-header-split-color))] [background-size:1px_1.6em] [background-repeat:no-repeat] [background-position:left_center] before:!bg-transparent',
        fixedShadowLeft: 'after:absolute after:top-0 after:bottom-[-1px] after:right-0 after:w-[30px] after:translate-x-full after:pointer-events-none after:shadow-[inset_10px_0_8px_-8px_rgba(0,0,0,0.08)] before:!bg-transparent',
        fixedShadowRight: 'after:absolute after:top-0 after:bottom-[-1px] after:left-0 after:w-[30px] after:-translate-x-full after:pointer-events-none after:shadow-[inset_-10px_0_8px_-8px_rgba(0,0,0,0.08)] before:!bg-transparent',
        fixedShadowLeftHidden: 'after:shadow-none',
        fixedShadowRightHidden: 'after:shadow-none',
      },
    },
  },
})
ts
createVTableGuild({
  themePreset: 'element-plus',
  theme: {
    table: {
      slots: {
        headerWrapper: 'overflow-hidden',
        bodyWrapper: 'overflow-auto',
        fixedCell: 'sticky z-[2] bg-inherit',
        fixedDividerLeft: '',
        fixedDividerRight: '',
        fixedShadowLeft: 'after:absolute after:top-0 after:bottom-[-1px] after:right-0 after:w-[30px] after:translate-x-full after:pointer-events-none after:shadow-[inset_10px_0_8px_-8px_rgba(0,0,0,0.06)]',
        fixedShadowRight: 'after:absolute after:top-0 after:bottom-[-1px] after:left-0 after:w-[30px] after:-translate-x-full after:pointer-events-none after:shadow-[inset_-10px_0_8px_-8px_rgba(0,0,0,0.06)]',
        fixedShadowLeftHidden: 'after:shadow-none',
        fixedShadowRightHidden: 'after:shadow-none',
      },
    },
  },
})

CSS variables ​

The table lists the default values for light mode. Dark mode can be overridden separately through .dark.

VariablePurposeantdvelement-plus
--vtg-table-bgMain table background, used by areas such as table and tdvar(--color-surface)var(--color-surface)
--vtg-table-header-bgBackground of the header, title, footer and summary rowvar(--color-surface-hover)var(--color-surface)
--vtg-table-border-colorTable border and cell dividersvar(--color-default)var(--color-default)
--vtg-table-header-split-colorantdv-style header split linergb(0 0 0 / 6%)not read

Type Declarations ​

Fixed columns use ColumnType<TRecord>['fixed'], and the scroll and sticky configuration belongs to TableProps<TRecord>. The props / column configuration are excerpts of the relevant fields; FixedColumnFields and ScrollFields are documentation helper interfaces, not package exports. See Type reference for the full declarations.

View type declarations
ts
/** ColumnType 节选;文档辅助接口,不是包导出。 */
export interface FixedColumnFields {
  fixed?: 'left' | 'right' | true
  width?: number | string
}
ts
export type TableLayout = 'auto' | 'fixed'
ts
export interface TableSticky {
  offsetHeader?: number
  offsetSummary?: number
  offsetScroll?: number
  getContainer?: () => Window | HTMLElement
}
ts
/** TableProps 节选;文档辅助接口,不是包导出。 */
export interface ScrollFields {
  scroll?: { x?: number | string; y?: number | string; scrollToFirstRowOnChange?: boolean }
  sticky?: boolean | TableSticky
  tableLayout?: TableLayout
}