Skip to content

单元格编辑

通过 renderEditorrender 为不同列定制查看态与编辑态渲染。

使用的 API

ts
type Column = {
  key: string;  // 列标识(必填)
  title: string;  // 列标题(必填)
  width: number;  // 列宽(必填)
  render: (ctx) => string | HTMLElement;  // 自定义渲染
  renderEditor: (ctx) => HTMLElement | null | void;  // 单元格编辑渲染
}

type Options = {
  list: T[];  // 数据列表
  itemKey: string;  // 行唯一标识字段名
  estimatedSize: number;  // 行预估高度(px)
  buffer: number;  // 缓冲区行数
  border: boolean;  // 是否显示边框
  textOverflow: 'ellipsis' | 'tooltip';  // 全局文本溢出处理
}

示例

微应用尚未挂载。

源码

点击查看源码
ts
import {
  VirtTable,
} from '@virt-table/vanilla';
import {
  vtCellEditor,
} from '@virt-table/vanilla/plugins';

export function bootstrapTableCellsRender(root: HTMLElement): () => void {
  const colCount = 6;

  const selectOptions = [
    { value: 'opt1', label: '选项 1' },
    { value: 'opt2', label: '选项 2' },
    { value: 'opt3', label: '选项 3' },
    { value: 'opt4', label: '选项 4' },
  ];

  const columns = [
    {
      key: 'input',
      title: 'Input',
      width: 200,
      renderEditor: ({ value, row, column: col }) => {
        const input = document.createElement('input');
        input.type = 'text';
        input.value = value ?? '';
        input.style.cssText = 'width:100%;height:100%;border:none;outline:none;padding:0 12px;font-size:14px;box-sizing:border-box;';
        input.addEventListener('input', () => { row[col.key] = input.value; });
        requestAnimationFrame(() => input.focus());
        return input;
      },
    },
    {
      key: 'number',
      title: 'Number',
      width: 160,
      renderEditor: ({ value, row, column: col }) => {
        const input = document.createElement('input');
        input.type = 'number';
        input.value = value ?? '';
        input.style.cssText = 'width:100%;height:100%;border:none;outline:none;padding:0 12px;font-size:14px;box-sizing:border-box;';
        input.addEventListener('input', () => { row[col.key] = input.value; });
        requestAnimationFrame(() => input.focus());
        return input;
      },
    },
    {
      key: 'select',
      title: 'Select',
      width: 180,
      render: ({ value }) => {
        const opt = selectOptions.find((o) => o.value === value);
        return opt ? opt.label : '请选择';
      },
      renderEditor: ({ value, row, column: col }) => {
        const select = document.createElement('select');
        select.style.cssText = 'width:100%;height:100%;border:none;outline:none;padding:0 8px;font-size:14px;box-sizing:border-box;background:#fff;';
        const placeholder = document.createElement('option');
        placeholder.value = '';
        placeholder.textContent = '请选择';
        placeholder.disabled = true;
        select.appendChild(placeholder);
        for (const opt of selectOptions) {
          const o = document.createElement('option');
          o.value = opt.value;
          o.textContent = opt.label;
          if (opt.value === value) o.selected = true;
          select.appendChild(o);
        }
        select.addEventListener('change', () => { row[col.key] = select.value; });
        requestAnimationFrame(() => select.focus());
        return select;
      },
    },
    {
      key: 'date',
      title: 'Date',
      width: 180,
      renderEditor: ({ value, row, column: col }) => {
        const input = document.createElement('input');
        input.type = 'date';
        input.value = value ?? '';
        input.style.cssText = 'width:100%;height:100%;border:none;outline:none;padding:0 12px;font-size:14px;box-sizing:border-box;';
        input.addEventListener('change', () => { row[col.key] = input.value; });
        requestAnimationFrame(() => input.focus());
        return input;
      },
    },
    {
      key: 'checkbox',
      title: 'Checkbox',
      width: 120,
      render: ({ value }) => value ? '✅ Yes' : '❌ No',
      renderEditor: ({ value, row, column: col }) => {
        const wrap = document.createElement('label');
        wrap.style.cssText = 'display:flex;align-items:center;gap:8px;padding:0 12px;height:100%;cursor:pointer;font-size:14px;';
        const cb = document.createElement('input');
        cb.type = 'checkbox';
        cb.checked = !!value;
        cb.style.cssText = 'width:16px;height:16px;cursor:pointer;';
        const span = document.createElement('span');
        span.textContent = value ? 'Yes' : 'No';
        cb.addEventListener('change', () => {
          row[col.key] = cb.checked;
          span.textContent = cb.checked ? 'Yes' : 'No';
        });
        wrap.appendChild(cb);
        wrap.appendChild(span);
        return wrap;
      },
    },
    {
      key: 'readonly',
      title: 'Readonly',
      width: 200,
    },
  ];

  const rowCount = 10000;
  const list = Array.from({ length: rowCount }, (_, i) => ({
    id: i,
    input: `Row ${i}`,
    number: String(Math.floor(Math.random() * 1000)),
    select: selectOptions[i % selectOptions.length].value,
    date: '2025-01-01',
    checkbox: i % 3 === 0,
    readonly: `只读 ${i}`,
  }));

  root.innerHTML = `
  <div class="virt-table-controls">
    <button class="virt-table-btn virt-table-btn-primary" id="btnTop">滚动到顶部</button>
    <button class="virt-table-btn virt-table-btn-primary" id="btnBottom">滚动到底部</button>
    <button class="virt-table-btn virt-table-btn-warning" id="btnRandom">随机滚动</button>
  </div>
  <div id="status" class="status-text"></div>
  <div style="width:800px;height:600px;" class="demo-container" id="virtTableContainer"></div>
`;

  const container = root.querySelector('#virtTableContainer') as HTMLElement;
  const status = root.querySelector('#status') as HTMLElement;
  const listeners: (() => void)[] = [];

  const table = new VirtTable(container, {
    plugins: [vtCellEditor()],
    list,
    columns,
    itemKey: 'id',
    estimatedSize: 40,
    buffer: 4,
    border: true,
    textOverflow: 'ellipsis',
  });

  status.textContent = `单元格编辑:${rowCount} 行 × ${colCount} 列(点击单元格进入编辑模式)`;

  const on = (id: string, handler: () => void) => {
    const el = root.querySelector(`#${id}`);
    if (!el) return;
    el.addEventListener('click', handler);
    listeners.push(() => el.removeEventListener('click', handler));
  };

  on('btnTop', () => table.scrollToTop());
  on('btnBottom', () => table.scrollToBottom());
  on('btnRandom', () => {
    const idx = Math.floor(Math.random() * rowCount);
    table.scrollToIndex(idx);
  });

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