Skip to main content

Table

A column-config-driven table. Supports sorting, fixed columns, striped rows, custom cells, multi-level headers, filtering, column resizing, and virtual scrolling for large datasets (the visible window is computed by pure core functions — no external library). Structurally identical to lh-ui-plus's LhTable: column normalization, sorting, filtering, and the virtual range all come from the same pure functions in share core; this library only adds the React glue and JSX. The DOM semantics are upgraded to native table / thead / tbody / th / td, with BEM class names identical to plus.

Basic usage​

From top to bottom, the overview demo below shows: a basic table, sorting + fixed columns + stripes, custom cells (render), and pagination linked with LhPagination.

Overview
NameRoleCity
沈砚Architecture杭州
苏晚Frontend苏州
陆离Design北京
墨白Frontend杭州
素笺Architecture苏州
留白Design北京
Last clicked row: (none)
NameRoleCity
沈砚34Architecture杭州
苏晚28Frontend苏州
陆离31Design北京
墨白26Frontend杭州
素笺29Architecture苏州
留白24Design北京
NameRole
沈砚Architecture
苏晚Frontend
陆离Design
墨白Frontend
素笺Architecture
留白Design
NameRoleCity
沈砚Architecture杭州
苏晚Frontend苏州
import { LhTable } from '@lh-ui/react'

export default function Demo() {
const rows = [
{ id: 1, name: 'Shen Yan', role: 'Architect', city: 'Hangzhou' },
{ id: 2, name: 'Su Wan', role: 'Frontend', city: 'Suzhou' },
{ id: 3, name: 'Lu Li', role: 'Designer', city: 'Beijing' },
]
const columns = [
{ prop: 'name', label: 'Name', width: 140 },
{ prop: 'role', label: 'Role' },
{ prop: 'city', label: 'City', width: 120 },
]
return <LhTable data={rows} columns={columns} />
}

Sorting and fixed columns​

Set sortable on a column to enable sorting: the first click starts at asc, clicking the same column cycles asc → desc → off, and switching columns restarts at asc; fixed pins a column (width required). Fixed columns stick via CSS position: sticky and show a shadow when scrolled out of bounds.

import { LhTable } from '@lh-ui/react'

export default function Demo() {
const columns = [
{ prop: 'name', label: 'Name', width: 140, fixed: 'left' },
{ prop: 'age', label: 'Age', width: 100, sortable: true, align: 'center' },
{ prop: 'role', label: 'Role' },
{ prop: 'city', label: 'City', width: 120 },
]
return <LhTable data={rows} columns={columns} stripe border size="small" />
}

Virtual scrolling (large datasets)​

Setting height enables it: only the rows inside the visible window are rendered (3 extra rows buffered above and below), and rowHeight must match the actual row height.

import { LhTable } from '@lh-ui/react'

export default function Demo() {
const bigRows = Array.from({ length: 1000 }, (_, i) => ({
id: i + 1,
name: `Row ${i + 1}`,
role: i % 2 === 0 ? 'Architect' : 'Frontend',
city: 'Hangzhou',
}))
return <LhTable data={bigRows} columns={columns} height={240} rowHeight={48} />
}

Custom cells​

plus's <column.slot> scoped slot maps to the column's render function in React, called with (row, value, index); columns without a render function fall back to the text formatted by formatter.

import { LhTable, LhTag } from '@lh-ui/react'

export default function Demo() {
const columns = [
{ prop: 'name', label: 'Name', width: 140 },
{
prop: 'role',
label: 'Role',
render: (row, value) => (
<LhTag type={row.role === 'Architect' ? 'primary' : 'info'}>{String(value)}</LhTag>
),
},
]
return <LhTable data={rows} columns={columns} />
}

Multi-level headers​

Nesting children in the column config yields multi-level headers, nestable without limit; the body aligns to the leaf columns.

import { LhTable } from '@lh-ui/react'

export default function Demo() {
const columns = [
{ prop: 'name', label: 'Name', width: 140, fixed: 'left' },
{
label: 'Project info',
children: [
{ prop: 'role', label: 'Role' },
{ prop: 'city', label: 'City', width: 120 },
],
},
{ prop: 'age', label: 'Age', sortable: true, align: 'center' },
]
return <LhTable data={rows} columns={columns} border />
}

Filtering​

Once a column configures filters, a filter trigger appears in the header; check options and click "OK" to apply, "Reset" to clear. filterMultiple: false makes it single-select and filterMethod customizes the predicate; for controlled usage pass filteredValues + onFilteredValuesChange.

import { useState } from 'react'
import { LhTable } from '@lh-ui/react'

export default function Demo() {
const [filteredValues, setFilteredValues] = useState({})
const columns = [
{ prop: 'name', label: 'Name', width: 140 },
{
prop: 'role',
label: 'Role',
filters: [
{ text: 'Architect', value: 'Architect' },
{ text: 'Frontend', value: 'Frontend' },
{ text: 'Designer', value: 'Designer' },
],
},
]
return (
<LhTable
data={rows}
columns={columns}
filteredValues={filteredValues}
onFilteredValuesChange={setFilteredValues}
onFilterChange={({ prop, values }) => console.log('filterChange', prop, values)}
/>
)
}

Column resizing​

Enable resizable (the master switch) and mark columns with resizable: true (leaf columns with a numeric width). A drag handle appears at the right edge of the header; once focused, ←/→ adjust by ±10px per step and the drag minimum is 40px.

import { LhTable } from '@lh-ui/react'

