table.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * @Author: Badguy
  3. * @Date: 2022-02-11 09:20:58
  4. * @LastEditTime: 2022-05-25 17:55:05
  5. * @LastEditors: your name
  6. * @Description: 表格用
  7. * have a nice day!
  8. */
  9. import _ from 'lodash'
  10. /**
  11. * @description: 表格行合并
  12. * @param {Object} config
  13. * @return {Array}
  14. */
  15. export function mergeTableRow(config) {
  16. let data = config.data
  17. const { mergeColNames, firstMergeColNames, firstMerge } = config
  18. if (!mergeColNames || mergeColNames.length === 0) {
  19. return data
  20. }
  21. mergeColNames.forEach(m => {
  22. const mList = {}
  23. data = data.map((v, index) => {
  24. const rowVal = v[m]
  25. if (mList[rowVal] && mList[rowVal].newIndex === index) {
  26. const flag =
  27. firstMergeColNames.filter(f => {
  28. return f === m
  29. }).length !== 0
  30. const mcFlag =
  31. mergeColNames.filter(mc => {
  32. return mc === firstMerge
  33. }).length === 0
  34. if (
  35. (mcFlag && flag) ||
  36. (flag && data[index][firstMerge + '-span'] && data[index][firstMerge + '-span'].rowspan === 1)
  37. ) {
  38. v[m + '-span'] = {
  39. rowspan: 1,
  40. colspan: 1
  41. }
  42. } else {
  43. data[mList[rowVal]['index']][m + '-span'].rowspan++
  44. v[m + '-span'] = {
  45. rowspan: 0,
  46. colspan: 0
  47. }
  48. mList[rowVal]['num']++
  49. mList[rowVal]['newIndex']++
  50. }
  51. } else {
  52. mList[rowVal] = { num: 1, index: index, newIndex: index + 1 }
  53. v[m + '-span'] = {
  54. rowspan: 1,
  55. colspan: 1
  56. }
  57. }
  58. return v
  59. })
  60. })
  61. return data
  62. }
  63. // 表格单元格class设置
  64. export function commonTableCellClass({ row, column, rowIndex, columnIndex }) {
  65. const classes = []
  66. if (['ActualDepartureTime', 'ActualLandingTime'].includes(column.property)) {
  67. classes.push('pre-line')
  68. }
  69. return classes
  70. }
  71. // 获取对应时区的时间
  72. export function timeInZone(date, timeZone = 0, local = 8) {
  73. if (!(date instanceof Date)) {
  74. if (typeof date === 'string' && date.length) {
  75. date = new Date(date)
  76. } else {
  77. return ''
  78. }
  79. }
  80. function formatDate(num) {
  81. return num < 10 ? '0' + num : num
  82. }
  83. const time = date.getTime() + (timeZone - local) * 60 * 60 * 1000
  84. date.setTime(time)
  85. const year = date.getFullYear()
  86. const month = formatDate(date.getMonth() + 1)
  87. const day = formatDate(date.getDate())
  88. const hour = formatDate(date.getHours())
  89. const minute = formatDate(date.getMinutes())
  90. const second = formatDate(date.getSeconds())
  91. return `${year}-${month}-${day} ${hour}:${minute}:${second}`
  92. }
  93. // // 表格添加过滤条件
  94. // export function setTableFilters(tableData, filterKeys) {
  95. // const tempSets = {}
  96. // const result = {}
  97. // filterKeys.forEach(key => {
  98. // tempSets[key] = new Set()
  99. // })
  100. // tableData.forEach(item => {
  101. // Object.keys(tempSets).forEach(key => {
  102. // (item[key] ?? '') !== '' && tempSets[key].add(item[key])
  103. // })
  104. // })
  105. // Object.keys(tempSets).forEach(key => {
  106. // result[key] = _.sortBy(
  107. // [...tempSets[key]].map(value => ({
  108. // text: value,
  109. // value
  110. // })),
  111. // ['value']
  112. // )
  113. // })
  114. // return result
  115. // }
  116. // 表格添加过滤条件
  117. export function setTableFilters(tableData, filters) {
  118. const tempSets = {}
  119. Object.keys(filters).forEach(key => {
  120. tempSets[key] = new Set()
  121. })
  122. tableData.forEach(item => {
  123. Object.keys(tempSets).forEach(key => {
  124. (item[key] ?? '') !== '' && tempSets[key].add(item[key])
  125. })
  126. })
  127. Object.keys(tempSets).forEach(key => {
  128. filters[key] = _.orderBy(
  129. [...tempSets[key]].map(value => ({
  130. text: value,
  131. value
  132. })),
  133. o => o.value
  134. )
  135. })
  136. }