Skip to content

Vue VtTextarea 多行文本 ​

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

示例 ​

微应用尚未挂载。

源码 ​

点击查看源码
vue
<template>
  <div class="demo-wrapper">
    <h3 class="demo-title">Vue VtTextarea 多行文本</h3>
    <div class="status-text">{{ status }}</div>
    <div style="width: 900px; height: 600px" class="demo-container">
      <VirtTableVue :columns="columns" :options="options" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { VirtTableVue, type VueTableColumn, vtCellEditor } from '@virt-table/vue';
import { VtTextarea } from '@virt-table/vanilla/components';

const ROW_COUNT = 5000;

const columns: VueTableColumn[] = [
  { 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: ROW_COUNT }, (_, i) => ({
  id: i,
  name: `条目 ${i}`,
  remark:
    i % 3 === 0 ? `这是一段比较长的备注文本,用来演示单元格里的截断显示,行号 ${i}` : '',
  desc: `多行描述第一行 ${i}\n第二行\n第三行`,
  fixed: `只读内容 ${i}`,
}));

const options = {
  list,
  itemKey: 'id',
  estimatedSize: 40,
  buffer: 4,
  border: true,
  textOverflow: 'tooltip' as const,
  plugins: [vtCellEditor()],
};

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

说明 ​

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

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

相关 API ​

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

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

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

上面的字段在 Vue 端原样透传(实例方法通过组件 ref 调用)。封装层特有的部分 —— 组件 Props、渲染函数返回 VNode、ref 转发了哪些方法 —— 见 Vue API。