Skip to content

单元格选区 ​

启用单元格选区后,可拖动鼠标选择矩形区域,并通过回调获取选区范围。

示例 ​

微应用尚未挂载。

源码 ​

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

export function bootstrapTableCellSelection(root: HTMLElement): () => void {
  const colCount = 10;
  const rowCount = 1000;

  const columns = Array.from({ length: colCount }, (_, i) => ({
    key: `extra_${i}`,
    title: `列 ${i}`,
    width: 200,
  }));

  const list = Array.from({ length: rowCount }, (_, i) => {
    const row: Record<string, any> = { id: i };
    for (let c = 0; c < colCount; c++) {
      row[`extra_${c}`] = `${i}-${c}-${faker.lorem.words(6)}`;
    }
    return row;
  });

  root.innerHTML = `
    <div id="status" class="status-text"></div>
    <div style="width:800px;height:600px;" class="demo-container" id="tableContainer"></div>
  `;

  const container = root.querySelector('#tableContainer') as HTMLElement;
  const status = root.querySelector('#status') as HTMLElement;

  const table = new VirtTable(container, {
    plugins: [vtCellSelection()],
    list,
    columns,
    itemKey: 'id',
    estimatedSize: 40,
    buffer: 4,
    border: true,
    onCellSelectionChange: (range) => {
      if (range) {
        status.textContent = `选区: 行 ${range.startRow}-${range.endRow}, 列 ${range.startCol}-${range.endCol}`;
      } else {
        status.textContent = '选区已清除';
      }
    },
  });

  status.textContent = '单元格选区(按住左键拖动选择单元格区域)';

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

相关 API ​

字段 / 方法作用详见
vtCellSelection()框选插件插件机制
onCellSelectionChange选区变化回调 { startRow, startCol, endRow, endCol } | null表格配置
getCellSelection / clearCellSelection读写选区实例方法

选区会按合并块扩张成完整矩形,见按值自动合并。

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