b293a661e420fd607019b784da0694c72cffc12d.svn-base 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. *
  3. * @param {Array} mergeKeys 要合并的keys数组
  4. * @param {Array} tableData 渲染table组件的原始数据
  5. */
  6. export function handleTableSpan(mergeKeys, tableData) {
  7. const spanObj = {}
  8. if (mergeKeys instanceof Array && tableData instanceof Array && mergeKeys.length && tableData.length) {
  9. mergeKeys.forEach(key => {
  10. spanObj[key] = []
  11. let position = 0
  12. tableData.forEach((item, index) => {
  13. if (index === 0) {
  14. spanObj[key].push(1)
  15. position = 0
  16. } else {
  17. // 如果要合并的key 值是引用数据类型,我们要先把 数组或者 对象转成json 格式然后对比
  18. if (tableData[index][key] instanceof Array || tableData[index][key] instanceof Object) {
  19. if (JSON.stringify(tableData[index][key]) === JSON.stringify(tableData[index - 1][key])) {
  20. spanObj[key][position] += 1
  21. spanObj[key].push(0)
  22. } else {
  23. spanObj[key].push(1)
  24. position = index
  25. }
  26. } else {
  27. if (tableData[index][key] === tableData[index - 1][key]) {
  28. spanObj[key][position] += 1
  29. spanObj[key].push(0)
  30. } else {
  31. spanObj[key].push(1)
  32. position = index
  33. }
  34. }
  35. }
  36. })
  37. })
  38. return spanObj
  39. }
  40. }
  41. /**
  42. *
  43. * @param {Object} tableObj table组件 span-method 方法四个参数{ row, column, rowIndex, columnIndex } 的对象
  44. * @param {Array} mergeKeys 要合并的keys数组
  45. * @param {Object} rowspanObj 经过 handleTableSpan 方法处理tableData 数据,得到的单元格的合并行数对象
  46. * @param {Boolean} notEmpty 是否要合并有 ’/‘ 单元格,此参数非必传。默认不合并, notEmpty = true 则合并
  47. */
  48. export function handleObjectSpanMethod(tableObj, mergeKeys, rowspanObj, notEmpty) {
  49. if (mergeKeys instanceof Array && rowspanObj instanceof Object) {
  50. const { row, column, rowIndex, columnIndex } = tableObj
  51. for (let i = 0; i < mergeKeys.length; i++) {
  52. let term
  53. if (notEmpty) {
  54. term = column.property === mergeKeys[i] && row[mergeKeys[i]] !== '/'
  55. } else {
  56. term = column.property === mergeKeys[i]
  57. }
  58. if (term) {
  59. const _row = rowspanObj[mergeKeys[i]][rowIndex]
  60. const _col = _row > 0 ? 1 : 0
  61. return {
  62. rowspan: _row,
  63. colspan: _col
  64. }
  65. }
  66. }
  67. }
  68. }