Skip to content

vtRowHeightConfig 行高 ​

低 / 中 / 高三档行高。一档换两件事 —— 密度类(单元格内边距与字号)与虚拟化行高:

档密度类行高
低 compact.vt-root--compact30px(= 19 行高 + 5×2 内边距 + 1 边框)
中 default无40px(= 21 + 9×2 + 1)
高 loose.vt-root--loose50px(= 21 + 14×2 + 1)

三个数是各档行高等式的解(由 css-contract.test.ts 钉住),不是魔数。只做一半的后果很具体: 只加类 → 行内容变矮但每行仍占旧高度,行之间露白;只改行高 → 内容撑不开或被裁。 这个插件的全部价值就是把两件事绑在一起。

只在固定行高下开放 ​

fixedSize: true 时行高是你说了算的(核心按 estimatedSize 摆位,不测量)。不定高模式下 每行多高由内容决定,estimatedSize 只是未测量行的估算值 —— 那时给三档是句空话,所以面板会 直说原因,setRowHeightLevel() 也不生效。

示例 ​

微应用尚未挂载。

源码 ​

点击查看源码
ts
import { faker } from '@faker-js/faker';
import { VirtTable, type VirtTableColumn } from '@virt-table/vanilla';
import { vtRowHeightConfig, type RowHeightLevel } from '@virt-table/vanilla/plugins';
import '@virt-table/vanilla/plugins/row-height-config.css';

interface Row {
  id: number;
  name: string;
  dept: string;
  city: string;
  email: string;
}

const DEPTS = ['研发', '设计', '销售', '客服'];

export function bootstrapTableRowHeight(root: HTMLElement): () => void {
  const columns: VirtTableColumn<Row>[] = [
    { key: 'id', title: 'ID', width: 70 },
    { key: 'name', title: '姓名', width: 150 },
    { key: 'dept', title: '部门', width: 110 },
    { key: 'city', title: '城市', width: 130 },
    { key: 'email', title: '邮箱', width: 220 },
  ];

  const list: Row[] = Array.from({ length: 1000 }, (_, i) => ({
    id: i + 1,
    name: faker.person.fullName(),
    dept: DEPTS[i % DEPTS.length]!,
    city: faker.location.city(),
    email: faker.internet.email(),
  }));

  root.innerHTML = `
    <div class="virt-table-controls">
      <button class="virt-table-btn virt-table-btn-primary" id="rh">行高</button>
      <span class="demo-note" id="hint"></span>
    </div>
    <div style="width:820px;height:420px;" class="demo-container" id="c"></div>`;
  const container = root.querySelector('#c') as HTMLElement;
  const hint = root.querySelector('#hint') as HTMLElement;

  const table = new VirtTable<Row>(container, {
    list,
    columns,
    itemKey: 'id',
    estimatedSize: 40,
    buffer: 6,
    border: true,
    /*
     * **必须固定行高**:三档改的是「每行多高」,而不定高模式下那件事由内容决定,
     * `estimatedSize` 只是未测量行的估算值 —— 那时插件会直说原因,不给三档。
     */
    fixedSize: true,
    plugins: [
      vtRowHeightConfig({
        // 存下来就能下次开表时当 `value` 传回来
        onChange: (level: RowHeightLevel, px: number) => {
          hint.textContent = `当前:${level}(每行 ${px}px),密度类与行高一起换`;
        },
      }),
    ],
  });

  hint.textContent = `当前:default(每行 ${table.getRowHeight()}px)`;
  (root.querySelector('#rh') as HTMLButtonElement).onclick = (e) =>
    table.toggleRowHeightConfig(e.currentTarget as HTMLElement);

  return () => {
    table.destroy();
    root.innerHTML = '';
  };
}

说明 ​

ts
new VirtTable(el, {
  list, columns, itemKey: 'id',
  estimatedSize: 40,
  fixedSize: true,          // 必须
  plugins: [
    vtRowHeightConfig({
      value: 'compact',                       // 初始档(给了就立刻应用)
      onChange: (level, px) => save(level),   // 存下来,下次当 value 传回来
    }),
  ],
});

table.toggleRowHeightConfig(btnEl);
table.setRowHeightLevel('loose');
选项作用
value初始档。给了就立刻应用(覆盖你写的 estimatedSize);不给则从当前 estimatedSize 反推一档回显,什么都不改
sizes每档的行高,默认 { compact: 30, default: 40, loose: 50 }。自定义过 --vt-density-* 才需要动它
onChange切档后触发,持久化用
toolbar装了 vtToolbar 时要不要占那条按钮条的位置,默认 true
texts文案覆盖,不给则跟随 setLocale()

与工具栏的关系

vtToolbar() 一颗按钮都不自带 —— 装了本插件才多出那颗「行高」按钮,按排序位排在四个面板之后。 vtToolbarPanels() 那个预设里不含它(预设只是那四个面板的一行写法)。

相关 API ​

字段 / 方法作用详见
setRowHeightLevel(level) / getRowHeightLevel()读写档位插件机制
toggleRowHeightConfig(anchor?)开合面板插件机制
setRowHeight(px) / getRowHeight()底层入口:直接改行高(同时更新账本并重算总高)实例方法
fixedSize / estimatedSize固定行高与行高值表格配置
.vt-compact / .vt-loose三档密度类(也能自己加在容器上)插件机制 · 行高与密度

全量字段见 表格配置 / 列配置 / 实例方法。