找你好多文章,没用合适的 直接上代码
<van-field
v-model="fieldValue"
is-link
readonly
label="地区"
placeholder="请选择所在地区"
@click="show3 = true"
></van-field>
<van-popup v-model="show3" round position="bottom">
<van-cascader
v-model="cascaderValue"
title="请选择所在地区"
:options="options"
@close="show3 = false"
@change="onChange"
@finish="onFinish"
></van-cascader>
</van-popup>
loadNextLevelData(label) {
let eiInfo = new EiInfo();
eiInfo.set("inqu_status-0-node", label); // 使用传入的 label 作为请求参数
return new Promise((resolve, reject) => {
EiCommunicator.send("YJGZ0100", "query", eiInfo, {
onSuccess: (ei) => {
if (ei.status !== -1) {
let res = ei.getBlock(label).getMappedRows(); // 提取数据
console.log(res, '11111111111111111111111')
let children = res.map(item => ({
text: item.text,
value: item.label,
children: item.leaf === '1' ? null : [] // 如果是最后一级节点,children 设置为 null
}));
resolve(children); // 返回数据
} else {
reject(new Error("Failed to retrieve data.")); // 处理错误
}
},
onFailure: (error) => {
reject(error); // 处理请求失败
}
});
});
},
onChange({value}) {
this.loadNextLevelData(value)
.then((children) => {
console.log("Fetched children:", children); // 调试输出
if (children && children.length > 0) {
let targetOption = this.findOptionByLabel(this.options, value); // 找到对应选项
if (targetOption) {
targetOption.children = children; // 更新选项中的子级数据
}
}
})
.catch((error) => {
console.error("Error fetching children:", error); // 捕捉错误
});
},
findOptionByLabel(options, label) {
// 递归查找选项,找到匹配的标签
for (let option of options) {
if (option.value === label) {
return option; // 找到匹配项
}
if (option.children && option.children.length > 0) {
let found = this.findOptionByLabel(option.children, label); // 递归查找子选项
if (found) {
return found; // 返回找到的项
}
}
}
return null; // 未找到
},