Newer
Older
KaiFengPC / src / views / spongePerformance / longTerm / examinemanage / mixins / index.js
@zhangdeliang zhangdeliang on 23 May 12 KB 初始化项目
  1. import { ref, reactive, nextTick } from 'vue'
  2. export default function useTable(proxy, uuidv4) {
  3. const router = useRouter();
  4. const treeData = ref([])
  5. const visible = ref(false)
  6. const curRow = ref({})
  7. const form = reactive({
  8. name: ''
  9. })
  10. const newName = ref('')
  11. const operateInfo = reactive({
  12. width: '0%',
  13. level: '',
  14. type: '',
  15. text: ''
  16. })
  17. let rowExpansionInfo = []
  18. const getTreeDataCurRow = (data, row, treeDataCurRow) => {
  19. if(!row?.nodeCode || row.nodeCode === '0') {
  20. treeDataCurRow.value = { children: data }
  21. return
  22. }
  23. for (const item of data) {
  24. if(row.nodeCode === item.nodeCode){
  25. treeDataCurRow.value = item
  26. break
  27. }
  28. if(item.children){
  29. getTreeDataCurRow(item.children, row, treeDataCurRow)
  30. }
  31. }
  32. }
  33.  
  34. const getTreeCurRow = (data, row) => {
  35. let treeDataCurRow = { value: {} }
  36. getTreeDataCurRow(data, row, treeDataCurRow)
  37. return treeDataCurRow.value
  38. }
  39.  
  40. const goBack = (route, path) => {
  41. proxy.$tab.closePage(route)
  42. router.push(path)
  43. }
  44.  
  45. const close = () => {
  46. curRow.value = {}
  47. operateInfo.level = ''
  48. operateInfo.type = ''
  49. operateInfo.text = ''
  50. restForm()
  51. visible.value = false
  52. }
  53.  
  54. const restForm = () => {
  55. proxy.$refs.dialogFormRef.resetFields()
  56. }
  57.  
  58. const statusChange = (row, val) => {
  59. const treeDataCurRow = getTreeCurRow(treeData.value, row)
  60. setTreeDataStatus(treeDataCurRow, val)
  61. }
  62.  
  63. const setTreeDataStatus = (row, status) => {
  64. row.status = status
  65. if(status === '0'){
  66. row.enableTime = ''
  67. row.stopTime = proxy.moment().format("YYYY-MM-DD HH:mm:ss")
  68. } else if(status === '1'){
  69. row.enableTime = proxy.moment().format("YYYY-MM-DD HH:mm:ss")
  70. row.stopTime = ''
  71. }
  72. if(row.children){
  73. for (const item of row.children) {
  74. item.statusDisabled = status === '0'
  75. if(status === '0') {
  76. setTreeDataStatus(item, '0')
  77. }
  78. }
  79. }
  80. }
  81.  
  82. const setStatusDisabled = (data) => {
  83. for (const item1 of data) {
  84. const children1 = item1.children || []
  85. item1.statusDisabled = false
  86. for (const item2 of children1) {
  87. const children2 = item2.children || []
  88. if(item1.status === '0' && item2.status === '0'){
  89. item2.statusDisabled = true
  90. } else {
  91. item2.statusDisabled = false
  92. }
  93. for (const item3 of children2) {
  94. if(item2.status === '0' && item3.status === '0'){
  95. item3.statusDisabled = true
  96. } else {
  97. item3.statusDisabled = false
  98. }
  99. }
  100. }
  101. }
  102. }
  103.  
  104. const canOperateChange = (row, val) => {
  105. const treeDataCurRow = getTreeCurRow(treeData.value, row)
  106. setTreeDataCanOperate(treeDataCurRow, val)
  107. }
  108.  
  109. const setTreeDataCanOperate = (row, canOperate) => {
  110. row.canOperate = canOperate
  111. if(row.children){
  112. for (const item of row.children) {
  113. item.canOperateDisabled = canOperate === 0
  114. if(canOperate === 0) {
  115. setTreeDataCanOperate(item, 0)
  116. }
  117. }
  118. }
  119. }
  120.  
  121. const setCanOperateDisabled = (data) => {
  122. for (const item1 of data) {
  123. const children1 = item1.children || []
  124. item1.canOperateDisabled = false
  125. for (const item2 of children1) {
  126. const children2 = item2.children || []
  127. if(item1.canOperate === 0 && item2.canOperate === 0){
  128. item2.canOperateDisabled = true
  129. } else {
  130. item2.canOperateDisabled = false
  131. }
  132. for (const item3 of children2) {
  133. if(item2.canOperate === 0 && item3.canOperate === 0){
  134. item3.canOperateDisabled = true
  135. } else {
  136. item3.canOperateDisabled = false
  137. }
  138. }
  139. }
  140. }
  141. }
  142.  
  143. const copy = (row) => {
  144. const treeDataCurRow = getTreeCurRow(treeData.value, row)
  145. const treeDataCurParentRow = getTreeCurRow(treeData.value, { nodeCode: row.parentNodeCode })
  146. proxy.$modal
  147. .confirm("是否确认复制?")
  148. .then(async () => {
  149. const index = treeDataCurParentRow.children.indexOf(treeDataCurRow)
  150. const data = JSON.parse(JSON.stringify(row))
  151. data.nodeCode = uuidv4()
  152. treeDataCurParentRow.children.splice(index, 0, data)
  153. treeDataCurParentRow.children.forEach((it, i, arr) => {
  154. it.sort = arr.length - i
  155. })
  156. computedScore(treeDataCurParentRow.nodeCode)
  157. })
  158. .catch(() => {});
  159. }
  160.  
  161. const del = (row) => {
  162. const treeDataCurRow = getTreeCurRow(treeData.value, row)
  163. const treeDataCurParentRow = getTreeCurRow(treeData.value, { nodeCode: row.parentNodeCode })
  164. proxy.$modal
  165. .confirm("是否确认删除?")
  166. .then(async () => {
  167. const index = treeDataCurParentRow.children.indexOf(treeDataCurRow)
  168. treeDataCurParentRow.children.splice(index, 1)
  169. treeDataCurParentRow.children.forEach((it, i, arr) => {
  170. it.sort = arr.length - i
  171. })
  172. computedScore(treeDataCurParentRow.nodeCode)
  173. })
  174. .catch(() => {});
  175. }
  176.  
  177. const getSortIsDisabled = (row, order) => {
  178. const treeDataCurRow = getTreeCurRow(treeData.value, row)
  179. const treeDataCurParentRow = getTreeCurRow(treeData.value, { nodeCode: row.parentNodeCode })
  180. const index = treeDataCurParentRow.children.indexOf(treeDataCurRow)
  181. if(order === 'ascending'){
  182. return index === 0
  183. } else {
  184. return index === treeDataCurParentRow.children.length - 1
  185. }
  186. }
  187.  
  188. const ascHandle = (row) => {
  189. const treeDataCurRow = getTreeCurRow(treeData.value, row)
  190. const treeDataCurParentRow = getTreeCurRow(treeData.value, { nodeCode: row.parentNodeCode })
  191. const index = treeDataCurParentRow.children.indexOf(treeDataCurRow)
  192. const preData = treeDataCurParentRow.children[index - 1]
  193. const temp = treeDataCurRow.sort
  194. treeDataCurRow.sort = preData.sort
  195. preData.sort = temp
  196. treeDataCurParentRow.children.sort((a, b) => {
  197. return b.sort - a.sort
  198. })
  199. }
  200.  
  201. const descHandle = (row) => {
  202. const treeDataCurRow = getTreeCurRow(treeData.value, row)
  203. const treeDataCurParentRow = getTreeCurRow(treeData.value, { nodeCode: row.parentNodeCode })
  204. const index = treeDataCurParentRow.children.indexOf(treeDataCurRow)
  205. const nextData = treeDataCurParentRow.children[index + 1]
  206. const temp = treeDataCurRow.sort
  207. treeDataCurRow.sort = nextData.sort
  208. nextData.sort = temp
  209. treeDataCurParentRow.children.sort((a, b) => {
  210. return b.sort - a.sort
  211. })
  212. }
  213.  
  214. const search = () => {
  215. newName.value = '@#$%^&*'
  216. rowExpansionInfo = []
  217. nextTick(() => {
  218. newName.value = form.name
  219. rowExpansionInfo = []
  220. nextTick(() => {
  221. if(!rowExpansionInfo.length) return
  222. for (const item of rowExpansionInfo) {
  223. rowExpansion(item.row)
  224. }
  225. const minIndex = Math.min(...rowExpansionInfo.map(it => it.index))
  226. tableScrollToRow(minIndex)
  227. })
  228. })
  229. }
  230.  
  231. const resetQuery = () => {
  232. proxy.$refs.ruleForm.resetFields()
  233. newName.value = ''
  234. rowExpansionInfo = []
  235. }
  236.  
  237. const formatSearch = (row, index, str) => {
  238. return str.replace(new RegExp(newName.value, 'ig'), function($1) {
  239. if($1 && !rowExpansionInfo.find(it => it.nodeCode === row.nodeCode)) rowExpansionInfo.push({ index, row })
  240. return `<span class="light">${$1}</span>`;
  241. });
  242. }
  243.  
  244. const rowExpansion = (row) => {
  245. const treeDataCurParentRow = getTreeCurRow(treeData.value, { nodeCode: row.parentNodeCode })
  246. proxy.$refs.tableRef.toggleRowExpansion(treeDataCurParentRow, true)
  247. if(treeDataCurParentRow.parentNodeCode === '0') return
  248. rowExpansion(treeDataCurParentRow)
  249. }
  250.  
  251. const tableScrollToRow = (rowIndex) => {
  252. const scrollbarWrapper = proxy.$refs.tableRef.$el.querySelector('.el-table__body-wrapper .el-scrollbar .el-scrollbar__wrap')
  253. const theTableRows = scrollbarWrapper.querySelectorAll('.el-table__body tbody .el-table__row')
  254. let scrollTop = 0
  255. for (let i = 0; i < theTableRows.length; i++) {
  256. if (i === rowIndex) {
  257. break
  258. }
  259. scrollTop += theTableRows[i].offsetHeight
  260. }
  261. if(scrollTop > scrollbarWrapper.offsetHeight) {
  262. scrollbarWrapper.scrollTop = scrollTop
  263. }
  264. }
  265.  
  266. const computedScore = (parentNodeCode) => {
  267. if(parentNodeCode === '0') return
  268. const treeDataCurParentRow = getTreeCurRow(treeData.value, { nodeCode: parentNodeCode })
  269. if(treeDataCurParentRow.level === '0') { //root
  270. const evaluationTypeScore = treeDataCurParentRow.children.reduce((pre, item) => {
  271. return pre += item.evaluationContentScore * 1
  272. }, 0)
  273. treeDataCurParentRow.evaluationTypeScore = evaluationTypeScore > 0 ? evaluationTypeScore + '' : ''
  274. } else if(treeDataCurParentRow.level === '1') {
  275. const evaluationContentScore = treeDataCurParentRow.children.reduce((pre, item) => {
  276. return pre += item.evaluationRuleScore * 1
  277. }, 0)
  278. treeDataCurParentRow.evaluationContentScore = evaluationContentScore > 0 ? evaluationContentScore + '' : ''
  279.  
  280. const rootRow = getTreeCurRow(treeData.value, { nodeCode: treeDataCurParentRow.parentNodeCode })
  281. const evaluationTypeScore = rootRow.children.reduce((pre, item) => {
  282. return pre += item.evaluationContentScore * 1
  283. }, 0)
  284. rootRow.evaluationTypeScore = evaluationTypeScore > 0 ? evaluationTypeScore + '' : ''
  285. }
  286. }
  287.  
  288. const forTableData = (data,isExpand) => {
  289. data.forEach(item =>{
  290. proxy.$refs.tableRef.toggleRowExpansion(item,isExpand);
  291. if(item.children){
  292. forTableData(item.children,isExpand)
  293. }
  294. })
  295. }
  296. // 展开
  297. const expand = () => {
  298. forTableData(treeData.value,true)
  299. }
  300. // 校验提交数据
  301. const checkData = (data) => {
  302. // const list1 = []
  303. let flag = false
  304. for (const item1 of data) {
  305. const children1 = item1.children || []
  306. if(!children1.length) return flag = true
  307. // const list2 = []
  308. for (const item2 of children1) {
  309. const children2 = item2.children || []
  310. if(!children2.length) return flag = true
  311. // if(children2.length) {
  312. // list2.push(item2)
  313. // }
  314. }
  315. // item1.children = list2
  316. // if(list2.length){
  317. // list1.push(item1)
  318. // }
  319. }
  320. return flag
  321. }
  322. const setMergeData = (data) => {
  323. let length1 = 0
  324. let length2 = 0
  325. for (const item1 of data) {
  326. const children1 = item1.children || []
  327. const sum = getSum(children1)
  328. length1 += sum
  329. for (const item2 of children1) {
  330. // item2.realScore= item2.scoreMap.realScore2
  331. const children2 = item2.children || []
  332. length2 += children2.length
  333. for (const item3 of children2) {
  334. item3.length1 = length1
  335. item3.sum1 = sum
  336. item3.length2 = length2
  337. item3.sum2 = children2.length
  338. }
  339. }
  340. }
  341. // data.map((k)=>{
  342. // if(k.itemLevel=='1') k.firstRealScore=k.realScore
  343. // })
  344. return data
  345. }
  346.  
  347. const getSum = (data) => {
  348. let sum = 0
  349. for (const item of data) {
  350. const children = item.children || []
  351. for (const it of children) {
  352. sum++
  353. if(it.children) {
  354. getSum(it.children)
  355. }
  356. }
  357. }
  358. return sum
  359. }
  360. const tableData = computed(() => {
  361. const list = getFlatData(JSON.parse(JSON.stringify(treeData.value)))
  362. return list.filter(item => item.itemLevel=='3')
  363. })
  364. const getFlatData = (data) => {
  365. let list = []
  366. for (const item of data) {
  367. const children = item.children
  368. delete item.children
  369. list.push(item)
  370. if(children){
  371. list = list.concat(getFlatData(children))
  372. }
  373. }
  374. return list
  375. }
  376. const handleData = (data) =>{
  377. data.value.forEach((v, k,m) => {
  378. v[`itemContent${k}`]=v.itemContent;
  379. if(v.children){
  380. handleData(v.children)
  381. }
  382. })
  383. return data.value
  384.  
  385. }
  386. return {
  387. handleData,
  388. form,
  389. treeData,
  390. visible,
  391. curRow,
  392. operateInfo,
  393. getTreeCurRow,
  394. search,
  395. resetQuery,
  396. goBack,
  397. close,
  398. statusChange,
  399. setStatusDisabled,
  400. canOperateChange,
  401. setCanOperateDisabled,
  402. getSortIsDisabled,
  403. copy,
  404. del,
  405. ascHandle,
  406. descHandle,
  407. formatSearch,
  408. computedScore,
  409. expand,
  410. checkData,
  411. setMergeData,
  412. tableData
  413. }
  414. }