horee-gridb0.0.1

快速开始

从安装到显示第一个表格大约需要 5 分钟。按照本页操作即可,其余指南在需要时查阅。

安装

horee-grid 没有发布到 npm 公共仓库。将购买后收到的安装包文件(horee-grid-版本.tgz)放在项目中进行安装。

# 把收到的文件放到项目的 vendor 文件夹后
npm install ./vendor/horee-grid-0.0.1-beta.1.tgz

package.json 中会留下如下内容。升级时替换文件并再次运行相同的命令。

"dependencies": {
  "horee-grid": "file:vendor/horee-grid-0.0.1-beta.1.tgz"
}
以相同版本号再次收到文件时

版本号相同时,仅运行 npm install 不会换成新文件。请直接运行上面的 npm install ./vendor/…tgz。

安装包内容:

文件内容
dist/horee-grid.jsES 模块(用于 Vite · webpack 等打包工具)
dist/horee-grid.umd.cjsUMD(通过 <script> 引入或 CommonJS)
dist/horee-grid.css默认样式
dist/types/*.d.tsTypeScript 类型定义

第一个表格

只需要三样东西:有高度的元素、列定义、行数组。

<div id="grid" style="height: 400px"></div>
basic.js
import { createGrid } from 'horee-grid'

const grid = createGrid(host, {
  locale: 'zh',
  columns: [
    { field: 'code', title: '代码', width: 80 },
    { field: 'name', title: '物料名称', flex: 1, minWidth: 160 },
    { field: 'qty', title: '数量', type: 'number', width: 90 },
    { field: 'price', title: '单价', type: 'number', width: 110 },
    { field: 'day', title: '入库日期', type: 'date', width: 110 }
  ],
  rows: [
    { code: 'A001', name: '螺栓 M8', qty: 1200, price: 85, day: '2026-11-02' },
    { code: 'A002', name: '螺母 M8', qty: 1500, price: 40, day: '2026-11-02' },
    { code: 'B010', name: '轴承 6204', qty: 64, price: 5300, day: '2026-11-15' },
    { code: 'C120', name: '油封 35x52', qty: 210, price: 1250, day: '2026-11-21' }
  ],
  rowKey: 'code'
})
import { createGrid } from 'horee-grid'
import 'horee-grid/dist/horee-grid.css'

const grid = createGrid(document.getElementById('grid')!, { columns, rows, rowKey: 'code', locale: 'zh' })
  • createGrid(元素, 选项) 返回一个 Grid 对象。之后用这个对象的方法操作(grid.setRows(…)、grid.getSelectedRows() …)。
  • 行就是普通对象。列的 field 就是对象的键。
  • rowKey 是区分行的字段。重新查询替换行时会保留选择和活动单元格,用 select(键) 选择行时也会用到。建议一定要设置。
  • 要用中文显示表格菜单,请加上 locale: 'zh'。参见 语言 · 文字。
本文档中的示例

示例代码中的 host 是放置表格的元素,toolbar 是示例上方的按钮栏。其他代码会在文档中原样运行。

高度

表格会填满传入的元素。如果该元素没有高度,就什么也看不到。

/* 固定高度 */
#grid { height: 400px; }

/* 填满屏幕剩余高度(flex) */
.page { display: flex; flex-direction: column; height: 100vh; }
.page #grid { flex: 1; min-height: 0; }
横向滚动超出表格时

在 flex · grid 布局中,容器可能会被撑宽到所有列宽之和。给放置表格的元素设置 min-width: 0,横向滚动就只会出现在表格内部。

替换数据、销毁

grid.setRows(newRows)      // 替换为查询结果。rowKey 相同的行保持选中
grid.setColumns(newCols)   // 替换列定义
grid.refresh()             // 在外部修改行的值后重新计算并绘制
grid.destroy()             // 离开页面时。清除所有事件和元素

在 Vue 中使用

没有官方的 Vue 组件。核心与框架无关,薄薄地包一层即可。不要在每个页面都包一次,在项目中放一个公共组件。

<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { createGrid, type ColumnDef, type Grid, type Row } from 'horee-grid'
import 'horee-grid/dist/horee-grid.css'

const props = defineProps<{ columns: ColumnDef[]; rows: Row[]; rowKey?: string }>()
const el = ref<HTMLDivElement>()
let grid: Grid | undefined

onMounted(() => {
  grid = createGrid(el.value!, { columns: props.columns, rows: props.rows, rowKey: props.rowKey, locale: 'zh' })
})
watch(() => props.rows, rows => grid?.setRows(rows))
watch(() => props.columns, cols => grid?.setColumns(cols))
onBeforeUnmount(() => grid?.destroy())

defineExpose({ grid: () => grid })
</script>

<template><div ref="el" style="height: 100%; min-width: 0" /></template>
不要传入响应式代理

如果 rows 放在 reactive · ref 中,表格会收到代理对象而变慢。大列表请放在 shallowRef 中,或传入 toRaw(rows)。

在 React 中使用

import { useEffect, useRef } from 'react'
import { createGrid, type ColumnDef, type Grid, type Row } from 'horee-grid'
import 'horee-grid/dist/horee-grid.css'

export function HoreeGrid({ columns, rows, rowKey }: { columns: ColumnDef[]; rows: Row[]; rowKey?: string }) {
  const el = useRef<HTMLDivElement>(null)
  const grid = useRef<Grid>()

  useEffect(() => {
    grid.current = createGrid(el.current!, { columns, rows, rowKey, locale: 'zh' })
    return () => grid.current?.destroy()
  }, [])                                              // 只创建一次
  useEffect(() => grid.current?.setRows(rows), [rows])
  useEffect(() => grid.current?.setColumns(columns), [columns])

  return <div ref={el} style={{ height: '100%', minWidth: 0 }} />
}

如果事件选项(如 onCellChange)需要读取最新状态,请把回调放在 useRef 中传入。

用 script 标签使用

在没有打包工具的页面中引入 UMD 文件。全局名称是 HoreeGrid。

<link rel="stylesheet" href="/lib/horee-grid.css">
<script src="/lib/horee-grid.umd.cjs"></script>
<script>
  const grid = HoreeGrid.createGrid(document.getElementById('grid'), { columns, rows, locale: 'zh' })
</script>

设置许可证密钥

在应用启动时设置 一次。已创建的表格也会立即重新验证。

import { setLicenseKey } from 'horee-grid'

setLicenseKey(import.meta.env.VITE_HOREE_GRID_KEY)
  • localhost · 127.0.0.1 等开发地址不需要密钥。
  • 没有密钥或密钥不匹配时,表格也 不会停止工作。 右下角会出现提示条,控制台会记录一次原因。
  • 详见 许可证密钥。

下一步

想做的事查看
数字千分位、日期格式、固定列、表头分组列
编辑单元格并保存编辑
从 Excel 粘贴、下载为 Excel 文件复制 · 粘贴 · Excel
数十万条的列表服务器数据
所有选项和方法API