import { ref, computed } from 'vue' export default function useTable(proxy, opts, fields) { const treeData = ref([]) const loading = ref(false) const tableData = computed(() => { const list = getFlatData(JSON.parse(JSON.stringify(treeData.value))) return list.filter(item => item[fields]) }) const getFlatData = (data) => { let list = [] for (const item of data) { const children = item.children delete item.children list.push(item) if(children){ list = list.concat(getFlatData(children)) } } return list } 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 setMergeData = (data) => { let length1 = 0 let length2 = 0 for (const item1 of data) { const children1 = item1.children || [] const sum = getSum(children1) length1 += sum for (const item2 of children1) { const children2 = item2.children || [] length2 += children2.length for (const item3 of children2) { item3.length1 = length1 item3.sum1 = sum item3.length2 = length2 item3.sum2 = children2.length } } } } const getSum = (data) => { let sum = 0 for (const item of data) { const children = item.children || [] for (const it of children) { sum++ if(it.children) { getSum(it.children) } } } return sum } const handlePreview = (file) => { if(opts.type === 'view') return // window.open(file.url, '_blank') proxy.$modal .confirm(`下载此文件: ${file.name}?`) .then(() => { window.open(file.url) }) } const uploadSuccess = (file, row) => { const treeDataCurRow = getTreeCurRow(treeData.value, row) treeDataCurRow.fileSaveRequestList.push(file) } const removeFile = (file, row) => { proxy.$modal .confirm("是否确认删除?") .then(() => { const treeDataCurRow = getTreeCurRow(treeData.value, row) const index = treeDataCurRow.fileSaveRequestList.findIndex(it => it.url === file.url) treeDataCurRow.fileSaveRequestList.splice(index, 1) }) } return { treeData, loading, tableData, getTreeCurRow, setMergeData, getSum, handlePreview, uploadSuccess, removeFile } }