Table 表格
列配置式表格。支援排序、固定欄、斑馬紋、自訂儲存格、多級表頭、篩選與欄寬拖曳,
以及大資料量的虛擬滾動(core 純函式計算可視視窗,自研不引庫)。
與 lh-ui-plus 的 LhTable 同構:欄歸一化、排序、篩選與虛擬區間全部來自
share core 的同一套純函式,本庫只寫 React 膠水與 JSX;DOM 語義升級為原生
table / thead / tbody / th / td,類名 BEM 與 plus 一致。
基礎用法
下方綜合範例由上而下依序為:基礎表格、排序 + 固定欄 + 斑馬紋、自訂儲存格(render)、
與 LhPagination 的分頁聯動。
| 名稱 | 職責 | 城市 |
|---|---|---|
| 沈砚 | 架構 | 杭州 |
| 苏晚 | 前端 | 苏州 |
| 陆离 | 設計 | 北京 |
| 墨白 | 前端 | 杭州 |
| 素笺 | 架構 | 苏州 |
| 留白 | 設計 | 北京 |
| 名稱 | 職責 | 城市 | |
|---|---|---|---|
| 沈砚 | 34 | 架構 | 杭州 |
| 苏晚 | 28 | 前端 | 苏州 |
| 陆离 | 31 | 設計 | 北京 |
| 墨白 | 26 | 前端 | 杭州 |
| 素笺 | 29 | 架構 | 苏州 |
| 留白 | 24 | 設計 | 北京 |
| 名稱 | 職責 |
|---|---|
| 沈砚 | 架構 |
| 苏晚 | 前端 |
| 陆离 | 設計 |
| 墨白 | 前端 |
| 素笺 | 架構 |
| 留白 | 設計 |
| 名稱 | 職責 | 城市 |
|---|---|---|
| 沈砚 | 架構 | 杭州 |
| 苏晚 | 前端 | 苏州 |
import { LhTable } from '@lh-ui/react'
export default function Demo() {
const rows = [
{ id: 1, name: '沈硯', role: '架構', city: '杭州' },
{ id: 2, name: '蘇晚', role: '前端', city: '蘇州' },
{ id: 3, name: '陸離', role: '設計', city: '北京' },
]
const columns = [
{ prop: 'name', label: '名稱', width: 140 },
{ prop: 'role', label: '職責' },
{ prop: 'city', label: '城市', width: 120 },
]
return <LhTable data={rows} columns={columns} />
}
排序與固定欄
列配置 sortable 開啟排序:點擊表頭從 asc 開始,同欄循環 asc → desc → 取消,
換欄重新從 asc 開始;fixed 固定欄(需配合 width),固定欄用 CSS position: sticky
吸附,滾出邊界時出現投影。
import { LhTable } from '@lh-ui/react'
export default function Demo() {
const columns = [
{ prop: 'name', label: '名稱', width: 140, fixed: 'left' },
{ prop: 'age', label: '年齡', width: 100, sortable: true, align: 'center' },
{ prop: 'role', label: '職責' },
{ prop: 'city', label: '城市', width: 120 },
]
return <LhTable data={rows} columns={columns} stripe border size="small" />
}
虛擬滾動(大資料量)
設定 height 即啟用:只渲染可視視窗內的列(上下各多渲染 3 列緩衝),
rowHeight 需與實際列高一致。
import { LhTable } from '@lh-ui/react'
export default function Demo() {
const bigRows = Array.from({ length: 1000 }, (_, i) => ({
id: i + 1,
name: `列 ${i + 1}`,
role: i % 2 === 0 ? '架構' : '前端',
city: '杭州',
}))
return <LhTable data={bigRows} columns={columns} height={240} rowHeight={48} />
}
自訂儲存格
plus 的 <column.slot> 作用域插槽在 React 映射為欄的 render 渲染函式,
入參 (row, value, index);未提供 render 的欄按 formatter 格式化後的文字渲染。
import { LhTable, LhTag } from '@lh-ui/react'
export default function Demo() {
const columns = [
{ prop: 'name', label: '名稱', width: 140 },
{
prop: 'role',
label: '職責',
render: (row, value) => (
<LhTag type={row.role === '架構' ? 'primary' : 'info'}>{String(value)}</LhTag>
),
},
]
return <LhTable data={rows} columns={columns} />
}
多級表頭
列配置嵌套 children 即得多級表頭,可無限嵌套;表體按葉子欄對齊。
import { LhTable } from '@lh-ui/react'
export default function Demo() {
const columns = [
{ prop: 'name', label: '名稱', width: 140, fixed: 'left' },
{
label: '專案資訊',
children: [
{ prop: 'role', label: '職責' },
{ prop: 'city', label: '城市', width: 120 },
],
},
{ prop: 'age', label: '年齡', sortable: true, align: 'center' },
]
return <LhTable data={rows} columns={columns} border />
}
篩選
列配置 filters 後表頭出現篩選觸發器;勾選選項後點「確定」生效、「重置」清空。
filterMultiple: false 為單選,filterMethod 自訂判定;受控用法傳
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: '名稱', width: 140 },
{
prop: 'role',
label: '職責',
filters: [
{ text: '架構', value: '架構' },
{ text: '前端', value: '前端' },
{ text: '設計', value: '設計' },
],
},
]
return (
<LhTable
data={rows}
columns={columns}
filteredValues={filteredValues}
onFilteredValuesChange={setFilteredValues}
onFilterChange={({ prop, values }) => console.log('filterChange', prop, values)}
/>
)
}
欄寬拖曳
開啟 resizable(總開關)並在欄上標記 resizable: true(葉子欄且為數字 width)。
表頭右緣出現拖曳把手,聚焦後 ←/→ 每次 ±10px,拖曳下限 40px。
import { LhTable } from '@lh-ui/react'
export default function Demo() {
const columns = [
{ prop: 'name', label: '名稱', width: 140, resizable: true, fixed: 'left' },
{ prop: 'role', label: '職責' },
{ prop: 'city', label: '城市', width: 120, resizable: true },
]
return (
<LhTable
data={rows}
columns={columns}
resizable
border
onColumnResize={({ prop, width }) => console.log(`${prop} → ${width}px`)}
/>
)
}
列鍵、最大高度與預設排序
rowKey 指定列唯一鍵(推薦);maxHeight 限高後表頭吸頂、表體滾動;
sorter 自訂排序函式;defaultSortProp / defaultSortOrder 設定初始排序;
defaultFilteredValues 設定初始篩選。
import { LhTable } from '@lh-ui/react'
export default function Demo() {
const columns = [
{ prop: 'name', label: '名稱', width: 140 },
{ prop: 'age', label: '年齡', sortable: true, sorter: (a, b) => a.age - b.age },
{ prop: 'role', label: '職責' },
]
return (
<LhTable
data={rows}
columns={columns}
rowKey="name"
maxHeight={200}
defaultSortProp="age"
defaultSortOrder="desc"
/>
)
}