npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@l1yp/data-table

v0.1.5

Published

Feature-rich Vue 3 data table built on TanStack Table

Readme

@l1yp/data-table

可复用的 Vue 3 数据表格组件,基于 TanStack Table v8,支持客户端/服务端模式、虚拟滚动、排序筛选、分组、树形、列固定/调整、行拖拽、行内编辑、单元格选区、右键菜单和 Excel 导出。

安装

pnpm add @l1yp/data-table

在应用入口引入样式:

import "@l1yp/data-table/style.css";

最小用法

<script setup lang="ts">
import { DataTable } from "@l1yp/data-table";
import type { ColumnDef } from "@tanstack/vue-table";

type Row = { id: string; name: string };
const columns: ColumnDef<Row>[] = [
  { accessorKey: "id", header: "ID" },
  { accessorKey: "name", header: "名称" },
];
const data: Row[] = [];
</script>

<template><DataTable :columns="columns" :data="data" caption="数据列表" /></template>

完整 Props、Emits、Slots、Expose、服务端分页和 feature 开关说明见 src/README.md。demo/ 是一个独立 consumer 示例,可用 pnpm demo:dev 启动。

独立滚动容器

Scrollbar 可以包裹列表、面板或任何需要横向或纵向滚动的内容。它与 DataTable 使用同一套滚动条。浏览器已启用覆盖式滚动条时使用原生样式,经典模式则使用不占布局空间的悬浮滑块。悬停滑块会显示无描边的轨道,拖动时保持展开。

<script setup lang="ts">
import { useTemplateRef } from "vue";
import { Scrollbar, type ScrollbarInstance } from "@l1yp/data-table";

const scrollbar = useTemplateRef<ScrollbarInstance>("scrollbar");
</script>

<template>
  <button type="button" @click="scrollbar?.scrollTo({ top: 0, behavior: 'smooth' })">回到顶部</button>
  <Scrollbar ref="scrollbar" :max-height="240" aria-label="活动记录">
    <p v-for="item in 30" :key="item">活动 {{ item }}</p>
  </Scrollbar>
</template>

| 参数 | 用法 | | --- | --- | | height、max-height | 数字按 px 处理,字符串使用 CSS 尺寸。max-height 允许容器随内容增长,达到上限后滚动。 | | native | 强制使用浏览器滚动条。默认自动选择。 | | always | 让自定义滑块在有溢出的方向常显。 | | wrap-class、wrap-style | 设置实际滚动容器的 class 和 style。 | | view-class、view-style、tag | 设置插槽内容层的 class、style 和 HTML 标签,默认标签为 div。 | | aria-label、tabindex | 设置实际滚动容器的无障碍名称和焦点顺序。 |

scroll 事件提供 ScrollbarScrollEvent 类型的 { scrollTop, scrollLeft }。组件实例提供 wrapRef、scrollTo(options)、scrollTo(x, y)、setScrollTop(top)、setScrollLeft(left) 和 update()。组件自动跟踪尺寸变化,需要手动重测时可调用 update()。

编辑联动

开启 features.editing 并通过 getRowId 指定稳定的行 ID 后,在下游列的 meta.editor 中声明 dependsOn,用 options 的第二个参数读取同一行的字段。例如账单的 category 列:

const categoryColumn: ColumnDef<Bill> = {
  accessorKey: "category",
  header: "分类",
  meta: {
    editor: {
      type: "enum",
      dependsOn: ["type"],
      options: (_search, { getValue }) => {
        switch (getValue("type")) {
          case "expense": return [{ value: "food", label: "餐饮" }];
          case "income": return [{ value: "salary", label: "工资" }];
          default: return [];
        }
      },
    },
  },
};
  • getValue 接收列 ID,优先读取整行编辑草稿,再读取暂存改动或原值;不会读取其他行。enum 和 person 都支持行相关选项,原有静态数组和 options(search) 写法不变。搜索词为空时,选项函数应返回该行的完整合法列表。
  • dependsOn 可包含多个列 ID,支持多级依赖,但不要配置循环。上游改变时,仍合法的选值保留,失效的选值清空为 "",并和触发修改一起撤销或重做。批量粘贴、填充会先处理同批次的上游值。
  • 文本、数字、日期也可作为上游字段。各类编辑器的 validate(value, row) 可读取同行的编辑值,例如根据账单类型校验金额。若分类必填,可在分类列拒绝空值以约束整行保存;单格切换类型仍允许先清空分类、再另选,业务保存接口应自行校验必填和分类归属。

动态编辑配置与自定义组件

meta.editor.resolve({ getValue, rowId }) 可按行覆盖编辑配置,不限于选项。例如金额列可以根据账单类型切换 placeholder、步进值和校验逻辑,未选类型时还可以返回 { disabled: true }。如果同一次粘贴或填充也会修改上游列,请用 dependsOn 声明依赖顺序。

type: "custom" 可接入自己的 Vue 编辑组件,并用 componentProps 传入任意组件属性:

