import { ref, computed } from 'vue' export default function useTable(proxy, 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 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) => { proxy.$modal .confirm(`下载此文件: ${file.name}?`) .then(() => { window.open(file.url) }) } return { treeData, loading, tableData, setMergeData, getSum, handlePreview } }