Skip to content

Table ​

Data table: multi-level headers, sorting, filtering, virtual scrolling and custom cells. Header building (buildHeaderCells), sorting (sortTableRows), filtering (filterTableRows), the virtual window (computeVisibleRange) and the class name / sticky offset computations all come from the @lh-ui/share core pure functions.

Platform differences (important) ​

  • Horizontal scrolling: on H5 the header and body are two independent scroll containers with scrollLeft synced both ways; on the WeChat side a single outer scroll-view (scroll-x) scrolls the whole table horizontally, so header and body share one scroll axis and no sync mechanism is needed.
  • Fixed columns (H5 only): fixed columns rely on position: sticky support on table cells, which is unreliable under WXSS, so the MP fixed-column styles are stripped at build time (the #ifdef H5 wrapper in build-styles) and degrade to plain horizontally scrolled columns.
  • Column resize (H5 only): mousedown and keyboard semantics do not exist on MP, so the drag handles are rendered on H5 only.
  • Stripe rows: the share :nth-child(even) rule is H5-only (WXSS does not support nth pseudo-classes); on MP the template adds an --alt class by row index instead.
  • Virtual scrolling: H5 uses clientHeight + ResizeObserver; MP uses the scroll-view detail.scrollTop + createSelectorQuery.

Basic usage ​

vue
<template>
  <lh-table :data="rows" :columns="columns" />
</template>

<script setup lang="ts">
const rows = [
  { id: 1, name: 'User 1', age: 28, city: 'Beijing' },
  { id: 2, name: 'User 2', age: 34, city: 'Shanghai' },
]
const columns = [
  { prop: 'name', label: 'Name', width: 140 },
  { prop: 'age', label: 'Age', width: 120, align: 'right' },
  { prop: 'city', label: 'City', width: 160 },
]
</script>

Stripe and border ​

vue
<template>
  <lh-table :data="rows" :columns="columns" stripe border />
</template>

Sorting and filtering ​

vue
<template>
  <lh-table
    :data="rows"
    :columns="columns"
    @sort-change="onSort"
    @filter-change="onFilter"
  />
</template>

<script setup lang="ts">
const columns = [
  { prop: 'age', label: 'Age', width: 120, sortable: true },
  {
    prop: 'city',
    label: 'City',
    width: 160,
    filters: [
      { text: 'Beijing', value: 'Beijing' },
      { text: 'Shanghai', value: 'Shanghai' },
    ],
  },
]

function onSort(payload: { prop: string; order?: 'asc' | 'desc' }) {}
function onFilter(payload: { prop: string; values: (string | number)[] }) {}
</script>

Clicking the header of a sortable column cycles none → ascending → descending (cycleSortOrder); a filterable column header carries a filter trigger, and the rows are filtered after checking items in the panel and confirming (the filtering logic runs through share's filterTableRows). The filter state supports both controlled (filtered-values) and uncontrolled (default-filtered-values) modes.

Fixed columns and horizontal scrolling (H5 only) ​

vue
<template>
  <lh-table :data="rows" :columns="columns" />
</template>

<script setup lang="ts">
const columns = [
  { prop: 'name', label: 'Name', width: 140, fixed: 'left' },
  { prop: 'age', label: 'Age', width: 120 },
  { prop: 'city', label: 'City', width: 160 },
  { prop: 'status', label: 'Status', width: 120, fixed: 'right' },
]
</script>

fixed: 'left' / 'right' requires a numeric width; for the MP side see Platform differences above.

Virtual scrolling (ten thousand rows) ​

vue
<template>
  <lh-table :data="hugeRows" :columns="columns" :height="360" />
</template>

Setting height enables virtual scrolling: the table body gets a fixed height and renders only the visible window plus overscan (LH_TABLE_OVERSCAN = 3); row-height must match the actual row height (default 48).

Custom cells ​

vue
<template>
  <lh-table :data="rows" :columns="columns">
    <template #status="{ row }">
      <lh-tag :type="row.status === 'active' ? 'success' : 'danger'">
        {{ row.status === 'active' ? 'Enabled' : 'Disabled' }}
      </lh-tag>
    </template>
  </lh-table>
</template>

<script setup lang="ts">
const columns = [
  { prop: 'name', label: 'Name', width: 140 },
  { prop: 'status', label: 'Status', width: 140, slot: 'status' },
]
</script>

The column config's slot field declares a named scoped slot; the slot params are { row, column, index, value }.

Multi-level headers ​

vue
<script setup lang="ts">
const columns = [
  { prop: 'name', label: 'Name', width: 140 },
  {
    label: 'Group',
    children: [
      { prop: 'age', label: 'Age', width: 120 },
      { prop: 'city', label: 'City', width: 160 },
    ],
  },
]
</script>

The column config's children declares sub-columns; the header rows are laid out per level and positioned with a grid (buildHeaderCells computes the colspan / rowspan).

API ​

Props ​

PropDescriptionTypeDefault
dataRow dataLhTableRow[][]
columnsColumn configLhTableColumn[][]
heightTable body height (enables virtual scrolling when set)number | string-
max-heightMax height in non-virtual modenumber | string-
row-heightRow height for virtual scrollingnumber48
stripeStripe rowsbooleanfalse
borderCell bordersbooleanfalse
sizeSize'small' | 'default' | 'large''default'
row-keyRow key field namestring'id'
default-sort-propDefault sort columnstring-
default-sort-orderDefault sort direction'asc' | 'desc'-
sorterCustom sort function(a, b) => number-
resizableEnable column resize (H5 only)booleanfalse
default-filtered-valuesDefault filter state (uncontrolled)Record<string, (string | number)[]>{}
filtered-valuesControlled filter stateRecord<string, (string | number)[]>-
sxsx quick layout propsLhSxProps-

LhTableColumn ​

PropDescriptionType
propField namestring
labelHeader textstring
widthColumn width (numbers are px)number | string
alignAlignment'left' | 'center' | 'right'
fixedFixed column (H5 only)'left' | 'right'
sortableSortableboolean
resizableResizable (needs to be enabled per column plus a numeric width, H5 only)boolean
filtersFilter options{ text, value }[]
filter-multipleMulti-select filter (true by default)boolean
formatterCell formatter(row, value) => string
slotNamed slot for custom cellsstring
childrenSub-columns (multi-level headers)LhTableColumn[]

Events ​

EventDescriptionParameters
sort-changeThe sort changed(payload: { prop, order })
row-clickA row was clicked(row, index)
filter-changeThe filter changed(payload: { prop, values })
update:filtered-valuesControlled filter update(values)
column-resizeColumn width adjusted (H5 only)(payload: { prop, width })

Slots ​

SlotDescriptionScoped params
[Column slot]Custom cell (name given by the column config slot){ row, column, index, value }
emptyEmpty data content ("No data" by default)-