Ben Lin
2024-09-12 a4ee6ba0ca28833cbbb8cf0e675561b10fa4c1af
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<template>
  <Table
    v-if="!!props.summaryFunc || props.summaryData"
    :showHeader="false"
    :bordered="false"
    :pagination="false"
    :dataSource="getDataSource"
    :rowKey="props.rowKey"
    :columns="getColumns"
    tableLayout="fixed"
    :scroll="props.scroll"
  />
</template>
<script lang="ts" setup>
  import { unref, computed, toRaw } from 'vue';
  import { Table } from 'ant-design-vue';
  import { cloneDeep } from 'lodash-es';
  import { isFunction } from '@/utils/is';
  import type { BasicColumn, BasicTableProps } from '../types/table';
  import { INDEX_COLUMN_FLAG } from '../const';
  import { useTableContext } from '../hooks/useTableContext';
  import { ColumnType } from 'ant-design-vue/es/table/interface';
  import { parseRowKey } from '../helper';
 
  defineOptions({ name: 'BasicTableFooter' });
 
  const props = withDefaults(
    defineProps<{
      summaryFunc?: Fn | null;
      summaryData?: Recordable[] | null;
      scroll?: BasicTableProps['scroll'];
      rowKey?: BasicTableProps['rowKey'];
    }>(),
    {
      summaryFunc: null,
      summaryData: null,
      rowKey: '',
    },
  );
 
  const SUMMARY_ROW_KEY = '_row';
  const SUMMARY_INDEX_KEY = '_index';
  const table = useTableContext();
 
  const getDataSource = computed((): Recordable[] => {
    if (props.summaryData?.length) {
      props.summaryData.forEach((item, i) => {
        item[parseRowKey(props.rowKey, item)] = `${i}`;
      });
      return props.summaryData;
    }
    if (!isFunction(props.summaryFunc)) {
      return [];
    }
    let dataSource = toRaw(unref(table.getDataSource()));
    dataSource = props.summaryFunc(dataSource);
    dataSource.forEach((item, i) => {
      item[parseRowKey(props.rowKey, item)] = `${i}`;
    });
    return dataSource;
  });
 
  const getColumns = computed(() => {
    const dataSource = unref(getDataSource);
    const columns: BasicColumn[] = cloneDeep(table.getColumns());
    const index = columns.findIndex((item) => item.flag === INDEX_COLUMN_FLAG);
    const hasRowSummary = dataSource.some((item) => Reflect.has(item, SUMMARY_ROW_KEY));
    const hasIndexSummary = dataSource.some((item) => Reflect.has(item, SUMMARY_INDEX_KEY));
 
    if (index !== -1) {
      if (hasIndexSummary) {
        columns[index].customRender = ({ record }) => record[SUMMARY_INDEX_KEY];
        columns[index].ellipsis = false;
      } else {
        Reflect.deleteProperty(columns[index], 'customRender');
      }
    }
 
    if (table.getRowSelection() && hasRowSummary) {
      const isFixed = columns.some((col) => col.fixed === 'left');
      columns.unshift({
        width: 60,
        title: 'selection',
        key: 'selectionKey',
        align: 'center',
        ...(isFixed ? { fixed: 'left' } : {}),
        customRender: ({ record }) => record[SUMMARY_ROW_KEY],
      });
    }
    return columns as unknown as ColumnType[];
  });
</script>