表格多列合并

小鱼儿 2023-01-11 01:09 80阅读 0赞

要实现的效果 : 表格合并
实现步骤解析:

  1. 通过第一列型别等级判断, 将序号列和第一列内容一致的分别合并
  2. 过滤数据, 将每一个型别等级的数据存放到一个数组, 使各个型别等级之间合并时不互相影响
  3. 通过第二列注册分公司判断, 将此列内容一致的合并
  4. 再次过滤, 在同一个型别等级的基础上将每一个注册分公司的数据存放到一个数组, 使各个注册分公司之间合并时不互相影响
  5. 同一个型别等级, 同一个注册分公司下, 通过第三列运行分公司判断, 将此列内容一致的合并(正常不会出现一致的, 若三者都一样则是一条数据, 此处只为展示过程)
  6. 到此图片上合并结束

具体实现过程:

1、template 此处 sino-table 为封装 element-ui table 后的版本,具体属性可参照 element-ui

  1. <template>
  2. <div>
  3. <sino-table
  4. :data="statisticData"
  5. :span-method="cellMerge"
  6. border
  7. :height="500"
  8. :cell-style="columnStyle"
  9. :header-cell-style="{backgroundColor:'#FAFCFF'}"
  10. @cell-click='goDetil'
  11. style="width: 100%; margin-top: 20px">
  12. <sino-table-column width="60" prop="indexSpan" label="序号" align="center"></sino-table-column>
  13. <sino-table-column width="120" prop="typeGrade" label="现飞型别等级" align="center"></sino-table-column>
  14. <sino-table-column width="150" prop="corpName" label="注册分公司" align="center"></sino-table-column>
  15. <sino-table-column width="150" prop="flyCorpName" label="运行分公司" align="center"></sino-table-column>
  16. <sino-table-column width="120" align="center" prop="SUM" label="带飞总人数" show-overflow-tooltip>
  17. <template scope="scope">
  18. <span class="count-num">{ { scope.row.SUM}}</span>
  19. </template>
  20. </sino-table-column>
  21. <sino-table-column width="80" align="center" prop="F1" label="F1" show-overflow-tooltip>
  22. <template scope="scope">
  23. <span class="count-num">{ { scope.row['F1']}}</span>
  24. </template>
  25. </sino-table-column>
  26. <sino-table-column width="80" align="center" prop="F2" label="F2" show-overflow-tooltip>
  27. <template scope="scope">
  28. <span class="count-num">{ { scope.row['F2']}}</span>
  29. </template>
  30. </sino-table-column>
  31. </sino-table>
  32. </div>
  33. </template>

