React 打印
由
vtExport插件提供:plugins: [vtExport()]。
调用 print({ title }) 在新窗口输出全表(内联样式),复用导出的 HTML 构造,适合快速打印或另存。
示例
微应用尚未挂载。
源码
点击查看源码
tsx
import React from 'react';
import {
VirtTableReact,
type ReactTableColumn,
type VirtTableRef,
vtExport,
} from '@virt-table/react';
import { faker } from '@faker-js/faker';
interface Row extends Record<string, unknown> {
id: number;
name: string;
city: string;
score: number;
}
const columns: ReactTableColumn<Row>[] = [
{ key: 'id', title: 'ID', width: 80 },
{ key: 'name', title: '姓名', width: 220 },
{ key: 'city', title: '城市', width: 220 },
{ key: 'score', title: '分数', width: 120 },
];
const list: Row[] = Array.from({ length: 200 }, (_, i) => ({
id: i + 1,
name: faker.person.fullName(),
city: faker.location.city(),
score: faker.number.int({ min: 0, max: 100 }),
}));
export default function PrintTable() {
const tableRef = React.useRef<VirtTableRef>(null);
// 打印与导出同属 vtExport
const plugins = React.useMemo(() => [vtExport()], []);
return (
<div className="demo-wrapper">
<h3 className="demo-title">React 打印</h3>
<div className="virt-table-controls">
<button
type="button"
className="virt-table-btn virt-table-btn-primary"
onClick={() => tableRef.current?.print({ title: '人员清单' })}
>
打印表格
</button>
<span className="demo-note">会打开新窗口输出全表(内联样式)</span>
</div>
<div style={{ width: 660, height: 460 }} className="demo-container">
<VirtTableReact
ref={tableRef}
columns={columns}
options={{ list, itemKey: 'id', estimatedSize: 40, buffer: 6, border: true, plugins }}
/>
</div>
</div>
);
}相关 API
| 字段 / 方法 | 作用 | 详见 |
|---|---|---|
vtExport() | 打印由导出插件提供 | 插件机制 · 导出 |
print(opts?: { title? }) | 新窗口输出全表(内联样式) | 插件机制 · 导出 |
上面的字段在 React 端原样透传(实例方法通过组件
ref调用)。封装层特有的部分 —— 组件 Props、渲染函数返回 JSX、ref转发了哪些方法 —— 见 React API。