import { ref, reactive, nextTick } from 'vue' export default function useTable(proxy, uuidv4) { const router = useRouter(); const treeData = ref([]) const visible = ref(false) const curRow = ref({}) const form = reactive({ name: '' }) const newName = ref('') const operateInfo = reactive({ width: '0%', level: '', type: '', text: '' }) let rowExpansionInfo = [] const getTreeDataCurRow = (data, row, treeDataCurRow) => { if(!row?.nodeCode || row.nodeCode === '0') { treeDataCurRow.value = { children: data } return } for (const item of data) { if(row.nodeCode === item.nodeCode){ treeDataCurRow.value = item break } if(item.children){ getTreeDataCurRow(item.children, row, treeDataCurRow) } } } const getTreeCurRow = (data, row) => { let treeDataCurRow = { value: {} } getTreeDataCurRow(data, row, treeDataCurRow) return treeDataCurRow.value } const goBack = (route, path) => { proxy.$tab.closePage(route) router.push(path) } const close = () => { curRow.value = {} operateInfo.level = '' operateInfo.type = '' operateInfo.text = '' restForm() visible.value = false } const restForm = () => { proxy.$refs.dialogFormRef.resetFields() } const statusChange = (row, val) => { const treeDataCurRow = getTreeCurRow(treeData.value, row) setTreeDataStatus(treeDataCurRow, val) } const setTreeDataStatus = (row, status) => { row.status = status if(status === '0'){ row.enableTime = '' row.stopTime = proxy.moment().format("YYYY-MM-DD HH:mm:ss") } else if(status === '1'){ row.enableTime = proxy.moment().format("YYYY-MM-DD HH:mm:ss") row.stopTime = '' } if(row.children){ for (const item of row.children) { item.statusDisabled = status === '0' if(status === '0') { setTreeDataStatus(item, '0') } } } } const setStatusDisabled = (data) => { for (const item1 of data) { const children1 = item1.children || [] item1.statusDisabled = false for (const item2 of children1) { const children2 = item2.children || [] if(item1.status === '0' && item2.status === '0'){ item2.statusDisabled = true } else { item2.statusDisabled = false } for (const item3 of children2) { if(item2.status === '0' && item3.status === '0'){ item3.statusDisabled = true } else { item3.statusDisabled = false } } } } } const canOperateChange = (row, val) => { const treeDataCurRow = getTreeCurRow(treeData.value, row) setTreeDataCanOperate(treeDataCurRow, val) } const setTreeDataCanOperate = (row, canOperate) => { row.canOperate = canOperate if(row.children){ for (const item of row.children) { item.canOperateDisabled = canOperate === 0 if(canOperate === 0) { setTreeDataCanOperate(item, 0) } } } } const setCanOperateDisabled = (data) => { for (const item1 of data) { const children1 = item1.children || [] item1.canOperateDisabled = false for (const item2 of children1) { const children2 = item2.children || [] if(item1.canOperate === 0 && item2.canOperate === 0){ item2.canOperateDisabled = true } else { item2.canOperateDisabled = false } for (const item3 of children2) { if(item2.canOperate === 0 && item3.canOperate === 0){ item3.canOperateDisabled = true } else { item3.canOperateDisabled = false } } } } } const copy = (row) => { const treeDataCurRow = getTreeCurRow(treeData.value, row) const treeDataCurParentRow = getTreeCurRow(treeData.value, { nodeCode: row.parentNodeCode }) proxy.$modal .confirm("是否确认复制?") .then(async () => { const index = treeDataCurParentRow.children.indexOf(treeDataCurRow) const data = JSON.parse(JSON.stringify(row)) data.nodeCode = uuidv4() treeDataCurParentRow.children.splice(index, 0, data) treeDataCurParentRow.children.forEach((it, i, arr) => { it.sort = arr.length - i }) computedScore(treeDataCurParentRow.nodeCode) }) .catch(() => {}); } const del = (row) => { const treeDataCurRow = getTreeCurRow(treeData.value, row) const treeDataCurParentRow = getTreeCurRow(treeData.value, { nodeCode: row.parentNodeCode }) proxy.$modal .confirm("是否确认删除?") .then(async () => { const index = treeDataCurParentRow.children.indexOf(treeDataCurRow) treeDataCurParentRow.children.splice(index, 1) treeDataCurParentRow.children.forEach((it, i, arr) => { it.sort = arr.length - i }) computedScore(treeDataCurParentRow.nodeCode) }) .catch(() => {}); } const getSortIsDisabled = (row, order) => { const treeDataCurRow = getTreeCurRow(treeData.value, row) const treeDataCurParentRow = getTreeCurRow(treeData.value, { nodeCode: row.parentNodeCode }) const index = treeDataCurParentRow.children.indexOf(treeDataCurRow) if(order === 'ascending'){ return index === 0 } else { return index === treeDataCurParentRow.children.length - 1 } } const ascHandle = (row) => { const treeDataCurRow = getTreeCurRow(treeData.value, row) const treeDataCurParentRow = getTreeCurRow(treeData.value, { nodeCode: row.parentNodeCode }) const index = treeDataCurParentRow.children.indexOf(treeDataCurRow) const preData = treeDataCurParentRow.children[index - 1] const temp = treeDataCurRow.sort treeDataCurRow.sort = preData.sort preData.sort = temp treeDataCurParentRow.children.sort((a, b) => { return b.sort - a.sort }) } const descHandle = (row) => { const treeDataCurRow = getTreeCurRow(treeData.value, row) const treeDataCurParentRow = getTreeCurRow(treeData.value, { nodeCode: row.parentNodeCode }) const index = treeDataCurParentRow.children.indexOf(treeDataCurRow) const nextData = treeDataCurParentRow.children[index + 1] const temp = treeDataCurRow.sort treeDataCurRow.sort = nextData.sort nextData.sort = temp treeDataCurParentRow.children.sort((a, b) => { return b.sort - a.sort }) } const search = () => { newName.value = '@#$%^&*' rowExpansionInfo = [] nextTick(() => { newName.value = form.name rowExpansionInfo = [] nextTick(() => { if(!rowExpansionInfo.length) return for (const item of rowExpansionInfo) { rowExpansion(item.row) } const minIndex = Math.min(...rowExpansionInfo.map(it => it.index)) tableScrollToRow(minIndex) }) }) } const resetQuery = () => { proxy.$refs.ruleForm.resetFields() newName.value = '' rowExpansionInfo = [] } const formatSearch = (row, index, str) => { return str.replace(new RegExp(newName.value, 'ig'), function($1) { if($1 && !rowExpansionInfo.find(it => it.nodeCode === row.nodeCode)) rowExpansionInfo.push({ index, row }) return `<span class="light">${$1}</span>`; }); } const rowExpansion = (row) => { const treeDataCurParentRow = getTreeCurRow(treeData.value, { nodeCode: row.parentNodeCode }) proxy.$refs.tableRef.toggleRowExpansion(treeDataCurParentRow, true) if(treeDataCurParentRow.parentNodeCode === '0') return rowExpansion(treeDataCurParentRow) } const tableScrollToRow = (rowIndex) => { const scrollbarWrapper = proxy.$refs.tableRef.$el.querySelector('.el-table__body-wrapper .el-scrollbar .el-scrollbar__wrap') const theTableRows = scrollbarWrapper.querySelectorAll('.el-table__body tbody .el-table__row') let scrollTop = 0 for (let i = 0; i < theTableRows.length; i++) { if (i === rowIndex) { break } scrollTop += theTableRows[i].offsetHeight } if(scrollTop > scrollbarWrapper.offsetHeight) { scrollbarWrapper.scrollTop = scrollTop } } const computedScore = (parentNodeCode) => { if(parentNodeCode === '0') return const treeDataCurParentRow = getTreeCurRow(treeData.value, { nodeCode: parentNodeCode }) if(treeDataCurParentRow.level === '0') { //root const evaluationTypeScore = treeDataCurParentRow.children.reduce((pre, item) => { return pre += item.evaluationContentScore * 1 }, 0) treeDataCurParentRow.evaluationTypeScore = evaluationTypeScore > 0 ? evaluationTypeScore + '' : '' } else if(treeDataCurParentRow.level === '1') { const evaluationContentScore = treeDataCurParentRow.children.reduce((pre, item) => { return pre += item.evaluationRuleScore * 1 }, 0) treeDataCurParentRow.evaluationContentScore = evaluationContentScore > 0 ? evaluationContentScore + '' : '' const rootRow = getTreeCurRow(treeData.value, { nodeCode: treeDataCurParentRow.parentNodeCode }) const evaluationTypeScore = rootRow.children.reduce((pre, item) => { return pre += item.evaluationContentScore * 1 }, 0) rootRow.evaluationTypeScore = evaluationTypeScore > 0 ? evaluationTypeScore + '' : '' } } const forTableData = (data,isExpand) => { data.forEach(item =>{ proxy.$refs.tableRef.toggleRowExpansion(item,isExpand); if(item.children){ forTableData(item.children,isExpand) } }) } // 展开 const expand = () => { forTableData(treeData.value,true) } // 校验提交数据 const checkData = (data) => { // const list1 = [] let flag = false for (const item1 of data) { const children1 = item1.children || [] if(!children1.length) return flag = true // const list2 = [] for (const item2 of children1) { const children2 = item2.children || [] if(!children2.length) return flag = true // if(children2.length) { // list2.push(item2) // } } // item1.children = list2 // if(list2.length){ // list1.push(item1) // } } return flag } return { form, treeData, visible, curRow, operateInfo, getTreeCurRow, search, resetQuery, goBack, close, statusChange, setStatusDisabled, canOperateChange, setCanOperateDisabled, getSortIsDisabled, copy, del, ascHandle, descHandle, formatSearch, computedScore, expand, checkData } }