🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## util.js ``` /** * @description 根据文件夹列表和文件列表获取树状结构数据 * @param {Object} folderList 文件夹列表 * @param {Object} fileList 文件列表 */ export const setTreeData = (folderList, fileList) => { let treeData = [] treeData = folderList.filter(floderItem => { let floderId = floderItem.id let index = fileList.length while (--index >= 0) { if (floderId === fileList[index].pId) { let file = fileList[index] file.title = file.name if (floderItem.children) { floderItem.children.push(file) floderItem.children = setTreeData(floderItem.children, fileList) } else { floderItem.children = [file] } } } return floderItem }) return treeData } /** * @description 将文件夹列表拼接归类 * @param {Object} folderList 文件夹列表 */ export const transferFolderToTree = folderList => { if (!folderList.length) return [] const handle = id => { let arr = [] folderList.forEach(folder => { if (folder.pId === id) { const children = handle(folder.id) if (folder.children) folder.children = [].concat(folder.children, children) else folder.children = children folder.title = folder.name arr.push(folder) } }) return arr } return handle(0) } ``` ## *vue页面 ``` import { setTreeData, transferFolderToTree } from '@/libs/util' ``` ## js // 定义方法将所有文件归类到所属文件夹里 ``` this.treeData = transferFolderToTree(setTreeData(_folderList, _fileList)) ```