Skip to content

Table 表格

用于展示多条结构类似的数据,支持排序、筛选、选择、展开、树形层级、合计和自定义单元格等能力。

基础表格

el-table 传入 data 数组,在 el-table-column 中使用 prop 对应字段名,使用 label 定义列标题。

vue
<el-table :data="tableData" style="width: 100%">
  <el-table-column prop="date" label="日期" width="140" />
  <el-table-column prop="project" label="项目名称" width="220" />
  <el-table-column prop="owner" label="负责人" width="120" />
  <el-table-column prop="city" label="城市" />
</el-table>

斑马纹与边框

设置 stripe 可以使用斑马纹;设置 border 可以显示纵向边框。两者常用于数据密度较高的后台页面。

vue
<el-table :data="tableData" stripe border style="width: 100%">
  <el-table-column prop="date" label="日期" width="140" />
  <el-table-column prop="project" label="项目名称" width="220" />
  <el-table-column prop="owner" label="负责人" width="120" />
  <el-table-column prop="amount" label="报价金额" width="140" />
  <el-table-column prop="status" label="状态" />
</el-table>

状态行

通过 row-class-name 可以给特定行添加状态样式,用于提示风险、成功或异常数据。

vue
<template>
  <el-table :data="tableData" :row-class-name="tableRowClassName">
    <el-table-column prop="date" label="日期" />
    <el-table-column prop="project" label="项目名称" />
    <el-table-column prop="risk" label="风险等级" />
  </el-table>
</template>

<script setup lang="ts">
const tableRowClassName = ({ row }: { row: { risk: string } }) => {
  if (row.risk === '高') return 'warning-row'
  if (row.risk === '低') return 'success-row'
  return ''
}
</script>

溢出提示

当单元格内容较长时,设置 show-overflow-tooltip 可以让内容保持单行,并在 hover 时展示完整内容。

vue
<el-table :data="tableData" style="width: 100%">
  <el-table-column type="selection" width="48" />
  <el-table-column prop="project" label="项目名称" width="200" />
  <el-table-column prop="city" label="城市" width="120" />
  <el-table-column prop="address" label="项目地址" show-overflow-tooltip />
</el-table>

固定表头

当纵向内容较多时,设置 height 即可固定表头,让表格内容区域滚动。

vue
<el-table :data="tableLongData" height="240" style="width: 100%">
  <el-table-column prop="date" label="日期" width="140" />
  <el-table-column prop="project" label="项目名称" width="220" />
  <el-table-column prop="owner" label="负责人" width="120" />
  <el-table-column prop="city" label="城市" />
</el-table>

固定列

横向字段较多时,可以为列设置 fixedfixedtrue 时固定在左侧,也可以设置为 right 固定到右侧。

vue
<el-table :data="tableWideData" style="width: 100%">
  <el-table-column fixed prop="date" label="日期" width="140" />
  <el-table-column prop="project" label="项目名称" width="220" />
  <el-table-column prop="address" label="项目地址" width="360" />
  <el-table-column fixed="right" label="操作" width="150">
    <template #default>
      <el-button link type="primary" size="small">详情</el-button>
      <el-button link type="primary" size="small">编辑</el-button>
    </template>
  </el-table-column>
</el-table>

固定列和表头

heightfixed 可以同时使用,适合字段多、数据多的表格。

vue
<el-table :data="tableWideData" height="240" border style="width: 100%">
  <el-table-column fixed prop="date" label="日期" width="140" />
  <el-table-column prop="project" label="项目名称" width="220" />
  <el-table-column prop="address" label="项目地址" width="360" />
  <el-table-column fixed="right" prop="status" label="状态" width="120" />
</el-table>

流体高度

设置 max-height 后,表格会在内容超过最大高度时滚动;内容较少时会保持实际高度。

vue
<template>
  <el-table :data="tableData" max-height="240">
    <el-table-column fixed prop="date" label="日期" width="140" />
    <el-table-column prop="project" label="项目名称" width="220" />
    <el-table-column fixed="right" label="操作" width="100">
      <template #default="scope">
        <el-button link type="danger" size="small" @click.prevent="deleteRow(scope.$index)">
          移除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

多级表头

el-table-column 嵌套在另一个 el-table-column 内即可创建多级表头。

vue
<el-table :data="tableData" border>
  <el-table-column prop="date" label="日期" width="140" />
  <el-table-column label="项目信息">
    <el-table-column prop="project" label="项目名称" width="220" />
    <el-table-column prop="owner" label="负责人" width="120" />
  </el-table-column>
</el-table>

单选

设置 highlight-current-row 可以高亮当前行,并通过 current-change 获取当前选中行。

当前选中:未选择
vue
<template>
  <el-table :data="tableData" highlight-current-row @current-change="handleCurrentChange">
    <el-table-column prop="date" label="日期" />
    <el-table-column prop="project" label="项目名称" />
  </el-table>
</template>