const amountEditor: ColumnEditor = {
  type: "custom",
  component: MoneyInput,
  componentProps: { showSymbol: true, precision: 2 },
  dependsOn: ["type", "currency"],
  resolve: ({ getValue }) => ({
    disabled: !getValue("type"),
    componentProps: {
      currency: getValue("currency"),
      allowNegative: getValue("type") === "expense",
    },
  }),
};
  • 回调读取同行草稿、暂存值或原值,不修改原列定义。返回的编辑属性覆盖基础值,componentProps 按属性合并;基础编辑器的 type 和转换规则固定。
  • 自定义组件通过 modelValue / update:modelValue 与表格同步;对象值请发出新引用。组件可发出 commit 或 cancel 结束编辑;自定义组件不由表格自动处理失焦提交。若需接收剪贴板文本,配置 fromInput 将文本转成组件的模型值。
  • disabled 阻止直接编辑、粘贴和填充,不清除数据;整行中变为禁用前的草稿会保留,禁用列不参与整行校验。这不是业务权限控制。

显示密度

通过 density="default"、density="compact" 或 density="dense" 切换整张表格的密度,运行时修改同样生效。工具栏、分页、行内编辑、保存条、分组栏、空态及弹出菜单会一起调整;桌面和移动端使用相同档位。

| 密度 | 行高 / 表头高度 | 控件 / 表头菜单按钮 | 表头按钮上下留白 | 筛选控件 | 正文字号 | | --- | --- | --- | --- | --- | --- | | default 默认 | 48px | 32px | 各 8px | 28px | 14px | | compact 紧凑 | 40px | 28px | 各 6px | 28px | 13px | | dense 密集 | 32px | 24px | 各 4px | 24px | 12px |

表头筛选控件上下固定保留各 4px;Tabs 与后续内容之间至少保留 16px,不随密度缩小。自定义筛选插槽无需设置高度,复合控件的附加按钮可用 data-field-control 标记以统一尺寸。表格通过 --dt-* CSS 变量提供密度尺寸,内置 Portal 菜单会继承所属表格的当前档位,多张表格之间相互独立。

运行 pnpm build 后再运行 pnpm demo:dev,可在 consumer 示例中切换三档密度,检查筛选、五类编辑器、列菜单、右键子菜单和本地模拟服务端分页。

Excel 导出

内置轻量 XLSX 写入器,按需生成 Open XML 与 ZIP,不依赖 ExcelJS 或第三方压缩库。现有 exportExcel(options)、工具栏导出和右键菜单接口保持一致,支持行范围、指定列、自定义表头与取值、列宽、表头加粗,以及字符串、数字、布尔、空值和日期。

await tableRef.value?.exportExcel({
  filename: "账户流水",
  sheetName: "账户流水",
  scope: "all",
  workerThreshold: 5000,
});

默认 5000 行起使用 Worker,小导出在主线程分块生成,均保留进度反馈。浏览器支持 CompressionStream('deflate-raw') 时使用原生压缩,否则输出未压缩的合法 XLSX,文件会更大;Worker 不可用时自动回退主线程。无需在消费项目中复制 Worker 文件。

导出能力面向单工作表数据,不包含读取或修改已有工作簿、多工作表、图片、合并单元格和复杂样式。日期按 UTC 写入 Excel 日期序号,保留日期时间格式,支持 1900–9999 年;以 = 开头的字符串保持文本。工作表名称会清理非法字符并截到 31 字符。最多 1,048,576 行(包含可选表头)、16,384 列、每个文本单元格 32,767 个 UTF-16 码元,超限明确报错;NaN / Infinity 导出为文本,无效或超范围日期报告错误。浏览器仍需容纳取值矩阵和文件数据,极大数据集建议后端导出。

2026-11-17 在同一依赖环境中替换前后执行 pnpm build,按实际字节数及 Node zlib.gzipSync 默认参数测量(kB = 1000 bytes):

| 库 ES 构建产物 | ExcelJS 版本 | 内置写入器版本 | | --- | ---: | ---: | | 按需导出块 / gzip | 1,298.37 / 285.66 kB | 8.24 / 3.26 kB | | Worker 代码与加载块 / gzip | 938.76 / 256.99 kB | 8.35 / 3.47 kB | | 主入口 / gzip | 239.30 / 62.55 kB | 239.17 / 62.52 kB |

原先已按需加载 ExcelJS,因此减少的主要是导出模块、Worker 和安装依赖,首屏主入口体积基本一致。测试覆盖独立 ZIP 解压与 CRC 校验、OOXML 内容、类型与边界,以及行列选择、进度和 Worker 回退;生产 demo 还验证了工具栏压缩/未压缩下载与十万行 Worker 文件,并用 Python zipfile / openpyxl 独立读取。

构建与验证

pnpm install
pnpm typecheck
pnpm test
pnpm build
pnpm demo:build
pnpm pack:check

发布前先登录 npm 官方 registry,然后发布;prepublishOnly 会自动生成 dist/:

npm login --registry=https://registry.npmjs.org/
npm publish

依赖

运行时 peer:vue、@tanstack/vue-table、@tanstack/vue-virtual、reka-ui、@lucide/vue、@vueuse/core。clsx、tailwind-merge、class-variance-authority 是包内样式辅助依赖。XLSX 导出没有额外运行时依赖。

许可证

本包继承仓库现有实现的 GPL-2.0-or-later,详见 LICENSE。如果需要改用 MIT/Apache-2.0,请先取得版权所有者和贡献者的重新授权。