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'orfixed: 'right';fixed: trueis shorthand forfixed: '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.
Basic example
<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 thestickylayout 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
| Prop | Type | Default | Description |
|---|---|---|---|
scroll | object | - | Horizontal and vertical scrolling; see options. Providing y creates a scroll area with a fixed header. |
sticky | boolean | TableSticky | false | Sticky header and summary row; for the object form, see options. |
tableLayout | 'auto' | 'fixed' | - | Table layout mode. |
Column fields
| Field | Type | Default | Description |
|---|---|---|---|
fixed | 'left' | 'right' | true | - | Fixed column position; true is equivalent to 'left'. |
width | number | 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
| Slot | Description |
|---|---|
headerWrapper | Header scroll wrapper in fixed-header mode. |
bodyWrapper | Body scroll wrapper in fixed-header mode. |
fixedCell | Fixed column cell. |
fixedDividerLeft | Left fixed column divider. |
fixedDividerRight | Right fixed column divider. |
fixedShadowLeft | Left fixed column shadow layer. |
fixedShadowRight | Right fixed column shadow layer. |
fixedShadowLeftHidden | Hidden state of the left shadow. |
fixedShadowRightHidden | Hidden 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.
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',
},
},
},
})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.
| Variable | Purpose | antdv | element-plus |
|---|---|---|---|
--vtg-table-bg | Main table background, used by areas such as table and td | var(--color-surface) | var(--color-surface) |
--vtg-table-header-bg | Background of the header, title, footer and summary row | var(--color-surface-hover) | var(--color-surface) |
--vtg-table-border-color | Table border and cell dividers | var(--color-default) | var(--color-default) |
--vtg-table-header-split-color | antdv-style header split line | rgb(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
/** ColumnType 节选;文档辅助接口,不是包导出。 */
export interface FixedColumnFields {
fixed?: 'left' | 'right' | true
width?: number | string
}export type TableLayout = 'auto' | 'fixed'export interface TableSticky {
offsetHeader?: number
offsetSummary?: number
offsetScroll?: number
getContainer?: () => Window | HTMLElement
}/** TableProps 节选;文档辅助接口,不是包导出。 */
export interface ScrollFields {
scroll?: { x?: number | string; y?: number | string; scrollToFirstRowOnChange?: boolean }
sticky?: boolean | TableSticky
tableLayout?: TableLayout
}