Blame view

ant-design-vue-jeecg/src/views/jeecg/TableTotal.vue 4.7 KB
肖超群 authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<template>
  <a-card :bordered="false">
    <a-table
      rowKey="rowIndex"
      bordered
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      @change="handleTableChange"
    >
    </a-table>
  </a-card>
</template>

<script>
肖超群 authored
16
17
18
19
20
21
22
23
24
25
26
27
28
export default {
  name: 'TableTotal',
  data() {
    return {
      columns: [
        {
          title: '#',
          width: '180px',
          align: 'center',
          dataIndex: 'rowIndex',
          customRender: function (text, r, index) {
            return (text !== '合计') ? (parseInt(index) + 1) : text
          }
肖超群 authored
29
        },
肖超群 authored
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
93
94
95
96
        {
          title: '姓名',
          dataIndex: 'name',
        },
        {
          title: '贡献点',
          dataIndex: 'point',
        },
        {
          title: '等级',
          dataIndex: 'level',
        },
        {
          title: '更新时间',
          dataIndex: 'updateTime',
        },
      ],
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + "-" + range[1] + " 共" + total + "条"
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      dataSource: [
        {id: "1", name: '张三', point: 23, level: 3, updateTime: '2019-8-14'},
        {name: '小王', point: 6, level: 1, updateTime: '2019-8-13'},
        {name: '李四', point: 53, level: 8, updateTime: '2019-8-12'},
        {name: '小红', point: 44, level: 5, updateTime: '2019-8-11'},
        {name: '王五', point: 97, level: 10, updateTime: '2019-8-10'},
        {name: '小明', point: 33, level: 2, updateTime: '2019-8-10'},
        {name: '小张', point: 33, level: 2, updateTime: '2019-8-10'},
        {name: '小六', point: 33, level: 2, updateTime: '2019-8-10'},
        {name: '小五', point: 33, level: 2, updateTime: '2019-8-10'},
        {name: '小赵', point: 33, level: 2, updateTime: '2019-8-10'},
        {name: '李华', point: 33, level: 2, updateTime: '2019-8-10'},
        {name: '小康', point: 33, level: 2, updateTime: '2019-8-10'},
        {name: '小鹿', point: 33, level: 2, updateTime: '2019-8-10'},
      ],
      newArr: [],
      newDataSource: [],
    }
  },
  mounted() {
    // this.tableAddTotalRow(this.columns, this.dataSource)
    /*新增分页合计方法*/
    this.newDataSource = this.dataSource
    this.dataHandling(this.ipagination.pageSize - 1)
  },
  watch: {
    'ipagination.pageSize': function (val) {
      this.dataHandling(val - 1)
    }
  },
  methods: {
    /** 表格增加合计行 */
    tableAddTotalRow(columns, dataSource) {
      let numKey = 'rowIndex'
      let totalRow = {[numKey]: '合计'}
      columns.forEach(column => {
        let {key, dataIndex} = column
        if (![key, dataIndex].includes(numKey)) {
肖超群 authored
97
肖超群 authored
98
99
100
101
102
          let total = 0
          dataSource.forEach(data => {
            total += /^\d+\.?\d?$/.test(data[dataIndex]) ? Number.parseInt(data[dataIndex]) : Number.NaN
            console.log(data[dataIndex], ':', (/^\d+\.?\d?$/.test(data[dataIndex]) ? Number.parseInt(data[dataIndex]) : Number.NaN))
          })
肖超群 authored
103
肖超群 authored
104
105
          if (Number.isNaN(total)) {
            total = '-'
肖超群 authored
106
          }
肖超群 authored
107
108
109
          totalRow[dataIndex] = total
        }
      })
肖超群 authored
110
肖超群 authored
111
112
113
114
115
116
117
118
119
120
121
122
123
124
      dataSource.push(totalRow)
    },
    handleTableChange(pagination, filters, sorter) {
      this.ipagination = pagination;
    },
    /*如果分页走这个方法*/
    dataHandling(num) {
      this.newArr = [];
      let arrLength = this.newDataSource.length; // 数组长度;
      let index = 0;
      for (let i = 0; i < arrLength; i++) {
        if (i % num === 0 && i !== 0) {
          this.newArr.push(this.newDataSource.slice(index, i));
          index = i;
肖超群 authored
125
        }
肖超群 authored
126
127
        if ((i + 1) === arrLength) {
          this.newArr.push(this.newDataSource.slice(index, (i + 1)));
肖超群 authored
128
        }
肖超群 authored
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
      }
      var arrs = this.newArr;
      for (let i = 0; i < arrs.length; i++) {
        let arr = arrs[i];
        let newArr = {};
        newArr.name = "-";
        newArr.updateTime = "-";
        newArr.rowIndex = "合计"
        var level = 0;
        var point = 0;
        for (let j = 0; j < arr.length; j++) {
          level += arr[j].level;
          point += arr[j].point;
        }
        newArr.level = level;
        newArr.point = point;
        arrs[i].push(newArr);
      }
      var newDataSource = [];
      for (let i = 0; i < arrs.length; i++) {
        let arr = arrs[i];
        for (var j in arr) {
          newDataSource.push(arr[j]);
肖超群 authored
152
153
        }
      }
肖超群 authored
154
155
156
      console.log(this.dataSource)
      this.dataSource = Object.values(newDataSource);
      console.log(this.dataSource)
肖超群 authored
157
158
    }
  }
肖超群 authored
159
}
肖超群 authored
160
161
162
163
164
</script>

<style scoped>

</style>