Ext树

var arry=[{
id:'ExtJs1',
text:'ExtJs1',
leaf:false,
children:[{
id:'a1',
text:'a1',
leaf:true
},{
id:'b1',
text:'b1',
leaf:true
}]
},{
id:'ExtJs2',
text:'ExtJs2',
leaf:false,
children:[{
id:'a2',
text:'a2',
leaf:true
},{
id:'b2',
text:'b2',
leaf:true
}]
}]

Ext.onReady(function(){
var tree=createTree();
var treeEditor=createTreeEditor(tree);
tree.on('contextmenu',showContextMenu);
tree.render(document.body);
// tree.expandAll();
});

//创建树
function createTree(){
var tree=new Ext.tree.TreePanel({
animate : true,
width :300,
height :300,
enableDD : false,
root : new Ext.tree.TreeNode({
id:'0',
text:'ExtJs',
leaf :false
})
});
createChildTreeNode(tree.root,arry);
return tree;
}
//为某节点创建子节点
function createChildTreeNode(node,childNodes){
for(var i=0;i<childNodes.length;i++){
var childNode=new Ext.tree.TreeNode({
id:childNodes[i].id,
text:childNodes[i].text
});
if(childNodes[i].children!=undefined){
createChildTreeNode(childNode,childNodes[i].children);
}
node.appendChild(childNode);
}
}
//为树创建treeEditor
function createTreeEditor(tree){
var treeEditor=new Ext.tree.TreeEditor(tree,{
allowBlank : false,

blankText : '请输入项目名称',

selectOnFocus : true,
listeners:{'complete':function(){

   }}

});
return treeEditor;
}
//创建菜单
function createContextMenu(node,event) {

var contextMenu;
//如果是根节点
if(node.root!=undefined){
contextMenu=new Ext.menu.Menu({
id:'rootMenu',
items:[{
text:'new',
scope:this,
handler:function(){

        }
      }]
    });
   return contextMenu;
 }
 //如果不是叶子节点
 else if(node.isLeaf()==false){
    contextMenu=new Ext.menu.Menu({
      id:'Menu',
      items:[{
        text:'new',
        scope:this,
        handler:function(){

        } 
      },'-',{
        text:'update',
        scope:this,
        handler:function(){

        }
      },'-',{
        text:'delete',
        scope:this,
        handler:function(){

        }
      }]
    });
   return contextMenu;
 }
 //如果是叶子节点
 else{
      contextMenu=new Ext.menu.Menu({
      id:'leafMenu',
      items:[{
        text:'update',
        scope:this,
        handler:function(){

        }
      },'-',{
        text:'delete',
        scope:this,
        handler:function(){

        }
      }]
    });
   return contextMenu;
 }

}
//显示菜单位置
function showContextMenu(node,event){
var contextMenu=createContextMenu(node,event);
contextMenu.showAt(event.getXY());
}

问题主要是右键菜单达不到预期效果
总是显示else if 那个
[b]问题补充:[/b]
你们说的方法还是不能达到想要的效果

在补充个问题下
怎么让可编辑的grid,编辑完的单元格如果值等于之前的,就没什么变化,如果值不等于之前的就显示出红色

[quote]你们说的方法还是不能达到想要的效果 [/quote]

你拿我的代码试了没有,我都帮你修改出来了,都测试了才放上来的.

至于你说的第二问题
你可以看一下 Ext.grid.EditorGridPanel的 afteredit事件,他里面有如下参数
[code="js"]
afteredit : ( Object e )
Fires after a cell is edited.
grid - This grid
record - The record being edited
field - The field name being edited
value - The value being set//这个是新的值
originalValue - The original value for the field, before the edit.//这个是原始值
row - The grid row index
column - The grid column index
Listeners will be called with the following arguments:
e : Object
An edit event (see above for description)[/code]

//你可以通过判断两个值是不是一样.一样就 return false; 不一样则 return true;

