增加
1 2 3 4 5 6 7 8 9 10 11 12 |
function addTreeNode(root, id, obj) { if (root.id == id) { if (root.children == null) root.children = []; root.children.push(obj); return true; } else if (root.children && root.children.length > 0) { for (var i = root.children.length - 1; i >= 0; i--) { addTreeNode(root.children[i], id, obj); } } } |
修改
1 2 3 4 5 6 7 8 9 10 11 12 |
function editTreeNode(root, obj) { if (root.id == obj.id) { $.each(obj, function (key, val) { root[key] = val; }) return true; } else if (root.children && root.children.length > 0) { for (var i = root.children.length - 1; i >= 0; i--) { editTreeNode(root.children[i], obj); } } } |
删除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function deleteTreeNode(root, id) { if (root.id == id) { for (var key in root) { delete root[key]; } return true; } else if (root.children && root.children.length > 0) { for (var i = root.children.length - 1; i >= 0; i--) { var isClean = deleteTreeNode(root.children[i], id); if (isClean === true) { root.children.splice(i, 1); } } } } |