Skip to content

表格自动行合并

针对Table表格行合并封装,支持缓存减少大量的重复计算

根据一个字段单列合并

ID
Name
Amount 1
Amount 2
Amount 3
1000
Tom
234
3.2
10
Tom
234
3.2
10
2000
Tom
324
1.9
9
Tom
324
1.9
9
3000
Tom
539
4.1
15
<template>
  <div>
    <el-table
      :border="true"
      :data="list"
      :span-method="
        ({ rowIndex, columnIndex }: any) =>
          useMergeTableRows({
            rowIndex,
            columnIndex,
            list: list,
            rules: {
              id: 0,
            },
          })
      "
    >
      <el-table-column prop="id" label="ID" width="180" />
      <el-table-column prop="name" label="Name" />
      <el-table-column prop="amount1" label="Amount 1" />
      <el-table-column prop="amount2" label="Amount 2" />
      <el-table-column prop="amount3" label="Amount 3" />
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import { useMergeTableRows } from '@fe-hl/admin-design-vue';

const list = [
  {
    id: '1000',
    name: 'Tom',
    amount1: '234',
    amount2: '3.2',
    amount3: 10,
  },
  {
    id: '1000',
    name: 'Tom',
    amount1: '234',
    amount2: '3.2',
    amount3: 10,
  },
  {
    id: '2000',
    name: 'Tom',
    amount1: '324',
    amount2: '1.9',
    amount3: 9,
  },
  {
    id: '2000',
    name: 'Tom',
    amount1: '324',
    amount2: '1.9',
    amount3: 9,
  },
  {
    id: '3000',
    name: 'Tom',
    amount1: '539',
    amount2: '4.1',
    amount3: 15,
  },
];
</script>
隐藏源代码

根据多个字段多列合并

ID
Name
Amount 1
Amount 2
Amount 3
1000
Tom
100
3.2
10
Tom
3.2
10
2000
Tom
1.9
9
Tom
1.9
9
3000
Tom
539
4.1
15
<template>
  <div>
    <el-table
      :border="true"
      :data="list"
      :span-method="
        ({ rowIndex, columnIndex }: any) =>
          useMergeTableRows({
            rowIndex,
            columnIndex,
            list: list,
            rules: {
              id: 0,
              amount1: 2,
            },
          })
      "
    >
      <el-table-column prop="id" label="ID" width="180" />
      <el-table-column prop="name" label="Name" />
      <el-table-column prop="amount1" label="Amount 1" />
      <el-table-column prop="amount2" label="Amount 2" />
      <el-table-column prop="amount3" label="Amount 3" />
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import { useMergeTableRows } from '@fe-hl/admin-design-vue';

const list = [
  {
    id: '1000',
    name: 'Tom',
    amount1: '100',
    amount2: '3.2',
    amount3: 10,
  },
  {
    id: '1000',
    name: 'Tom',
    amount1: '100',
    amount2: '3.2',
    amount3: 10,
  },
  {
    id: '2000',
    name: 'Tom',
    amount1: '100',
    amount2: '1.9',
    amount3: 9,
  },
  {
    id: '2000',
    name: 'Tom',
    amount1: '100',
    amount2: '1.9',
    amount3: 9,
  },
  {
    id: '3000',
    name: 'Tom',
    amount1: '539',
    amount2: '4.1',
    amount3: 15,
  },
];
</script>
隐藏源代码

根据一个字段多列合并

ID
Name
Amount 1
Amount 2
Amount 3
1000
Tom
234
3.2
10
Tom
3.2
10
2000
Tom
324
1.9
9
Tom
1.9
9
3000
Tom
539
4.1
15
<template>
  <div>
    <el-table
      :border="true"
      :data="list"
      :span-method="
        ({ rowIndex, columnIndex }: any) =>
          useMergeTableRows({
            rowIndex,
            columnIndex,
            list: list,
            rules: {
              id: [0, 2],
            },
          })
      "
    >
      <el-table-column prop="id" label="ID" width="180" />
      <el-table-column prop="name" label="Name" />
      <el-table-column prop="amount1" label="Amount 1" />
      <el-table-column prop="amount2" label="Amount 2" />
      <el-table-column prop="amount3" label="Amount 3" />
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import { useMergeTableRows } from '@fe-hl/admin-design-vue';

