我在Extjs中使用Treepanel创建了一个树。以下是用于使用拖放遍历节点的代码片段
beforenodedrop:function(dropEvent){
try {
targetNode = dropEvent.target;
sourceNode = dropEvent.dropNode;
sourceNode = getDeepAllChildNodes(sourceNode);
treeTarget = sourceNode.getOwnerTree().id;
if (treeTarget == 'tree' && sourceNode.attributes.lvl > 0){
changeAlignTrees = true;
if (sourceNode.childNodes != null){
var targetFather = {};
var node = sourceNode;
if(targetNode.id==-2){
targetFather = targetNode;
}else{
targetFather = targetNode.parentNode;
}
while(sourceNode.hasChildNodes() == true){
node = node.firstChild;
if (node.hasChildNodes() == false){
node = node.parentNode;
targetFather.appendChild(node.firstChild);
if(node.hasChildNodes() == false){
node = node.parentNode;
}
}
}
}
if(targetNode.id==-2){
dropEvent.point = 'append';
}else{
dropEvent.point = 'below';
targetNode.leaf='true';
}
Ext.Ajax.request({
url: "testURL",
params: {
option: 'alUnit',
unitNum: sourceNode.id,
parentUnitNum: '-1'
},
scriptTag: true,
success: function(response, options){
}
});
}else{
dropEvent.cancel=true;
}
}catch (e) {
logException("Exception: " + e.description);
}
}
这是异步树节点,如果我不扩展节点,则以下行node = node.firstChild;
返回null。我还有一个函数来获取所有子节点,以下是代码片段
getDeepAllChildNodes = function(node){
var allNodes = new Array();
if(!Ext.value(node,false)){
return [];
}
if(!node.hasChildNodes()){
return node;
}else{
allNodes.push(node);
node.eachChild(function(Mynode){allNodes = allNodes.concat(getDeepAllChildNodes(Mynode));});
}
return allNodes;
};
执行第!node.hasChildNodes()
行时出现以下错误"Object doesn't support property or method 'hasChildNodes'"
如何在不扩展的情况下实现遍历treepanel中的所有子节点。