fix tree table level

This commit is contained in:
梁灏 2020-01-12 09:28:49 +08:00
parent 85a1bc8af0
commit 64298a9b0e
3 changed files with 21 additions and 9 deletions

View file

@ -107,8 +107,8 @@
date: '2016-10-03', date: '2016-10-03',
_disabled: true, _disabled: true,
level: 0, level: 0,
// _loading: false, _loading: false,
// children: [] children: []
}, },
{ {
id: '101', id: '101',
@ -193,7 +193,7 @@
date: '2016-10-02', date: '2016-10-02',
_disabled: false, _disabled: false,
level: 0, level: 0,
_showChildren: true, _showChildren: false,
children: [ children: [
{ {
id: '10200', id: '10200',

View file

@ -151,8 +151,10 @@
const showChildren = row._isShowChildren; const showChildren = row._isShowChildren;
if (row._rowKey === rowKey) { if (row._rowKey === rowKey) {
status = status && showChildren; status = status && showChildren;
break;
} else if (row.children && row.children.length) { } else if (row.children && row.children.length) {
status = this.getTrStatus(rowKey, row, status && showChildren); status = this.getTrStatus(rowKey, row, status && showChildren);
return status;
} }
} }
return status; return status;
@ -173,33 +175,42 @@
}, },
getLevel (rowKey) { getLevel (rowKey) {
let level; let level;
let child;
for (let i = 0; i < this.data.length; i++) { for (let i = 0; i < this.data.length; i++) {
const row = this.data[i]; const row = this.data[i];
if (row[this.rowKey] === rowKey) { if (row[this.rowKey] === rowKey) {
level = 0; level = 0;
break; break;
} else if (row.children && row.children.length) { } else if (row.children && row.children.length) {
level = this.getChildLevel(row, rowKey, 1); child = this.getChildLevel(row, rowKey, 1);
if (child[0] && child[0][this.rowKey] === rowKey) {
return child[1];
}
} }
} }
return level; return level;
}, },
getChildLevel (data, rowKey, level) { getChildLevel (data, rowKey, level) {
let newLevel; let newLevel;
let childData;
if (data.children && data.children.length) { if (data.children && data.children.length) {
for (let i = 0; i < data.children.length; i++) { for (let i = 0; i < data.children.length; i++) {
const row = data.children[i]; const row = data.children[i];
if (row[this.rowKey] === rowKey) { if (row[this.rowKey] === rowKey) {
childData = row;
newLevel = level; newLevel = level;
break; break;
} else if (row.children && row.children.length) { } else if (row.children && row.children.length) {
newLevel = this.getChildLevel(row, rowKey, level + 1); const child = this.getChildLevel(row, rowKey, level + 1);
if (child[0] && child[0][this.rowKey] === rowKey) {
return child;
}
} }
} }
} }
return newLevel; return [childData, newLevel];
}, },
getChildNode (h, data, nodes, level = 1) { getChildNode (h, data, nodes) {
if (data.children && data.children.length) { if (data.children && data.children.length) {
data.children.forEach((row, index) => { data.children.forEach((row, index) => {
let $tds = []; let $tds = [];
@ -255,8 +266,7 @@
nodes.push($tableTr); nodes.push($tableTr);
if (row.children && row.children.length) { if (row.children && row.children.length) {
level++; this.getChildNode(h, row, nodes);
this.getChildNode(h, row, nodes, level);
} }
}); });
return nodes; return nodes;

View file

@ -816,6 +816,7 @@
break; break;
} else if (thisData.children && thisData.children.length) { } else if (thisData.children && thisData.children.length) {
data = this.getChildrenByRowKey(rowKey, thisData); data = this.getChildrenByRowKey(rowKey, thisData);
if (data && data._rowKey === rowKey) return data;
} }
} }
return data; return data;
@ -844,6 +845,7 @@
break; break;
} else if (thisData.children && thisData.children.length) { } else if (thisData.children && thisData.children.length) {
data = this.getChildrenDataByRowKey(rowKey, thisData); data = this.getChildrenDataByRowKey(rowKey, thisData);
return data;
} }
} }
return data; return data;