1.如果直接处理二维数据
// 转换后的二维数组(多选级联回显)
convert(arr) {
var map1 = {};
while(arr.length) {
let current = arr.pop(); // 会影响原数组
// console.log('current.relationId',map1[current.relationId])
map1[current.relationId] = map1[current.relationId] || [];
if(current.tid){
map1[current.relationId].push(current.tid, current.pid, current.relationId);
}else{
map1[current.relationId].push(current.pid,current.relationId);
}
}
return Object.keys(map1).map(key => map1[key]);
},
2.三维数据(先处理数据再调用convert()方法处理)
//多选级联回显(三级数据处理)
// tid,1级数据;pid,2级数据;id,3级数据
convertPlus(p){
// console.log('ppp', p)
const r = []
const hash = {}
const len = p.length
for (let i = 0; i < len; i++) {
hash[p[i]['relationId']] = p[i] //所有以id为标记的数据
}
console.log('hashVPforPid',hash)
for (let j = 0; j < len; j++) {
const aVal = p[j]
const hashVP = hash[aVal['pid']] //取aVal以pid为标记的数据,如果在hash中存在
if(hashVP){
this.$set(aVal, 'tid', hashVP.pid)
}
r.push(aVal)
}
return r
},
3.处理后的数据格式
页面效果(处理后台返回数据,回显效果如下):