判断是否为根节点错误,应该用 if(node.parentNode==null){ 表示为根节点。所有上面的你只需要修改 if(node.root!=undefined)这个即可

帮你修改了一下,强烈不建议你用这种方式来写,你没有发现你每次右键都会创建一次菜单吗?效率何等的低,官方是怎么写的,你完全可以参考别人的
[code="js"]
function createChildTreeNode(node, childNodes) {
for (var i = 0; i < childNodes.length; i++) {
var childNode = new Ext.tree.TreeNode({
id : childNodes[i].id,
text : childNodes[i].text,
leaf : childNodes[i].leaf//主要加这个
});
if (childNodes[i].children != undefined) {
createChildTreeNode(childNode, childNodes[i].children);
}
node.appendChild(childNode);
}
}
[/code]

修改后的所有代码

[code="js"]
var arry = [{
id : 'ExtJs1',
text : 'ExtJs1',
leaf : false,
children : [{
id : 'a1',
text : 'a1',
leaf : true
}, {
id : 'b1',
text : 'b1',
leaf : true
}]
}, {
id : 'ExtJs2',
text : 'ExtJs2',
leaf : false,
children : [{
id : 'a2',
text : 'a2',
leaf : true
}, {
id : 'b2',
text : 'b2',
leaf : true
}]
}]

Ext.onReady(function() {
Ext.BLANK_IMAGE_URL = 'ext2.2.1/resources/images/default/s.gif';
var tree = createTree();
var treeEditor = createTreeEditor(tree);
tree.on('contextmenu', showContextMenu);
tree.render(document.body);
});

function createTree() {
var tree = new Ext.tree.TreePanel({
animate : true,
width : 300,
height : 300,
enableDD : false,
root : new Ext.tree.TreeNode({ // 你在这里已经定义了root所以肯定每次菜单都是那个
id : '0',
text : 'ExtJs',
leaf : false
})
});
createChildTreeNode(tree.root, arry);
return tree;
}
function createChildTreeNode(node, childNodes) {
for (var i = 0; i < childNodes.length; i++) {
var childNode = new Ext.tree.TreeNode({
id : childNodes[i].id,
text : childNodes[i].text,
leaf : childNodes[i].leaf//主要加这个
});
if (childNodes[i].children != undefined) {
createChildTreeNode(childNode, childNodes[i].children);
}
node.appendChild(childNode);
}
}
function createTreeEditor(tree) {
var treeEditor = new Ext.tree.TreeEditor(tree, {
allowBlank : false,
blankText : '请输入项目名称',
selectOnFocus : true,
listeners : {
'complete' : function() {

            }
        }
    });
return treeEditor;

}
function createContextMenu(node, event) {
var contextMenu;
if (!node.isLeaf()) {// 这里修改了
contextMenu = new Ext.menu.Menu({
id : 'rootMenu',
items : [{
text : 'new',
scope : this,
handler : function() {

                    }
                }]
        });
    return contextMenu;
} else if (node.isLeaf()) {// 这里修改了
    contextMenu = new Ext.menu.Menu({
            id : 'Menu',
            items : [{
                    text : 'new',
                    scope : this,
                    handler : function() {

                    }
                }, '-', {
                    text : 'update',
                    scope : this,
                    handler : function() {

                    }
                }, '-', {
                    text : 'delete',
                    scope : this,
                    handler : function() {

                    }
                }]
        });
    return contextMenu;
} else {
    contextMenu = new Ext.menu.Menu({
            id : 'leafMenu',
            items : [{
                    text : 'update',
                    scope : this,
                    handler : function() {

                    }
                }, '-', {
                    text : 'delete',
                    scope : this,
                    handler : function() {

                    }
                }]
        });
    return contextMenu;
}

}
function showContextMenu(node, event) {
alert(node.isLeaf());
var contextMenu = createContextMenu(node, event);
contextMenu.showAt(event.getXY());
}

[/code]