省市区三级,根据一定模版自动选择
<el-cascader v-else v-model="demoList[scope.$index].provinceRegex" collapse-tags show-checkbox filterable :options="cityOptions" :props="{ value: 'code', label: 'name', children: 'children', multiple: true }" style="width: 700px" placeholder="请选择配送区域" @change="(e) => handleChange(e, scope.$index)" /> let aa=[ { "code": 11, "name": "北京市", "children": [ { "code": 1101, "name": "北京市", "children": [ { "code": 110101, "name": "东城区", "children": [] }, { "code": 110102, "name": "西城区", "children": [] }, { "code": 110105, "name": "朝阳区", "children": [] }, { "code": 110106, "name": "丰台区", "children": [] }, { "code": 110107, "name": "石景山区", "children": [] }, { "code": 110108, "name": "海淀区", "children": [] }, { "code": 110109, "name": "门头沟区", "children": [] }, { "code": 110111, "name": "房山区", "children": [] }, { "code": 110112, "name": "通州区", "children": [] }, { "code": 110113, "name": "顺义区", "children": [] }, { "code": 110114, "name": "昌平区", "children": [] }, { "code": 110115, "name": "大兴区", "children": [] }, { "code": 110116, "name": "怀柔区", "children": [] }, { "code": 110117, "name": "平谷区", "children": [] }, { "code": 110118, "name": "密云区", "children": [] }, { "code": 110119, "name": "延庆区", "children": [] } ] } ] }] const findRegionPathsByNames=(data, names)=>{ const nameList = names.split(',').map(name => name.trim()); const result = new Set(); // 使用 Set 自动去重 // 递归遍历所有节点 function traverse(nodes, path = []) { for (const node of nodes) { const currentPath = [...path, node.code]; // 检查是否匹配当前节点 for (const name of nameList) { const nameParts = name.split('-').map(part => part.trim()); // 情况1:单层匹配(如 "北京市") if (nameParts.length === 1 && nameParts[0] === node.name) { // 如果是叶子节点,直接添加 if (node.children.length === 0) { result.add(JSON.stringify(currentPath)); } // 否则递归添加所有子节点 else { collectAllChildren(node.children, currentPath); } } // 情况2:多层匹配(如 "北京市-北京市" 或 "北京市-北京市-东城区") else if (nameParts.length > 1) { // 检查当前节点是否匹配第一层(如 "北京市") if (nameParts[0] === node.name) { // 递归检查子节点是否匹配剩余层级 checkChildren(node.children, nameParts.slice(1), currentPath); } } } // 继续向下遍历 if (node.children && node.children.length > 0) { traverse(node.children, currentPath); } } } // 检查子节点是否匹配剩余层级 function checkChildren(nodes, remainingParts, parentPath) { for (const node of nodes) { const currentPath = [...parentPath, node.code]; // 如果当前节点匹配下一层级 if (node.name === remainingParts[0]) { // 如果已经是最后一层(如 "东城区") if (remainingParts.length === 1) { // 如果是叶子节点,直接添加 if (node.children.length === 0) { result.add(JSON.stringify(currentPath)); } // 否则递归添加所有子节点 else { collectAllChildren(node.children, currentPath); } } // 否则继续检查下一层 else if (node.children && node.children.length > 0) { checkChildren(node.children, remainingParts.slice(1), currentPath); } } } } // 递归收集某个节点的所有子节点路径 function collectAllChildren(nodes, parentPath) { for (const node of nodes) { const currentPath = [...parentPath, node.code]; // 如果是叶子节点,直接添加 if (node.children.length === 0) { result.add(JSON.stringify(currentPath)); } // 否则继续递归子节点 else { collectAllChildren(node.children, currentPath); } } } traverse(data); // 将 Set 转回数组 return Array.from(result).map(path => JSON.parse(path)); } findRegionPathsByNames(aa,"江苏省,江西省-上饶市,北京市-北京市-东城区")
您还未登录, 登录 后可进行评论
发表
还没有评论哦,来抢个沙发吧!