当前位置:   article > 正文

vue项目 table的展示列控制+行计数+table相同行的合并_如何控制eltable的行数

如何控制eltable的行数

一、效果

二、实现代码

  1. <template>
  2. <div>
  3. <div>
  4. <el-popover
  5. placement="bottom"
  6. title="展示列控制"
  7. width="350"
  8. trigger="click">
  9. <el-checkbox-group v-model="checkedColumnsValue" >
  10. <el-checkbox v-for="item in columnlist" :label="item.value" :key="item.value">{{item.name}}</el-checkbox>
  11. </el-checkbox-group>
  12. <i class="el-icon-s-grid column-icon" alt="设置" title="设置" slot="reference" ></i>
  13. </el-popover>
  14. <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" :summary-method="getSummaries"
  15. show-summary row-key="RowKey" @selection-change="handleSelectionChange" :span-method="objectSpanMethod">
  16. <el-table-column v-if="checkedColumnsValue.includes('index')" type="index" label="序号"> </el-table-column>
  17. <el-table-column v-if="checkedColumnsValue.includes('schoolName')" prop="schoolName" label="学校名称" sortable> </el-table-column>
  18. <el-table-column v-if="checkedColumnsValue.includes('dep')" prop="dep" label="检查部门" width="160" show-overflow-tooltip sortable> </el-table-column>
  19. <el-table-column v-if="checkedColumnsValue.includes('systemClassify')" prop="systemClassify" label="系统类型" w show-overflow-tooltip sortable> </el-table-column>
  20. <el-table-column v-if="checkedColumnsValue.includes('lessonType')" prop="lessonType" label="课程类型" show-overflow-tooltip sortable> </el-table-column>
  21. <el-table-column v-if="checkedColumnsValue.includes('lessonName')" prop="lessonName" label="课程名称" show-overflow-tooltip sortable> </el-table-column>
  22. <el-table-column v-if="checkedColumnsValue.includes('num')" prop="num" label="数量" width="160" show-overflow-tooltip sortable> </el-table-column>
  23. </el-table>
  24. </div>
  25. </div>
  26. </template>
  27. <script>
  28. export default {
  29. name:'page',
  30. data() {
  31. return {
  32. tableData:[
  33. {
  34. num: 1,
  35. dep: "教导处",
  36. lessonName: "化学",
  37. lessonType: "理科",
  38. hospitalCode: "sch",
  39. schoolName: "学校名称",
  40. systemClassify: "系统1",
  41. },
  42. {
  43. num: 3,
  44. dep: "教导处",
  45. lessonName: "物理",
  46. lessonType: "理科",
  47. hospitalCode: "sch",
  48. schoolName: "学校名称",
  49. systemClassify: "系统1",
  50. },{
  51. num: 1,
  52. dep: "教导处",
  53. lessonName: "地理",
  54. lessonType: "文科",
  55. hospitalCode: "sch",
  56. schoolName: "学校名称",
  57. systemClassify: "系统2"
  58. }
  59. ],
  60. multipleSelection: [],//table多选
  61. checkedColumnsValue:[],
  62. columnlist:[
  63. {name:'序号', value:'index'} ,
  64. {name:'学校名称', value:'schoolName'} ,
  65. {name:'检查部门', value:'dep'} ,
  66. {name:'系统类型', value:'systemClassify'} ,
  67. {name:'课程类型', value:'lessonType'} ,
  68. {name:'疾病名称', value:'lessonName'} ,
  69. {name:'数量', value:'num'} ,
  70. ],
  71. spanArr:{
  72. index:[],
  73. schoolName:[],
  74. dep:[],
  75. systemClassify:[],
  76. lessonType:[],
  77. lessonName:[],
  78. num:[]
  79. },//数组合并span
  80. pos:{
  81. index:[],
  82. schoolName:[],
  83. dep:[],
  84. systemClassify:[],
  85. lessonType:[],
  86. lessonName:[],
  87. num:[]
  88. },
  89. }
  90. },
  91. mounted() {
  92. this.fetchData()
  93. },
  94. methods: {
  95. //得到计算数值
  96. getSummaries(param) {
  97. const { columns, data } = param
  98. const sums = []
  99. columns.forEach((column, index) => {
  100. if (index === 0) {
  101. sums[index] = '总计'
  102. return
  103. }
  104. if (index === 1) {
  105. sums[index] = ''
  106. return
  107. }
  108. const values = data.map(item => Number(item[column.property]))
  109. if (!values.every(value => isNaN(value))) {
  110. sums[index] = values.reduce((prev, curr) => {
  111. const value = Number(curr)
  112. if (!isNaN(value)) {
  113. return prev + curr
  114. } else {
  115. return prev
  116. }
  117. }, 0)
  118. } else {
  119. sums[index] = ''
  120. }
  121. })
  122. return sums
  123. },
  124. fetchData(){
  125. this.checkedColumnsValue = []
  126. this.columnlist.forEach((item) => {
  127. this.checkedColumnsValue.push(item.value);
  128. });
  129. let len = this.columnlist.length
  130. for(let i = 0; i < len; i++ ){
  131. this.getSpanArr(this.tableData, this.columnlist[i].value)
  132. }
  133. },
  134. //相同行的合并
  135. getSpanArr(data, column) { 
  136. //清空
  137. this.spanArr[column] = []
  138.  this.pos[column] = []
  139. for (var i = 0; i < data.length; i++) {
  140.     if (i === 0) {
  141.         this.spanArr[column].push(1);
  142.         this.pos[column] = 0
  143.     } else {
  144.     // 判断当前元素与上一个元素是否相同
  145. if (data[i][column] === data[i - 1][column]) {
  146.         this.spanArr[column][this.pos[column]] += 1;
  147.         this.spanArr[column].push(0);
  148.         } else {
  149.         this.spanArr[column].push(1);
  150.         this.pos[column] = i;
  151.         }
  152.     }
  153.   }
  154. },
  155. objectSpanMethod({ row, column, rowIndex, columnIndex }) {
  156. //1-5列有相同的合并
  157. if (columnIndex >=1 && columnIndex <= 5) {
  158.       const _row = this.spanArr[column.property][rowIndex];
  159.         const _col = _row > 0 ? 1 : 0;
  160.         return {
  161.          rowspan: _row,
  162.            colspan: _col
  163.         }
  164.     }
  165. },
  166. //table选择行改变
  167. handleSelectionChange(val) {
  168. this.multipleSelection = val;
  169. },
  170. }
  171. }
  172. </script>
  173. <style lang="scss" scoped>
  174. .table-wrap {
  175. .el-table{
  176. margin-top: 10px;
  177. }
  178. .column-icon{
  179. font-size: 20px;
  180. float: right;
  181. margin-top: 10px;
  182. color: #047d9b;
  183. }
  184. .el-checkbox{
  185. display: block;
  186. }
  187. }
  188. </style>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/103263
推荐阅读
相关标签