export default function Demo() {
const columns = [
{ prop: 'name', label: 'Name', width: 140, resizable: true, fixed: 'left' },
{ prop: 'role', label: 'Role' },
{ prop: 'city', label: 'City', width: 120, resizable: true },
]
return (
<LhTable
data={rows}
columns={columns}
resizable
border
onColumnResize={({ prop, width }) => console.log(`${prop} → ${width}px`)}
/>
)
}

Row key, max height, and default sorting​

rowKey specifies the unique row key (recommended); maxHeight caps the height so the header pins and the body scrolls; sorter provides a custom compare function; defaultSortProp / defaultSortOrder set the initial sorting; defaultFilteredValues sets the initial filtering.

import { LhTable } from '@lh-ui/react'

export default function Demo() {
const columns = [
{ prop: 'name', label: 'Name', width: 140 },
{ prop: 'age', label: 'Age', sortable: true, sorter: (a, b) => a.age - b.age },
{ prop: 'role', label: 'Role' },
]
return (
<LhTable
data={rows}
columns={columns}
rowKey="name"
maxHeight={200}
defaultSortProp="age"
defaultSortOrder="desc"
/>
)
}

Pagination linkage​

LhTable itself does not embed pagination: data paging is driven by LhPagination, which slices the full dataset locally on page change; each of the two pieces keeps its own responsibility (as in the overview demo above).

import { useState } from 'react'
import { LhPagination, LhTable } from '@lh-ui/react'

export default function Demo() {
const [current, setCurrent] = useState(1)
const pageSize = 2
const pagedRows = rows.slice((current - 1) * pageSize, current * pageSize)
return (
<>
<LhTable data={pagedRows} columns={columns} />
<LhPagination total={rows.length} pageSize={pageSize} current={current} onChange={setCurrent} />
</>
)
}

API mapping from plus​

plus's named slots and v-model merge into prop / callback forms in React:

plusReact
The <column.slot> scoped slot ({ row, column, index, value })The column's render(row, value, index) function
The #empty named slotThe empty prop (ReactNode)
update:filteredValues (v-model)The onFilteredValuesChange callback (controlled via filteredValues)
@sort-change / @row-click / @filter-change / @column-resizeonSortChange / onRowClick / onFilterChange / onColumnResize

LhTable Props​

PropDescriptionTypeDefault
dataRow dataLhTableRow[]—
columnsColumn configLhTableColumn[]—
heightBody height (enables virtual scrolling; number = px)string | number—
maxHeightMax height in non-virtual modestring | number—
rowHeightVirtual scrolling row height (must match the actual row height)number48
stripeStriped rowsbooleanfalse
borderCell bordersbooleanfalse
sizeSize'small' | 'default' | 'large''default'
rowKeyRow key field namestring'id'
defaultSortPropDefault sort columnstring—
defaultSortOrderDefault sort order'asc' | 'desc'—
sorterCustom compare function(a, b) => number—
resizableEnable column resizing (column-level resizable still required)booleanfalse
defaultFilteredValuesDefault filter state (uncontrolled, keyed by column prop)Record<string, (string | number)[]>{}
filteredValuesControlled filter state (keyed by column prop)Record<string, (string | number)[]>—
emptyCustom empty state (merging plus's #empty slot)ReactNode—
sxShorthand layout propsSxProps—

LhTableColumn​

FieldDescriptionType
propData field namestring
labelColumn titlestring
widthColumn width (number = px; unset columns share the remaining space)number | string
alignAlignment'left' | 'center' | 'right'
fixedFixed column (width required)'left' | 'right'
sortableSortableboolean
renderCustom cell renderer (merging plus's scoped slot)(row, value, index) => ReactNode
formatterValue formatter (effective when render is absent)(row, value) => string
childrenChild columns (configuring them yields multi-level headers)LhTableColumn[]
filtersColumn filter options (leaf columns only)LhTableFilterOption[]
filterMethodCustom filter predicate (strict prop comparison by default)(value, row) => boolean
filterMultipleWhether the filter panel is multi-selectboolean
resizableWhether the column can be resized (numeric-width leaf columns)boolean

Events​

EventDescriptionSignature
onSortChangeFired when sorting changes (switching columns restarts at asc; the same column cycles asc → desc → off)({ prop, order }) => void
onRowClickFired when a row is clicked (Enter / Space activation included)(row, index) => void
onFilterChangeFired after the filter is committed via "OK" or "Reset"({ prop, values }) => void
onFilteredValuesChangeFired when the filter state changes(values) => void
onColumnResizeFired after a column resize completes (drag end or ←/→)({ prop, width }) => void

Accessibility​

  • The root element is a native <table> and carries row / header / cell semantics out of the box, with aria-rowcount / aria-colcount added (virtual scrolling only renders windowed rows; screen readers learn the full scale from these attributes)
  • Sortable headers are <th> + aria-sort (none / ascending / descending) with real <button> triggers; group headers in multi-level headers express themselves via colspan / rowspan and scope="colgroup"
  • Fixed columns stick via CSS position: sticky (no homemade pinning math)
  • The filter trigger has aria-haspopup + aria-expanded, and the panel is a role="group"
  • The resize handle is a role="separator", focusable, adjusting ±10px with the ←/→ keys
  • Rows are focusable (tabindex="0"); Enter / Space activate them and fire onRowClick
  • The placeholder spacer rows of virtual scrolling carry aria-hidden="true" and are removed from the screen-reader tree

ref​

forwardRef<HTMLTableElement> — the root <table> element.

Styles​

import '@lh-ui/react/theme.css'
import '@lh-ui/react/style/table'