| | |
| | | if (selectedOptions.value !== topLevelItems) { |
| | | selectedOptions.value = topLevelItems; |
| | | } |
| | | |
| | | const topLevelItemsWithChildren = topLevelItems; // 包含顶级菜单项及其子菜单项的数组 |
| | | let selectedPath = []; |
| | | |
| | | console.log(topLevelItems); |
| | | // selectedOptions.value = topLevelItems; |
| | | const topLevelItemsWithChildren = topLevelItems; // 包含顶级菜单项及其子菜单项的数组 |
| | | let selectedPath = []; |
| | | // 循环遍历 topLevelItemsWithChildren 数组 |
| | | for (let i = 0; i < topLevelItemsWithChildren.length; i++) { |
| | | // 添加顶级菜单项的 id |
| | | selectedPath.push(topLevelItemsWithChildren[i].id); |
| | | |
| | | // 遍历该顶级菜单项的子菜单项 |
| | | if (topLevelItemsWithChildren[i].children && topLevelItemsWithChildren[i].children.length > 0) { |
| | | for (let j = 0; j < topLevelItemsWithChildren[i].children.length; j++) { |
| | | // 添加每个子菜单项的 id |
| | | selectedPath.push(topLevelItemsWithChildren[i].children[j].id); |
| | | } |
| | | } |
| | | // 添加顶级菜单项的 id |
| | | selectedPath.push(topLevelItemsWithChildren[i].id); |
| | | // 如果有子菜单项,添加第一个子菜单项的 id |
| | | if (topLevelItemsWithChildren[i].children && topLevelItemsWithChildren[i].children.length > 0) { |
| | | for (let j = 0; j < topLevelItemsWithChildren[i].children.length; j++) { |
| | | // 将每个子菜单项的 id 添加到 selectedPath 中 |
| | | selectedPath.push(topLevelItemsWithChildren[i].children[j].id); |
| | | } |
| | | } |
| | | } |
| | | |
| | | console.log(selectedPath); |
| | | console.log(selectedPath); |
| | | selectedOptions.value = selectedPath; |
| | | } |
| | | // function handleEdit(row) { |
| | | // name.value = row.name; |
| | | // adda.value = true; |
| | | // window.localStorage.setItem('ids', row.id); |
| | | // // 构建菜单树 |
| | | // const buildMenuTree = (menuList) => { |
| | | // const parentIdMap = {}; |
| | | // const menuItemsById = {}; |
| | | // menuList.forEach(item => { |
| | | // const parentId = item.parentId === 0 ? null : item.parentId; |
| | | // const menuItem = { |
| | | // id: item.id, |
| | | // parentId: parentId, |
| | | // menuName: item.menuName, |
| | | // children: [] |
| | | // }; |
| | | // menuItemsById[item.id] = menuItem; |
| | | |
| | | // if (!parentIdMap[parentId]) { |
| | | // parentIdMap[parentId] = []; |
| | | // } |
| | | // parentIdMap[parentId].push(menuItem); |
| | | // }); |
| | | // // 构建父子关系 |
| | | // for (const parentId in parentIdMap) { |
| | | // if (parentId !== 'null') { |
| | | // parentIdMap[parentId].forEach(child => { |
| | | // if (menuItemsById[parentId]) { |
| | | // menuItemsById[parentId].children.push(child); |
| | | // } |
| | | // }); |
| | | // } |
| | | // } |
| | | // return parentIdMap[null] || []; |
| | | // }; |
| | | // const options = buildMenuTree(row.menuList); |
| | | // const selectedIds = row.menuList.filter(item => item.parentId === 0).map(item => item.id); |
| | | // selectedOptions.value = selectedIds; |
| | | // console.log(options); // 输出整个菜单树 |
| | | // console.log(selectedIds); // 输出应默认选中的节点ID数组 |
| | | // } |
| | | // function handleEdit(row) { |
| | | // name.value = row.name; |
| | | // adda.value = true; |
| | | // window.localStorage.setItem('ids', row.id); |
| | | // // 初始化 parentIdMap 和 menuItemsById |
| | | // const parentIdMap = {}; |
| | | // const menuItemsById = {}; |
| | | // let topLevelItems = []; |
| | | |
| | | // // 构建树形结构 |
| | | // row.menuList.forEach(item => { |
| | | // const parentId = item.parentId === 0 ? null : item.parentId; |
| | | // const menuItem = { |
| | | // id: item.id, |
| | | // parentId: parentId, |
| | | // label: item.menuName, // 注意:el-cascader 使用 label 而不是 menuName |
| | | // value: item.id, // 通常 value 是唯一标识符 |
| | | // children: [] |
| | | // }; |
| | | // menuItemsById[item.id] = menuItem; |
| | | |
| | | // if (!parentIdMap[parentId]) { |
| | | // parentIdMap[parentId] = []; |
| | | // } |
| | | |
| | | // if (parentId === null) { |
| | | // topLevelItems.push(menuItem); |
| | | // } else { |
| | | // parentIdMap[parentId].push(menuItem); |
| | | // } |
| | | // }); |
| | | |
| | | // // 构建父子关系 |
| | | // for (const parentId in parentIdMap) { |
| | | // if (parentId !== 'null') { |
| | | // parentIdMap[parentId].forEach(child => { |
| | | // if (menuItemsById[parentId]) { |
| | | // menuItemsById[parentId].children.push(child); |
| | | // } |
| | | // }); |
| | | // } |
| | | // } |
| | | |
| | | // // 设置 options |
| | | // options.value = topLevelItems; |
| | | |
| | | // // 构建 selectedOptions |
| | | // let selected = []; |
| | | // // 假设你想选中所有节点,你可以遍历 row.menuList 并构建路径 |
| | | // row.menuList.forEach(item => { |
| | | // let path = []; |
| | | // let currentParentId = item.parentId; |
| | | // while (currentParentId !== null) { |
| | | // const parentItem = menuItemsById[currentParentId]; |
| | | // if (parentItem) { |
| | | // path.unshift(parentItem.value); // 从父节点到子节点 |
| | | // currentParentId = parentItem.parentId; |
| | | // } else { |
| | | // break; // 如果找不到父节点,则停止 |
| | | // } |
| | | // } |
| | | // // 添加当前节点的 ID 到路径末尾 |
| | | // path.push(item.id); |
| | | // // 如果路径不为空,则添加到 selectedOptions |
| | | // if (path.length > 0) { |
| | | // selected.push(path); |
| | | // } |
| | | // }); |
| | | |
| | | // // 更新 selectedOptions |
| | | // selectedOptions.value = selected; |
| | | // console.log(selected); |
| | | // console.log(topLevelItems); |
| | | // // 注意:确保 selectedOptions 和 options 是响应式的 |
| | | // } |
| | | // 编辑 |
| | | const getTableRowa = async () => { |
| | | let ids = window.localStorage.getItem('ids') |
| | |
| | | console.error(error); |
| | | } |
| | | }; |
| | | function closeDialog(row) { |
| | | add.value = false; |
| | | name.value = ''; |
| | | selectedOptions.value = ''; |
| | | } |
| | | function closeDialoga(row) { |
| | | adda.value = false; |
| | | name.value = ''; |
| | | selectedOptions.value = ''; |
| | | } |
| | | // 删除 |
| | | const opena = async(row) => { |
| | | try { |
| | |
| | | <el-button type="primary" @click="getTableRow"> |
| | | {{ $t('delivery.sure') }} |
| | | </el-button> |
| | | <el-button @click="add = false"> {{ $t('delivery.cancel') }}</el-button> |
| | | <el-button @click="closeDialog"> {{ $t('delivery.cancel') }}</el-button> |
| | | </div> |
| | | </template> |
| | | </el-dialog> |
| | |
| | | <el-button type="primary" @click="getTableRowa"> |
| | | {{ $t('delivery.sure') }} |
| | | </el-button> |
| | | <el-button @click="adda = false">{{ $t('delivery.cancel') }}</el-button> |
| | | <el-button @click="closeDialoga">{{ $t('delivery.cancel') }}</el-button> |
| | | </div> |
| | | </template> |
| | | </el-dialog> |