2、script

  1. <script>
  2. export default {
  3. data () {
  4. return {
  5. // 以假数据为例, 也可请求接口获取
  6. statisticData: [
  7. {
  8. typeGrade: "A-320",
  9. typeGradeName: "A-320",
  10. corpId: 74,
  11. corpName: "飞行总队",
  12. countMap: { F1: "2", F2: "0", SUM: "3"},
  13. flyCorpId: 4,
  14. flyCorpName: "西南分公司"
  15. },
  16. {
  17. typeGrade: "A-320",
  18. typeGradeName: "A-320",
  19. corpId: 74,
  20. corpName: "飞行总队",
  21. countMap: { F1: "0", F2: "0", SUM: "1"},
  22. flyCorpId: 5,
  23. flyCorpName: "浙江分公司"
  24. },
  25. {
  26. typeGrade: "A-320",
  27. typeGradeName: "A-320",
  28. corpId: 4,
  29. corpName: "上海分公司",
  30. countMap: { F1: "0", F2: "0", SUM: "1"},
  31. flyCorpId: 5,
  32. flyCorpName: "浙江分公司"
  33. },
  34. {
  35. typeGrade: "A-320",
  36. typeGradeName: "A-320",
  37. corpId: 4,
  38. corpName: "上海分公司",
  39. countMap: { F1: "0", F2: "0", SUM: "1"},
  40. flyCorpId: 5,
  41. flyCorpName: "浙江分公司"
  42. },
  43. {
  44. typeGrade: "A-330",
  45. typeGradeName: "A-330",
  46. corpId: 5,
  47. corpName: "浙江分公司",
  48. flyCorpId: 4,
  49. flyCorpName: "西南分公司",
  50. countMap: { F1: "0", F2: "0", SUM: "1"}
  51. },
  52. {
  53. typeGrade: "A-330",
  54. typeGradeName: "A-330",
  55. corpId: 74,
  56. corpName: "飞行总队",
  57. countMap: { F1: "1", F2: "0", SUM: "2"},
  58. flyCorpId: 4,
  59. flyCorpName: "西南分公司"
  60. }
  61. ]
  62. }
  63. },
  64. mounted () {
  65. this.init();
  66. },
  67. methods: {
  68. init () {
  69. let data = [];
  70. this.statisticData.map((item, index) => {
  71. // 处理数据,使countMap(SUM F1 F2)内容渲染到列表
  72. data.push({
  73. index,
  74. ...item,
  75. ...item.countMap
  76. });
  77. });
  78. // 合并typeGrade(第一列型别等级)内容一致的
  79. this.statisticData = this.mergeTableRow(data, ['index', 'typeGrade']);
  80. let typeArr = [];
  81. let corpArr = [];
  82. // 将第一列内容存放数组且去重
  83. this.statisticData.map(item => {
  84. typeArr.push(item.typeGrade);
  85. });
  86. typeArr = Array.from(new Set(typeArr));
  87. // 循环表格数据
  88. this.statisticData.map(item => {
  89. // 循环第一列内容存放数组
  90. typeArr.map(it => {
  91. // 过滤出每一大行的数据,使每行之后的数据不会互相影响
  92. let a = this.statisticData.filter(item => {
  93. return item.typeGrade == it;
  94. });
  95. // 合并每大行corpName(第二列)内容一致的
  96. this.mergeTableRow(a, ['index', 'corpName']);
  97. // 继续之前的操作,基于a数组(每一大行的数据)对第三列进行判断合并,若有多列需要合并可一直执行下去,不建议过多以防性能不好
  98. // 将第二列内容存放数组且去重
  99. a.map(item => {
  100. corpArr.push(item.corpId);
  101. });
  102. corpArr = Array.from(new Set(corpArr));
  103. // 循环表格每一大行数据
  104. a.map(item => {
  105. // 循环第二列内容存放数组
  106. corpArr.map(it => {
  107. // 过滤出每一小行的数据
  108. let b = a.filter(item => {
  109. return item.corpId == it;
  110. });
  111. // 合并每小行flyCorpName(第三列)内容一致的
  112. this.mergeTableRow(b, ['index', 'flyCorpName']);
  113. });
  114. });
  115. });
  116. });
  117. },
  118. // 合并内容相等的某一列(传入的merge)
  119. mergeTableRow (data, merge) {
  120. if (!merge || merge.length === 0) {
  121. return data;
  122. }
  123. merge.forEach((m) => {
  124. const mList = { };
  125. data = data.map((v, index) => {
  126. const rowVal = v[m];
  127. if (mList[rowVal]) {
  128. mList[rowVal]++;
  129. data[index - (mList[rowVal] - 1)][m + '-span'].rowspan++;
  130. v[m + '-span'] = {
  131. rowspan: 0,
  132. colspan: 0
  133. };
  134. } else {
  135. mList[rowVal] = 1;
  136. v[m + '-span'] = {
  137. rowspan: 1,
  138. colspan: 1
  139. };
  140. }
  141. return v;
  142. });
  143. });
  144. return data;
  145. },
  146. // 点击单元格
  147. goDetil (row, col, cell) {
  148. if (row[col['property']]) {
  149. let query = {
  150. 'column': col['property']
  151. };
  152. this.$emit('transmit', query); // 传递给父组件参数
  153. };
  154. },
  155. // 合并行或列
  156. cellMerge ({ row, column, rowIndex, columnIndex }) {
  157. if (columnIndex == 0) {
  158. let _row = this.indexFilter(this.statisticData, 'typeGrade')[rowIndex];
  159. let _col = _row > 0 ? 1 : 0;
  160. const arr = this.indexFilter(this.statisticData, 'typeGrade');
  161. var Nosort = 0;
  162. for (let i in arr) {
  163. if (arr[i] > 0) {
  164. Nosort += 1;
  165. };
  166. for (let j in this.statisticData) {
  167. if (i == j) {
  168. // indexSpan 为表格序号列
  169. this.statisticData[i].indexSpan= Nosort;
  170. this.statisticData[i].indexSpan == 0 ? this.statisticData[i].indexSpan= null : this.statisticData[i].indexSpan= Nosort; // 去掉序号0
  171. }
  172. };
  173. };
  174. return [_row, _col];
  175. };
  176. const span = column['property'] + '-span';
  177. if (row[span]) {
  178. return row[span];
  179. }
  180. },
  181. // 通过传入的type(除序号第一列)使序号列进行合并
  182. indexFilter (arr, type) {
  183. var spanOneArr = [];
  184. var concatone = 0;
  185. arr.forEach((item, index) => {
  186. if (index == 0) {
  187. spanOneArr.push(1);
  188. } else {
  189. if (item[type] == arr[index - 1][type]) {
  190. spanOneArr[concatone] += 1;
  191. spanOneArr.push(0);
  192. } else {
  193. spanOneArr.push(1);
  194. concatone = index;
  195. }
  196. }
  197. });
  198. return spanOneArr;
  199. },
  200. columnStyle ({ row, column, rowIndex, columnIndex }) {
  201. if (columnIndex === 0) { // 第1列的背景色就改变了
  202. return 'background:rgb(250, 252, 255);';
  203. }
  204. }
  205. }
  206. };
  207. </script>

发表评论

表情:
评论列表 (有 0 条评论,80人围观)

还没有评论,来说两句吧...

相关阅读

    相关 合并表格第一

    最近老是遇见第一列是地市,第二列是区县。说一堆区县对应一个地市看起来不太好看,要把第一列的地市合并在一起。故写了个小小的Demo,留待以后复用。 先看效果图,表一为原数据效果