const list = [
  {
    id: '1000',
    name: 'Tom',
    amount1: '234',
    amount2: '3.2',
    amount3: 10,
  },
  {
    id: '1000',
    name: 'Tom',
    amount1: '234',
    amount2: '3.2',
    amount3: 10,
  },
  {
    id: '2000',
    name: 'Tom',
    amount1: '324',
    amount2: '1.9',
    amount3: 9,
  },
  {
    id: '2000',
    name: 'Tom',
    amount1: '324',
    amount2: '1.9',
    amount3: 9,
  },
  {
    id: '3000',
    name: 'Tom',
    amount1: '539',
    amount2: '4.1',
    amount3: 15,
  },
];
</script>
隐藏源代码

根据多个字段组合合并

id
name
amount1
amount2
amount3
1000
Tom
234
3.2
10
Tom
3.2
10
2000
Tom
324
1.9
9
Tom
1.9
9
3000
Tom
539
4.1
15
Total 5
10/page
  • 1
Go to
<template>
  <AdTable v-bind="tableConfig" ref="tableRef"></AdTable>
</template>
<script lang="ts" setup>
import { useMergeTableRows } from '@fe-hl/admin-design-vue';
import type { AdTablePorps, AdTableInstance } from '@fe-hl/admin-design-vue';
import { ref } from 'vue';
import data from './data';

const tableRef = ref<AdTableInstance>();
const tableConfig = ref<AdTablePorps>({
  api: () => Promise.resolve(data), // 模拟ajax请求
  table: {
    spanMethod: ({ rowIndex, columnIndex }: any) =>
      useMergeTableRows({
        rowIndex,
        columnIndex,
        list: tableRef.value?.list || [],
        rules: {
          'id~name': [0, 2],
        },
      }),
  },
  columns: [
    { label: 'id', prop: 'id' },
    { label: 'name', prop: 'name' },
    { label: 'amount1', prop: 'amount1' },
    { label: 'amount2', prop: 'amount2' },
    { label: 'amount3', prop: 'amount3' },
  ],
});
</script>
隐藏源代码

结合AdTable组件合并

id
name
amount1
amount2
amount3
1000
Tom
234
3.2
10
Tom
234
3.2
10
2000
Tom
324
1.9
9
Tom
324
1.9
9
3000
Tom
539
4.1
15
Total 5
10/page
  • 1
Go to
<template>
  <AdTable v-bind="tableConfig" ref="tableRef"></AdTable>
</template>
<script lang="ts" setup>
import { useMergeTableRows } from '@fe-hl/admin-design-vue';
import type { AdTablePorps, AdTableInstance } from '@fe-hl/admin-design-vue';
import { ref } from 'vue';
import data from './data';

const tableRef = ref<AdTableInstance>();
const tableConfig = ref<AdTablePorps>({
  api: () => Promise.resolve(data), // 模拟ajax请求
  table: {
    spanMethod: ({ rowIndex, columnIndex }: any) =>
      useMergeTableRows({
        rowIndex,
        columnIndex,
        list: tableRef.value?.list || [],
        rules: {
          id: 0,
        },
      }),
  },
  columns: [
    { label: 'id', prop: 'id' },
    { label: 'name', prop: 'name' },
    { label: 'amount1', prop: 'amount1' },
    { label: 'amount2', prop: 'amount2' },
    { label: 'amount3', prop: 'amount3' },
  ],
});
</script>
隐藏源代码

Func参数

名称描述类型
rowIndexnumber
columnIndexnumber
list数据源T[]
rules合并规则

类型声明

显示类型声明
ts
interface IMergeTableRows<T = any> {
  rowIndex: number; // 行
  columnIndex: number; // 列
  list: T[]; // 列表数据
  rules: Record<string, number | Array<number>>; // 合并规则 {id:0} OR {id:[2,4]} OR {'id~name': [0, 1]}
}