<script setup lang="ts">
const handleCurrentChange = (row) => {
  console.log(row)
}
</script>

多选

添加 type="selection" 列即可启用多选。配合 row-keyreserve-selection 可以在数据刷新后保留选中项。

已选 0 项
vue
<template>
  <el-table ref="tableRef" :data="tableData" row-key="id" @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="48" :selectable="selectable" />
    <el-table-column prop="project" label="项目名称" />
  </el-table>
</template>

排序

在列上设置 sortable 即可排序。使用 default-sort 可以设置默认排序列和排序顺序;后端排序时将 sortable 设置为 custom 并监听 sort-change

vue
<el-table :data="tableData" :default-sort="{ prop: 'amountValue', order: 'descending' }">
  <el-table-column prop="date" label="日期" sortable />
  <el-table-column prop="project" label="项目名称" />
  <el-table-column prop="amountValue" label="报价金额" sortable :formatter="formatAmount" />
</el-table>

筛选

在列上设置 filtersfilter-method 即可开启筛选。需要重置筛选时,可以调用 Table 暴露的 clearFilter 方法。

vue
<template>
  <el-table ref="tableRef" row-key="id" :data="tableData">
    <el-table-column
      prop="status"
      label="状态"
      column-key="status"
      :filters="statusFilters"
      :filter-method="filterStatus"
    />
  </el-table>
</template>

自定义列模板

通过默认插槽可以自定义列内容。插槽提供 rowcolumn$index 和内部 store 等信息。

vue
<el-table :data="tableData">
  <el-table-column label="项目">
    <template #default="{ row }">
      <el-tag>{{ row.project }}</el-tag>
    </template>
  </el-table-column>
  <el-table-column label="操作">
    <template #default>
      <el-button link type="primary" size="small">详情</el-button>
    </template>
  </el-table-column>
</el-table>

自定义表头

通过 header 插槽可以自定义表头内容,常用于表头搜索、提示图标或复杂标题。

vue
<el-table :data="filterTableData">
  <el-table-column prop="project" label="项目名称" />
  <el-table-column>
    <template #header>
      <el-input v-model="search" size="small" placeholder="搜索城市" />
    </template>
    <template #default="{ row }">{{ row.city }}</template>
  </el-table-column>
</el-table>

展开行

当行内容较多且不希望横向滚动时,可以设置 type="expand" 的列,将额外信息放入展开区域。

vue
<el-table :data="tableData">
  <el-table-column type="expand">
    <template #default="{ row }">
      <div>项目地址:{{ row.address }}</div>
    </template>
  </el-table-column>
  <el-table-column prop="project" label="项目名称" />
</el-table>

树形数据

当行数据包含 children 字段时,Table 可以展示树形数据。树形表格需要设置 row-key

vue
<el-table :data="treeData" row-key="id" border default-expand-all>
  <el-table-column prop="name" label="模块" width="240" />
  <el-table-column prop="owner" label="负责人" width="120" />
  <el-table-column prop="status" label="状态" />
</el-table>

表尾合计行

设置 show-summary 可以显示合计行。通过 summary-method 可以自定义合计逻辑和展示内容。

vue
<el-table :data="tableData" border show-summary :summary-method="getSummaries">
  <el-table-column prop="name" label="费用项" />
  <el-table-column prop="amount" label="金额" />
  <el-table-column prop="count" label="数量" />
</el-table>

合并行或列

通过 span-method 可以合并行或列。方法返回 [rowspan, colspan],也可以返回 { rowspan, colspan }

vue
<el-table :data="tableData" :span-method="spanMethod" border>
  <el-table-column prop="category" label="类别" />
  <el-table-column prop="name" label="费用项" />
  <el-table-column prop="amount" label="金额" />
</el-table>

自定义索引

type="index" 会展示默认行号。通过 index 属性可以设置起始值或传入方法自定义行号。

vue
<el-table :data="tableData">
  <el-table-column type="index" :index="indexMethod" width="80" label="序号" />
  <el-table-column prop="project" label="项目名称" />
</el-table>

表格布局

通过 table-layout 可以控制表格布局方式,支持 fixedauto

vue
<el-radio-group v-model="tableLayout">
  <el-radio-button value="fixed">fixed</el-radio-button>
  <el-radio-button value="auto">auto</el-radio-button>
</el-radio-group>
<el-table :data="tableData" :table-layout="tableLayout">
  <el-table-column prop="date" label="日期" />
  <el-table-column prop="project" label="项目名称" />
</el-table>

Table API

Table 属性

