测试代码如下:
Ext.onReady(function() {
var store = Ext.create('Ext.data.TreeStore', {
root : {
text : "Root",
leaf : false,
expanded : true,
children : [{
text : "detention",
id : 'first',
leaf : true
}, {
text : "homework",
expanded : true,
children : [{
text : "book report",
leaf : true
}, {
text : "algebra",
leaf : true
}]
}, {
text : "buy lottery tickets",
leaf : true
}]
}
});
Ext.create('Ext.tree.Panel', {
title : 'Simple Tree',
dockedItems : [{
xtype : 'toolbar',
dock : 'top',
items : ['->', {
xtype : 'button',
text : '新增同级',
id : 'listaddSamebtn',
handler : function(thiz) {
var treePanel = thiz.up('treepanel');
var selNodes = treePanel.getSelectionModel().getSelection();
if (selNodes.length > 0) {
var selNode = selNodes[0];
if (selNode.get('parentId') == 'root') {
console.log('selNodes is root', selNodes);
var root = treePanel.getRootNode();
console.log('RRRRRRRRRRRRRRRR', root);
root.appendChild({
text : 'newNodeForRoot',
leaf : true
});
} else {
console.log('selNodes', selNodes);
selNode.appendChild({
text : 'newNode',
leaf : true
});
}
}
}
}, {
xtype : 'button',
text : '新增下级',
id : 'listaddNextbtn',
handler : function(thiz) {
var treePanel = thiz.up('treepanel');
var selNodes = treePanel.getSelectionModel().getSelection();
if (selNodes.length > 0) {
var selNode = selNodes[0];
selNode.data.leaf = false;
console.log('selNode', selNode);
selNode.set('children', [{
text : 'newNode',
leaf : true
}]);
selNode.appendChild({
text : 'newNode',
leaf : true
});
}
}
}]
}],
width : 600,
height : 550,
store : store,
rootVisible : true,
renderTo : Ext.getBody()
});
});
最后发现只有设置selNode.data.leaf = false;后才能正常显示添加的节点。