日期:2014-05-16  浏览次数:20549 次

Extjs 4.2 树结点右键菜单(全选,反选,撤销)

自己写的最新版本extjs4.2的树结点的操作,记录一下,以后可能会用到。

	var tree = new Ext.tree.TreePanel({
		flex: 1,
		animate: true,  
		autoScroll: true,
		anchor: '100% 93%',
		store: new Ext.data.TreeStore({
			root:{
				expanded: true,
				text:'A',
				children:[{
					expanded: true,
					text:'1',
					children:[{
						text:'2',
						icon:'sysimg/icon/image.gif',
						leaf:true,
						id:'1_1',
						checked:false
					},{
						text:'3',
						icon:'sysimg/icon/layout.gif',
						leaf:true,
						id:'1_2',
						checked:false
					},{
						text:'4',
						icon:'sysimg/icon/image_add.png',
						leaf:true,
						id:'1_3',
						checked:false
					},{
						text:'5',
						icon:'sysimg/icon/out.gif',
						leaf:true,
						id:'1_4',
						checked:false
					},{
						text:'6',
						icon:'sysimg/icon/leave.gif',
						leaf:true,
						id:'1_5',
						checked:false
					},{
						text:'7',
						icon:'sysimg/icon/preview_16x16.gif',
						leaf:true,
						id:'1_5',
						checked:false
					},{
						text:'8',
						icon:'sysimg/icon/upload.gif',
						leaf:true,
						id:'1_7',
						checked:false
					},{
						text:'9',
						icon:'sysimg/icon/users--exclamation.gif',
						leaf:true,
						id:'1_8',
						checked:false
					},{
						text:'10',
						icon:'sysimg/icon/Pixies_020.gif',
						leaf:true,
						id:'1_9',
						checked:false
					}]
				},{
					expanded: true,
					text:'B',
					children:[{
						text:'11',
						icon:'sysimg/icon/Pixies_023.gif',
						leaf:true,
						id:'2_1',
						checked:false
					},{
						text:'12',
						icon:'sysimg/icon/Pixies_017.gif',
						leaf:true,
						id:'2_2',
						checked:false
					}]
				},{
					expanded: true,
					text:'C',
					children:[{
						text:'13',
						icon:'sysimg/icon/users--arrow.gif',
						leaf:true,
						id:'3_1',
						checked:false
					},{
						text:'14',
						icon:'sysimg/icon/users.gif',
						leaf:true,
						id:'3_2',
						checked:false
					},{
						text:'15',
						icon:'sysimg/icon/hammer.gif',
						leaf:true,
						id:'3_3',
						checked:false
					},{
						text:'16',
						icon:'sysimg/icon/bandaid--arrow.gif',
						leaf:true,
						id:'3_4',
						checked:false
					},{
						text:'17',
						icon:'sysimg/icon/user_go.gif',
						leaf:true,
						id:'3_5',
						checked:false
					},{
						text:'18',
						icon:'sysimg/icon/cog.gif',
						leaf:true,
						id:'3_6',
						checked:false
					},{
						text:'19',
						icon:'sysimg/icon/publish.gif',
						leaf:true,
						id:'3_7',
						checked:false
					},{
						text:'20',
						icon:'sysimg/icon/users--exclamation.gif',
						leaf:true,
						id:'3_8',
						checked:false
					},{
						text:'21',
						icon:'sysimg/icon/user-black.gif',
						leaf:true,
						id:'3_9',
						checked:false
					},{
						text:'22',
						icon:'sysimg/icon/leaf.gif',
						leaf:true,
						id:'3_10',
						checked:false
					}]
				}]
			}
		}),
		listeners:{
			'itemcontextmenu':function(menutree,record,items,index,e){
				e.preventDefault();
				e.stopEvent();
				//判断是否为叶子结点
				if(record.data.leaf==false){
				var nodemenu = new Ext.menu.Menu({
					floating:true,
					items:[{
						text:'全选',
						handler:function(){
							for( var i =0;i<record.data.children.length;++i){
								//设置结点checked属性为true
								record.childNodes[i].set('checked',true);	
							}
						}
					},{
						text:'反选',
						handler:function(){
							for( var i =0;i<record.data.children.length;++i){
								if(record.childNodes[i].data.checked == false) {
									//设置结点checked属性为true
									record.childNodes[i].set('checked',true)}
								else {
									//设置结点checked属性为true
									record.childNodes[i].set('checked',false);	
								}	
							}
						}