Skip to content

VtTextarea 多行文本 ​

多行文本列:常态一行截断(配 textOverflow: 'tooltip' 可悬停看全文),编辑态盖住单元格并随内容长高,到 maxRows 或容器下缘封顶后内部滚动。

示例 ​

微应用尚未挂载。

源码 ​

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

export function bootstrapTableVtTextarea(root: HTMLElement): () => void {
  const rowCount = 5000;

  const columns: VirtTableColumn[] = [
    { key: 'id', title: 'ID', width: 80, align: 'center' },
    { key: 'name', title: '名称', width: 160 },
    VtTextarea({ key: 'remark', title: '备注', width: 260, placeholder: '点一下写点什么…' }),
    VtTextarea({ key: 'desc', title: '描述(最多长到 12 行 / 限 200 字)', width: 300, maxRows: 12, maxLength: 200 }),
    VtTextarea({ key: 'fixed', title: '只读', width: 200, disabled: true }),
  ];

  const list = Array.from({ length: rowCount }, (_, i) => ({
    id: i,
    name: `条目 ${i}`,
    remark: i % 3 === 0 ? `这是一段比较长的备注文本,用来演示单元格里的截断显示,行号 ${i}` : '',
    desc: `多行描述第一行 ${i}\n第二行\n第三行`,
    fixed: `只读内容 ${i}`,
  }));

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

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

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

  statusEl.textContent =
    'VtTextarea:点单元格进入编辑,编辑框直接盖住单元格 —— 宽度与单元格一致、初始等高,随输入一行行长高(到 maxRows 或容器下缘封顶后内部滚动)。滚到底部几行试试,长高时会自动上移,不会被容器裁掉';

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

说明 ​

编辑态走 renderEditor,必须装 vtCellEditor 插件才会被触发。Enter 在这里是换行、不提交;结束编辑按 Esc 或点别处。

编辑框不放在 .vt-cell-cover 里(那层高度恒等于单元格高度),而是与 cover 同挂 .vt-client、按「左上角 + 宽度」对齐单元格,高度自己长 —— 视觉上就是单元格自己长高了。宽度恒等于单元格宽度,横向不溢出,不会盖住相邻列。

相关 API ​

字段作用
maxRows最多长到几行,超过内部滚动
maxLength最大输入长度
col.textOverflow常态一行截断,配 tooltip 可悬停看全文

全量选项表见 单元格组件;公共选项 (placeholder / disabled / clearable / onChange)、asCell()、三类交互模型见 同页上方;列的通用字段见 列配置。

编辑态需装 vtCellEditor,见 插件机制。