Preview Updated 2026-05-10

Data grid

Heavy-duty table with column resize, double-click cell editing, and a sticky header. Built for back-office and data-maintenance workflows.

Basic usage

CfDataGrid extends Table’s column definition with column resizing, cell editing, and a sticky header. The minimal form — configure columns + rows + rowKey — is identical to Table.

ID 姓名 角色 等级
1Alice Chen前端3
2Bob Wang后端2
3Carol Liu设计5
4Dave Zhang运维1
<script setup lang="ts">
import { ref } from 'vue';
import { CfDataGrid, type DataGridColumn } from '@chufix-design/vue';

interface Row { id: string; name: string; role: string; level: number; }

const rows = ref<Row[]>([
{ id: '1', name: 'Alice Chen', role: 'Frontend', level: 3 },
// ...
]);
const columns: DataGridColumn<Row>[] = [
{ key: 'id', title: 'ID', width: 60, sortable: true },
{ key: 'name', title: 'Name', width: 140, sortable: true },
{ key: 'role', title: 'Role', width: 100 },
{ key: 'level', title: 'Level', width: 80, align: 'right', sortable: true },
];
</script>

<template>
<CfDataGrid :columns="columns" :rows="rows" row-key="id" />
</template>
import { CfDataGrid, type DataGridColumn } from '@chufix-design/react';

interface Row { id: string; name: string; role: string; level: number; }
const columns: DataGridColumn<Row>[] = [
{ key: 'id', title: 'ID', width: 60, sortable: true },
{ key: 'name', title: 'Name', width: 140, sortable: true },
{ key: 'role', title: 'Role', width: 100 },
{ key: 'level', title: 'Level', width: 80, align: 'right', sortable: true },
];

<CfDataGrid columns={columns} rows={rows} rowKey="id" />

Column resize

Set resizable: true on a column to let users drag its right edge to change the width. minWidth caps the lower bound so a column can’t be dragged out of sight. Each release fires the columnResize(col, width) event, which you can use to persist the user’s column-width preferences.

ID 姓名 邮箱 角色
1Alice Chen[email protected]前端
2Bob Wang[email protected]后端
3Carol Liu[email protected]设计

把鼠标放到列头右边缘,光标变成 ↔ 即可拖拽。

const columns: DataGridColumn<Row>[] = [
{ key: 'id', title: 'ID', width: 60 },
{ key: 'name', title: 'Name', width: 140, resizable: true, minWidth: 80 },
{ key: 'email', title: 'Email', width: 220, resizable: true, minWidth: 120 },
{ key: 'role', title: 'Role', width: 100, resizable: true },
];

<CfDataGrid :columns="columns" :rows="rows" row-key="id" />
const columns: DataGridColumn<Row>[] = [
{ key: 'id', title: 'ID', width: 60 },
{ key: 'name', title: 'Name', width: 140, resizable: true, minWidth: 80 },
{ key: 'email', title: 'Email', width: 220, resizable: true, minWidth: 120 },
{ key: 'role', title: 'Role', width: 100, resizable: true },
];

<CfDataGrid columns={columns} rows={rows} rowKey="id" />

Cell editing

Set editable: true on a column to enable double-click-to-edit inline mode. Enter commits and fires cellEdit(edit); Esc cancels. The callback receives { row, column, value } so you can write the change back to the data source.

ID 姓名 角色 等级
1Alice Chen前端3
2Bob Wang后端2
3Carol Liu设计5

双击任意单元格进入编辑,Enter 提交,Esc 取消。

function onEdit(edit: DataGridCellEdit<Row>) {
const target = rows.value.find((r) => r.id === edit.row.id);
if (!target) return;
const key = edit.column.dataIndex ?? edit.column.key;
(target as any)[key] = key === 'level' ? Number(edit.value) : edit.value;
}

<CfDataGrid :columns="columns" :rows="rows" row-key="id" @cell-edit="onEdit" />
function onEdit(edit: DataGridCellEdit<Row>) {
setRows((arr) =>
  arr.map((r) =>
    r.id === edit.row.id
      ? { ...r, [edit.column.dataIndex ?? edit.column.key]: edit.value }
      : r,
  ),
);
}

<CfDataGrid columns={columns} rows={rows} rowKey="id" onCellEdit={onEdit} />

Bounded height with sticky header

Pass maxHeight to enable vertical scrolling — the header sticks to the top of the container. Combined with selectable="multiple", this works well for browsing large datasets.

ID 项目 状态
1项目 001进行中
2项目 002已完成
3项目 003待审核
4项目 004进行中
5项目 005已完成
6项目 006待审核
7项目 007进行中
8项目 008已完成
9项目 009待审核
10项目 010进行中
11项目 011已完成
12项目 012待审核
13项目 013进行中
14项目 014已完成
15项目 015待审核
16项目 016进行中
17项目 017已完成
18项目 018待审核
19项目 019进行中
20项目 020已完成
21项目 021待审核
22项目 022进行中
23项目 023已完成
24项目 024待审核
25项目 025进行中
26项目 026已完成
27项目 027待审核
28项目 028进行中
29项目 029已完成
30项目 030待审核
<CfDataGrid
:columns="columns"
:rows="rows"
row-key="id"
:max-height="280"
selectable="multiple"
/>
<CfDataGrid
columns={columns}
rows={rows}
rowKey="id"
maxHeight={280}
selectable="multiple"
/>

API · DataGrid

Inherits all column props from Table (title / width / align / sortable / dataIndex, etc.) and adds:

PropTypeDefaultDescription
maxHeightnumber | stringContainer max height; enables sticky thead and vertical scroll
onCellEdit (React) / cell-edit (Vue)(edit) => voidCell edit commit callback
onColumnResize / column-resize(col, width) => voidColumn-width change callback

DataGridColumn adds to TableColumn:

PropTypeDescription
resizablebooleanWhether the column can be drag-resized
minWidthnumberMinimum drag width
editablebooleanWhether to enable double-click inline editing

The current version does not bundle virtual scroll or column drag-reordering. For datasets larger than 1000 rows, paginate or slice in the layer above and feed the slice into rows.

反馈与讨论

Data grid · Discussion

0
0 / 600
一键发送
正在加载评论...