21a052504a8741d1cabaaa3039d5fd6fce61ee24.svn-base 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <template>
  2. <a-card :bordered="false">
  3. <a-table
  4. rowKey="rowIndex"
  5. bordered
  6. :columns="columns"
  7. :dataSource="dataSource"
  8. :pagination="ipagination"
  9. @change="handleTableChange"
  10. >
  11. </a-table>
  12. </a-card>
  13. </template>
  14. <script>
  15. export default {
  16. name: 'TableTotal',
  17. data() {
  18. return {
  19. columns: [
  20. {
  21. title: '#',
  22. width: '180px',
  23. align: 'center',
  24. dataIndex: 'rowIndex',
  25. customRender: function (text, r, index) {
  26. return (text !== '合计') ? (parseInt(index) + 1) : text
  27. }
  28. },
  29. {
  30. title: '姓名',
  31. dataIndex: 'name',
  32. },
  33. {
  34. title: '贡献点',
  35. dataIndex: 'point',
  36. },
  37. {
  38. title: '等级',
  39. dataIndex: 'level',
  40. },
  41. {
  42. title: '更新时间',
  43. dataIndex: 'updateTime',
  44. },
  45. ],
  46. /* 分页参数 */
  47. ipagination:{
  48. current: 1,
  49. pageSize: 10,
  50. pageSizeOptions: ['10', '20', '30'],
  51. showTotal: (total, range) => {
  52. return range[0] + "-" + range[1] + " 共" + total + "条"
  53. },
  54. showQuickJumper: true,
  55. showSizeChanger: true,
  56. total: 0
  57. },
  58. dataSource: [
  59. { id:"1",name: '张三', point: 23, level: 3, updateTime: '2019-8-14' },
  60. { name: '小王', point: 6, level: 1, updateTime: '2019-8-13' },
  61. { name: '李四', point: 53, level: 8, updateTime: '2019-8-12' },
  62. { name: '小红', point: 44, level: 5, updateTime: '2019-8-11' },
  63. { name: '王五', point: 97, level: 10, updateTime: '2019-8-10' },
  64. { name: '小明', point: 33, level: 2, updateTime: '2019-8-10' },
  65. { name: '小张', point: 33, level: 2, updateTime: '2019-8-10' },
  66. { name: '小六', point: 33, level: 2, updateTime: '2019-8-10' },
  67. { name: '小五', point: 33, level: 2, updateTime: '2019-8-10' },
  68. { name: '小赵', point: 33, level: 2, updateTime: '2019-8-10' },
  69. { name: '李华', point: 33, level: 2, updateTime: '2019-8-10' },
  70. { name: '小康', point: 33, level: 2, updateTime: '2019-8-10' },
  71. { name: '小鹿', point: 33, level: 2, updateTime: '2019-8-10' },
  72. ],
  73. newArr:[],
  74. newDataSource:[],
  75. }
  76. },
  77. mounted() {
  78. // this.tableAddTotalRow(this.columns, this.dataSource)
  79. /*新增分页合计方法*/
  80. this.newDataSource=this.dataSource
  81. this.dataHandling(this.ipagination.pageSize-1)
  82. },
  83. watch:{
  84. 'ipagination.pageSize':function(val) {
  85. this.dataHandling(val-1)
  86. }
  87. },
  88. methods: {
  89. /** 表格增加合计行 */
  90. tableAddTotalRow(columns, dataSource) {
  91. let numKey = 'rowIndex'
  92. let totalRow = { [numKey]: '合计' }
  93. columns.forEach(column => {
  94. let { key, dataIndex } = column
  95. if (![key, dataIndex].includes(numKey)) {
  96. let total = 0
  97. dataSource.forEach(data => {
  98. total += /^\d+\.?\d?$/.test(data[dataIndex]) ? Number.parseInt(data[dataIndex]) : Number.NaN
  99. console.log(data[dataIndex], ':', (/^\d+\.?\d?$/.test(data[dataIndex]) ? Number.parseInt(data[dataIndex]) : Number.NaN))
  100. })
  101. if (Number.isNaN(total)) {
  102. total = '-'
  103. }
  104. totalRow[dataIndex] = total
  105. }
  106. })
  107. dataSource.push(totalRow)
  108. },
  109. handleTableChange(pagination, filters, sorter) {
  110. this.ipagination = pagination;
  111. },
  112. /*如果分页走这个方法*/
  113. dataHandling(num) {
  114. this.newArr=[];
  115. let arrLength = this.newDataSource.length; // 数组长度;
  116. let index = 0;
  117. for (let i = 0; i < arrLength; i++) {
  118. if (i % num === 0 && i !== 0) {
  119. this.newArr.push(this.newDataSource.slice(index, i));
  120. index = i;
  121. }
  122. if ((i + 1) === arrLength) {
  123. this.newArr.push(this.newDataSource.slice(index, (i + 1)));
  124. }
  125. }
  126. var arrs=this.newArr;
  127. for (let i =0;i<arrs.length;i++){
  128. let arr = arrs[i];
  129. let newArr= { };
  130. newArr.name="-";
  131. newArr.updateTime="-";
  132. newArr.rowIndex="合计"
  133. var level=0;
  134. var point=0;
  135. for (let j=0;j<arr.length;j++){
  136. level+=arr[j].level;
  137. point+=arr[j].point;
  138. }
  139. newArr.level=level;
  140. newArr.point=point;
  141. arrs[i].push(newArr);
  142. }
  143. var newDataSource=[];
  144. for (let i =0;i<arrs.length;i++){
  145. let arr = arrs[i];
  146. for(var j in arr){
  147. newDataSource.push(arr[j]);
  148. }
  149. }
  150. console.log(this.dataSource)
  151. this.dataSource = Object.values(newDataSource);
  152. console.log(this.dataSource)
  153. }
  154. }
  155. }
  156. </script>
  157. <style scoped>
  158. </style>