Newer
Older
KaiFengPC / src / views / preassess / evaluation / mixins / index.js
@zhangdeliang zhangdeliang on 23 May 2 KB 初始化项目
  1. import { ref, computed } from 'vue'
  2. export default function useTable(proxy, opts, fields) {
  3. const treeData = ref([])
  4. const loading = ref(false)
  5. const tableData = computed(() => {
  6. const list = getFlatData(JSON.parse(JSON.stringify(treeData.value)))
  7. return list.filter(item => item[fields])
  8. })
  9.  
  10. const getFlatData = (data) => {
  11. let list = []
  12. for (const item of data) {
  13. const children = item.children
  14. delete item.children
  15. list.push(item)
  16. if(children){
  17. list = list.concat(getFlatData(children))
  18. }
  19. }
  20. return list
  21. }
  22.  
  23. const getTreeDataCurRow = (data, row, treeDataCurRow) => {
  24. if(!row?.nodeCode || row.nodeCode === '0') {
  25. treeDataCurRow.value = { children: data }
  26. return
  27. }
  28. for (const item of data) {
  29. if(row.nodeCode === item.nodeCode){
  30. treeDataCurRow.value = item
  31. break
  32. }
  33. if(item.children){
  34. getTreeDataCurRow(item.children, row, treeDataCurRow)
  35. }
  36. }
  37. }
  38.  
  39. const getTreeCurRow = (data, row) => {
  40. let treeDataCurRow = { value: {} }
  41. getTreeDataCurRow(data, row, treeDataCurRow)
  42. return treeDataCurRow.value
  43. }
  44.  
  45. const setMergeData = (data) => {
  46. let length1 = 0
  47. let length2 = 0
  48. for (const item1 of data) {
  49. const children1 = item1.children || []
  50. const sum = getSum(children1)
  51. length1 += sum
  52. for (const item2 of children1) {
  53. const children2 = item2.children || []
  54. length2 += children2.length
  55. for (const item3 of children2) {
  56. item3.length1 = length1
  57. item3.sum1 = sum
  58. item3.length2 = length2
  59. item3.sum2 = children2.length
  60. }
  61. }
  62. }
  63. }
  64.  
  65. const getSum = (data) => {
  66. let sum = 0
  67. for (const item of data) {
  68. const children = item.children || []
  69. for (const it of children) {
  70. sum++
  71. if(it.children) {
  72. getSum(it.children)
  73. }
  74. }
  75. }
  76. return sum
  77. }
  78.  
  79. const handlePreview = (file) => {
  80. if(opts.type === 'view') return
  81. // window.open(file.url, '_blank')
  82. proxy.$modal
  83. .confirm(`下载此文件: ${file.name}?`)
  84. .then(() => {
  85. window.open(file.url)
  86. })
  87. }
  88.  
  89. const uploadSuccess = (file, row) => {
  90. const treeDataCurRow = getTreeCurRow(treeData.value, row)
  91. treeDataCurRow.fileSaveRequestList.push(file)
  92. }
  93.  
  94. const removeFile = (file, row) => {
  95. proxy.$modal
  96. .confirm("是否确认删除?")
  97. .then(() => {
  98. const treeDataCurRow = getTreeCurRow(treeData.value, row)
  99. const index = treeDataCurRow.fileSaveRequestList.findIndex(it => it.url === file.url)
  100. treeDataCurRow.fileSaveRequestList.splice(index, 1)
  101. })
  102. }
  103.  
  104. return {
  105. treeData,
  106. loading,
  107. tableData,
  108. getTreeCurRow,
  109. setMergeData,
  110. getSum,
  111. handlePreview,
  112. uploadSuccess,
  113. removeFile
  114. }
  115. }