Skip to content

Commit

Permalink
[fix bug] __treeId__ index error
Browse files Browse the repository at this point in the history
  • Loading branch information
taoqili committed Nov 21, 2018
1 parent 4fa2faa commit 0df7e1a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
44 changes: 20 additions & 24 deletions src/innerMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,22 @@ function syncRecord(obj, cb) {
}
});
}

/**
* update sub row's treeId
* @param obj row object
* @param prefixStr
* update row's treeId
* @param rows
* @param prefix
*/
function updateSubTreeId(obj, prefixStr) {
if (obj.data) {
obj.data.forEach(item => {
updateSubTreeId(item, prefixStr)
})
} else {
obj.__treeId__ = prefixStr + obj.__treeId__.substr(prefixStr.length)
function updateTreeId(rows, prefix) {
if (!rows || !rows.length) {
return;
}
for (let i = 0; i < rows.length; i++) {
let row = rows[i];
let treeId = prefix ? `${prefix}-${i}` : `${i}`;
row.__treeId__ = treeId;
if (row.data) {
updateTreeId(row.data, treeId);
}
}
}

Expand All @@ -170,7 +173,7 @@ function removeRecords(obj, cb) {
const me = this;
const content = deepcopy(me.state.data);
const data = content.data || content.datas;
const treeIdArr = obj.__treeId__.split('-');
const treeIdArr = obj.__treeId__.split('-').map(item => parseInt(item, 10));
let rows = data;
for (let i = 0; i < treeIdArr.length - 1; i++) {
const rowIndex = treeIdArr[i];
Expand All @@ -180,24 +183,16 @@ function removeRecords(obj, cb) {
if (Object.prototype.toString.call(objAux) !== '[object Array]') {
objAux = [objAux];
}
let hasRemove = false;
objAux.forEach((item) => {
for (let i = 0; i < rows.length; i++) {
const element = rows[i];
if (hasRemove) {
let tmpTreeIdArr = [...treeIdArr];
let newTreeId = tmpTreeIdArr.splice(0, tmpTreeIdArr.length - 1).concat([i]).join('-');
element.__treeId__ = newTreeId;
updateSubTreeId(element, newTreeId)
} else {
if (element.jsxid === item.jsxid) {
rows.splice(i, 1);
i--;
hasRemove = true;
}
if (element.jsxid === item.jsxid) {
rows.splice(i, 1);
break;
}
}
});
updateTreeId(content.data);
me.data = content;
this.setState({
data: content,
Expand All @@ -215,5 +210,6 @@ export default {
updateRecord,
syncRecord,
removeRecords,
updateTreeId
};

6 changes: 2 additions & 4 deletions src/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ function moveRowUp(rowData, cb) {
const lastIndex = treeIdArr.pop();
if (lastIndex > 0) {
rows.splice(lastIndex, 1);
// change treeId before setState;
rows[lastIndex - 1].__treeId__ = treeIdArr.concat([lastIndex]).join('-');
rows.splice(lastIndex - 1, 0, { ...rowData, __treeId__: treeIdArr.concat([lastIndex - 1]).join('-') });
this.data = content;
this.setState({
Expand All @@ -168,6 +166,7 @@ function moveRowUp(rowData, cb) {
}
});
}
this.updateTreeId(data)
}
}

Expand All @@ -183,8 +182,6 @@ function moveRowDown(rowData, cb) {
}
const lastIndex = treeIdArr.pop();
if (lastIndex < rows.length - 1) {
// change treeId before setState;
rows[lastIndex + 1].__treeId__ = treeIdArr.concat([lastIndex]).join('-');
rows.splice(lastIndex, 1);
rows.splice(lastIndex + 1, 0, { ...rowData, __treeId__: treeIdArr.concat([lastIndex + 1]).join('-') });
this.data = content;
Expand All @@ -196,6 +193,7 @@ function moveRowDown(rowData, cb) {
}
});
}
this.updateTreeId(data)
}
}

Expand Down

0 comments on commit 0df7e1a

Please sign in to comment.