需求有了變動,之前只需要提交被選中的葉節(jié)點的id數(shù)組,那時候直接通過官方的api【getCheckedKeys】直接拿到?,F(xiàn)在要求也要將父節(jié)點的id也傳過去。
卡殼了,看了Issues,不少網(wǎng)友都有這種需求,沒看到靠譜的方法。
有個思路,先通過getCheckedKeys拿到子節(jié)點的id,然后去tree的data去一個個去遍歷,存在的給父節(jié)點id取出來,最后重組所有已選中的id進行提交,但如果遇到N多級的咋辦? 不會寫了。。。
tree綁定的數(shù)據(jù)是一個list,經(jīng)過下面的函數(shù)computed出來的,保存的時候,我從這個list里面去拿的parentId
function toTree(data, parent_id) {
var tree = [];
var temp;
for (var i = 0; i < data.length; i++) {
if (data[i].parentId == parent_id) {
var obj = data[i];
temp = toTree(data, data[i].id);
if (temp.length > 0) {
obj.children = temp;
}
tree.push(obj);
}
}
return tree;
}
我自己獲取是這么寫的
getPermissions() {
let _this=this;
let permissions=this.$refs.tree.getCheckedKeys();
let ids=[];
permissions.forEach(key=>{
getParentId(key)
})
function getParentId(id) {
let node=_this.list.find(item=>item.id==id)
if(node.parentId==0){
return
}else{
ids.push(node.parentId)
getParentId(node.parentId)
}
}
permissions=[...new Set([...permissions,...ids])]
return permissions.join(',')
},
今天遇見了同樣的問題 運用的也是改源碼的方法
官方給了兩個方法 getCheckedNodes 和 getSelectedNodes 沒有獲取父級的
所以就自己新增了個方法 getCheckedAll 獲取選中和父級的
getCheckedAll:function(){
return this.flatState.filter(function (e) {
if(e.node.indeterminate){
return e.node.indeterminate
}
return e.node.checked
}).map(function (e) {
return e.node
})
}
打印e會很容易的看見 官方有標識的 套用之前的getCheckedNodes 的代碼一下就能寫出來最后再
this.$refs.tree.getCheckedAll() 就能獲取了父級的對象了
官方v2.2.1版本已經(jīng)提供獲取半選中狀態(tài)節(jié)點對象。
具體實現(xiàn)思路:
// 1. 先合并選中/半選中節(jié)點Id,請求傳給后臺。
[].concat(this.$refs.menuListTree.getCheckedKeys(), [this.tempKey], this.$refs.menuListTree.getHalfCheckedKeys())
this.tempKey === -666666 // 臨時key, 用于解決tree半選中狀態(tài)項不能傳給后臺接口問題. # 待優(yōu)化
// 2. 給tree綁定數(shù)據(jù)時,通過tempKey移除之前添加的半選中狀態(tài)節(jié)點Id。
var idx = this.menuIdList.indexOf(this.tempKey)
if (idx !== -1) {
this.menuIdList.splice(idx, this.menuIdList.length - idx)
}
this.$refs.menuListTree.setCheckedKeys(this.menuIdList)
詳細實現(xiàn)代碼,可在https://github.com/daxiongYan...查閱。
我有一個思路,我也親測過,可以取得父節(jié)點id
一般返回來的子節(jié)點數(shù)據(jù)data中存有父id(parent_id),通過element 的getCheckedNodes方法可以獲得選中節(jié)點就可以獲得父id了。多簡單
treeNodes = this.$refs.tree.getCheckedNodes(true);
treeKeys = this.$refs.tree.getCheckedKeys();
/**
子節(jié)點id
*/
for(var i = 0; i < treeKeys.length - 1; i++) {
ids[i] = treeKeys[i];
}
/**
選中子節(jié)點的父節(jié)點id
*/
for(var i = 0; i < treeNodes.length; i++) {
ids.push(treeNodes[i].parent_id);
}
ids = JSON.stringify(ids);
改源碼不太好,所以我用了笨辦法,
思路就是拿到選中的code 后 ,遍歷 整棵樹, 然后把半選中的添加進去,編輯的時候去掉這些半選中的nodes
//同步樹插件的半選中狀態(tài)(參數(shù): 樹的data,選中的node列表,是添加還是刪除,選中節(jié)點的關鍵字key,如果是null 就取整個node)
export const handleUpdateCheckds = (tree, checkeds, isAdd = true, checkKey = 'no') => {
let findHalfCheckds = (item, checkeds, result = new Set()) => {
if (item.children) {
let node = [...item.children];
while (node.length) {
let data = node.shift();
if (!item.isRoot) {
if (isAdd && checkeds.includes(data[checkKey] || data) && !checkeds.includes(item[checkKey] || item)) {
result.add(item[checkKey] || item);
} else if (!isAdd && !checkeds.includes(data[checkKey] || data) && checkeds.includes(item[checkKey] || item)) {
result.add(item[checkKey] || item);
}
}
if (data.children && data.children.length > 0) {
node = node.concat(data.children);
}
findHalfCheckds(data, checkeds, result);
}
}
return result;
};
let result,
halfCheckds = [...findHalfCheckds({
isRoot: true,
children: tree
}, checkeds)];
if (isAdd) { //
result = [...new Set(checkeds.concat(halfCheckds))];
} else {
result = checkeds.filter(item => !halfCheckds.includes(item));
}
//console.log(halfCheckds);
return result;
};
北大青鳥APTECH成立于1999年。依托北京大學優(yōu)質雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學院和江蘇省首批服務外包人才培訓基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術與教育服務機構,發(fā)展為教育服務業(yè)的綜合性企業(yè)集團,成為集合面授教學培訓、網(wǎng)
達內教育集團成立于2002年,是一家由留學海歸創(chuàng)辦的高端職業(yè)教育培訓機構,是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責任公司從事總經(jīng)理職務負責iOS教學及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術, 熟練的跨平臺面向對象開發(fā)經(jīng)驗,技術功底深厚。 授課風格 授課風格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術顧問,美國Dachieve 系統(tǒng)架構師,美國AngelEngineers Inc. 系統(tǒng)架構師。