Vue 组件渲染
列的 render / renderEditor / renderHeader / renderExpandRow 可以直接返回 VNode (h() 或 JSX),挂载与回收由适配层负责 —— 行被虚拟滚动回收时对应的组件实例一起卸载, 不会泄漏。
示例
微应用尚未挂载。
源码
点击查看源码
vue
<template>
<div style="padding: 16px">
<h3 style="margin: 0 0 12px; color: #1d2129">Vue 组件渲染单元格</h3>
<div style="color: #86909c; font-size: 13px; margin-bottom: 8px">
render 返回 VNode(JSX),框架自动挂载/卸载,支持响应式
</div>
<div style="width: 800px; height: 500px; border: 1px solid #e5e6eb">
<VirtTableVue :columns="columns" :options="options" />
</div>
</div>
</template>
<script setup lang="tsx">
import { ref } from 'vue';
import { VirtTableVue, type VueTableColumn } from '@virt-table/vue';
import { faker } from '@faker-js/faker';
interface Row {
id: number;
name: string;
age: number;
city: string;
score: number;
[k: string]: any;
}
const cities = ['北京', '上海', '杭州', '深圳', '广州'] as const;
const list = ref<Row[]>(
Array.from({ length: 500 }, (_, i) => ({
id: i,
name: faker.person.fullName(),
age: 20 + Math.floor(Math.random() * 40),
city: cities[i % 5]!,
score: Math.floor(Math.random() * 100),
})),
);
const RatingBar = (props: { value: number }) => {
const pct = props.value;
const color = pct >= 80 ? '#00b42a' : pct >= 60 ? '#ff7d00' : '#f53f3f';
return (
<div style="display:flex;align-items:center;gap:6px;">
<div style="width:60px;height:6px;background:#f2f3f5;border-radius:3px;overflow:hidden;">
<div style={`width:${pct}%;height:100%;background:${color};border-radius:3px;`} />
</div>
<span style={`font-size:12px;color:${color};min-width:28px;`}>{pct}</span>
</div>
);
};
const TagCell = (props: { text: string }) => {
const colors: Record<string, string> = {
'北京': '#165dff', '上海': '#0fc6c2', '杭州': '#7816ff',
'深圳': '#ff7d00', '广州': '#00b42a',
};
const bg = colors[props.text] || '#86909c';
return (
<span style={`display:inline-block;padding:2px 8px;border-radius:10px;font-size:12px;color:#fff;background:${bg};`}>
{props.text}
</span>
);
};
const columns: VueTableColumn<Row>[] = [
{ key: 'id', title: 'ID', width: 80 },
{
key: 'name',
title: '姓名',
width: 180,
render: ({ value }) => <span style="font-weight:500;color:#1d2129;">{value}</span>,
},
{ key: 'age', title: '年龄', width: 80 },
{
key: 'city',
title: '城市',
width: 120,
render: ({ value }) => <TagCell text={value} />,
},
{
key: 'score',
title: '分数',
width: 160,
render: ({ value }) => <RatingBar value={value} />,
},
];
const options = {
list: list.value,
itemKey: 'id',
estimatedSize: 40,
buffer: 4,
border: true,
};
</script>说明
vanilla 没有这一层(render 返回的就是 DOM 字符串或元素),所以这一页只在框架端有。
ts
import { h } from 'vue';
import { VirtTableVue, type VueTableColumn } from '@virt-table/vue';
import MyTag from './MyTag.vue';
const columns: VueTableColumn[] = [
// 返回 VNode:适配层负责 mount / unmount
{ key: 'status', title: '状态', width: 120, render: ({ value }) => h(MyTag, { value }) },
// 也可以返回字符串 / DOM 元素,与 vanilla 同义
{ key: 'name', title: '姓名', width: 160, render: ({ value }) => `<b>${value}</b>` },
];两条需要留意:
- 查看态别挂重组件。 滚动时行 DOM 被反复复用,持续挂载 / 销毁组件会拖帧。轻量内容用 字符串或
h('span', …),真组件留给编辑态(只有当前激活的那一格才会构造),理由与 第三方组件库集成那页一样。 - 编辑态里的 VNode 要刷新时调
forceUpdate(),见 Vue API · 编辑态刷新。
相关 API
| 字段 / 方法 | 作用 | 详见 |
|---|---|---|
col.render | 返回 VNode / 字符串 / DOM 元素 | Vue API · 渲染函数返回 VNode |
col.renderEditor / renderHeader / renderExpandRow | 同样支持 VNode | Vue API · 列级 |
setCellRender(rowKey, colKey, cfg) | 单元格级也能返回 VNode(走 ref) | Vue API · 单元格级渲染 |
forceUpdate() | 刷新编辑态里的 VNode | 实例方法 |
内容层的优先级(表级 / 列级 / 单元格级)与四层模型见 渲染粒度; 字段全量表在 Vanilla API · 单元格渲染。