这样子获取到数据是,checked等于true的,获取不到他的父级,父级的父级
解决办法代码如下:
//需要有一个唯一ID
//======================================
//扩展remove方法
Array.prototype.remove = function (val) {
let index = this.indexOf(val);
if (index > -1) {
this.splice(index, 1);
}
};
//======================================
//获取整条数据链
function getParent(array, childs, ids) {
for (let i = 0; i < array.length; i++) {
let item = array[i];
if (Number(item.id) === Number(ids)) {
childs.push(item);
return childs;
}
if (item.children && item.children.length > 0) {
childs.push(item);
let rs = getParent(item.children, childs, ids);
if (rs) {
return rs;
}
else {
childs.remove(item);
}
}
}
return false;
} //获取所有选中节点
let params = this.$refs.tree.getCheckedNodes();
//所有数据
let allData = ['所有数据'];
//循环执行所有选中的节点链,放到arr1数组里
let arr1 = [];
for (let i = 0; i < params.length; i++) {
//单条数据链
let aData = getParent(allData, [], params[i].id);//方法入口在这里
for (let y = 0; y < aData.length; y++) {
//拆分成单个json数组放到arr1里
arr1.push(aData[y]);
}
} //arr1去重 es6的set方法
function dedupe(array) {
return Array.from(new Set(array));
} arr1 = dedupe(arr1);
这样就能获取完整的整条数据链