属性名说明类型默认值
data表数据array[]
height表格高度,设置后可固定表头string | number-
max-height表格最大高度string | number-
stripe是否为斑马纹表格booleanfalse
border是否带有纵向边框booleanfalse
size表格尺寸large | default | small-
fit列宽是否自撑开booleantrue
show-header是否显示表头booleantrue
highlight-current-row是否高亮当前行booleanfalse
current-row-key当前行的 key,只写属性string | number-
row-class-name行的 className 回调或固定类名function | string-
row-style行的 style 回调或固定样式function | object-
cell-class-name单元格 className 回调或固定类名function | string-
cell-style单元格 style 回调或固定样式function | object-
header-cell-class-name表头单元格 className 回调或固定类名function | string-
header-cell-style表头单元格 style 回调或固定样式function | object-
row-key行数据的唯一 Key,保留选择和树形数据时必填function | string-
empty-text空数据时显示的文本,也可以通过 empty 插槽设置stringNo Data
default-expand-all是否默认展开所有行booleanfalse
expand-row-keys当前展开行的 key 数组,需要设置 row-keyarray-
default-sort默认排序列和排序顺序object-
tooltip-effect溢出 Tooltip 的主题dark | lightdark
tooltip-options溢出 Tooltip 的选项object-
show-summary是否显示合计行booleanfalse
sum-text合计行第一列文本stringSum
summary-method自定义合计方法function-
span-method合并行或列的计算方法function-
select-on-indeterminate表头半选状态下点击是否全选booleantrue
indent树形数据缩进距离number16
lazy是否懒加载子节点booleanfalse
load加载子节点数据的方法function-
tree-props树形数据配置object{ hasChildren: 'hasChildren', children: 'children' }
table-layout表格布局方式fixed | autofixed
scrollbar-always-on是否总是显示滚动条booleanfalse
flexible是否确保主轴最小尺寸booleanfalse
show-overflow-tooltip是否让所有列开启溢出 Tooltipboolean | objectfalse
tooltip-formatter自定义溢出 Tooltip 内容function-
preserve-expanded-content折叠后是否保留展开行内容 DOMbooleanfalse
native-scrollbar是否使用原生滚动条样式booleanfalse
row-expandable判断行是否允许展开function-

Table 事件

事件名说明类型
select用户手动勾选数据行时触发Function
select-all用户手动勾选全选时触发Function
selection-change选择项发生变化时触发Function
cell-click单元格被点击时触发Function
row-click行被点击时触发Function
row-dblclick行被双击时触发Function
header-click表头被点击时触发Function
sort-change排序条件变化时触发Function
filter-change筛选条件变化时触发Function
current-change当前行变化时触发Function
header-dragend拖动表头改变列宽时触发Function
expand-change展开行或树形节点展开状态变化时触发Function
scroll表格滚动后触发Function

Table 插槽

插槽名说明子标签
default自定义默认内容Table-column
append插入到表格最后一行之后的内容-
empty空数据时的自定义内容-

Table 暴露

名称说明类型
clearSelection清空用户选择Function
getSelectionRows返回当前选中的行Function
toggleRowSelection切换某一行的选中状态Function
toggleAllSelection切换全选和全不选Function
toggleRowExpansion切换展开行或树形行的展开状态Function
setCurrentRow设置当前选中行Function
clearSort清空排序条件Function
clearFilter清除指定列或全部筛选条件Function
doLayout对 Table 进行重新布局Function
sort手动排序表格Function
scrollTo滚动到指定坐标Function
setScrollTop设置垂直滚动位置Function
setScrollLeft设置水平滚动位置Function

Table-column API

Table-column 属性

属性名说明类型默认值
type列类型,可选 selectionindexexpanddefault | selection | index | expanddefault
index自定义索引,仅对 type="index" 有效number | function-
label列标题string-
column-key列 key,使用 filter-change 时用于标识列string-
prop / property字段名称string-
width列宽string | number''
min-width最小列宽,剩余宽度会按比例分配string | number''
fixed是否固定列,true 表示固定在左侧left | right | booleanfalse
render-header自定义表头渲染方法function-
sortable是否可排序,custom 表示远程排序boolean | stringfalse
sort-method自定义排序方法function-
sort-by指定排序字段或排序方法function | string | array-
sort-orders排序策略轮转顺序array['ascending', 'descending', null]
resizable是否可拖动改变列宽,需要 Table 设置 borderbooleantrue
formatter格式化单元格内容function-
show-overflow-tooltip内容过长时是否显示 Tooltipboolean | objectundefined
align内容对齐方式left | center | rightleft
header-align表头对齐方式,未设置时跟随 alignleft | center | rightleft
class-name列内容自定义类名string-
label-class-name当前列标题自定义类名string-
selectable决定行是否可勾选,仅对 selection 列有效function-
reserve-selection数据刷新后是否保留选择,需要设置 row-keybooleanfalse
filters过滤选项数组array-
filter-placement过滤弹出框位置enum-
filter-method数据过滤方法function-
filter-multiple是否允许多选过滤booleantrue

Table-column 插槽

插槽名说明
default自定义列内容,作用域包含 rowcolumn$indexstore
header自定义表头内容,作用域包含 column$index
filter-icon自定义过滤图标

HOMEVISTA 设计规范