Ext4.2 treepanel 获取当前节点node,展开节点node,选择节点node,刷新树

阅读更多

 

treepanel 当前节点,以下3种方法都可以

currentNode = record;   
currentNode = this.getSelectionModel().getLastSelected();  
currentNode = treOrganization.getStore().getNodeById(record.data.id);

 

	var currentNodeId = 1; 	//组织树当前节点id;
	var parentNodeId = 0;	//组织树父节点id;	0	

	//组织树store
	var treeStoreOrganization = Ext.create('Ext.data.TreeStore', {
		nodeParam: 'parentId',	//默认的是把id作为node传到后台
		autoLoad: true, 		//此属性为false时,root的expanded也得为false
		proxy: {
            type: 'ajax',
            url: 'getListByParentId.json',
            extraParams  : {                         
            	expandedLevels : 1 //预先在后台展开几级,暂时没用
			},
            reader: {
				type: 'json',
        		root: 'extTreeVoList' //josn中的根节点
         	}
        },
	    root: {
	    	text: '根节点',
            id: 0,
	        expanded: true,	//为true就会自动加载
	    }
//	        ,
//		    sorters: [{
//	            property: 'text',
//	            direction: 'ASC'
//	        }]
	});

	treeStoreOrganization.on('load',function(store,records,success,options) {
		if(records.data.id == 0){	//records看似像加载来的子节点,但他是父节点
			parentNodeId = 0;
			if(treOrganization.getRootNode().childNodes.length>0){
				var node = treOrganization.getRootNode().childNodes[0];	//根下的第一个节点
				treOrganization.getSelectionModel().select(node);
				currentNodeId = node.data['id'];
				node.expand();
				//idPath = node.getPath("id"); // /0/1
				//node.getPath('text')
			}
		}
	});
	
	//组织树
	var treOrganization = Ext.create('Ext.tree.Panel', {
		id: 'treOrganization',
		//title: '组织机构管理',
		//region:'west',
        store: treeStoreOrganization,
        rootVisible:false,	                
        useArrows: false,
        border: false,
        listeners : {
            'itemclick' : function(view,record,item,index,e,eOpts){  
            	currentNodeId = record.data.id;
            	parentNodeId = record.parentNode.data.id;
            	//currentNodePath = record.getPath();

//        		currentNode = record; 
//            	currentNode = this.getSelectionModel().getLastSelected();
//            	currentNode = treOrganization.getStore().getNodeById(record.data.id);
            	
            	getPageList();
                if(record.data.leaf){
                    //alert(record.data.id);
                	//alert(record.data.text);
                    //alert(record.raw.url); //不扩展也能从raw取
                }else{  
//                    if(record.data.expanded){  
//                        view.collapse(record);  
//                    }else{  
//                        view.expand(record);  
//                    }  
                }  
            }  
        }  
    });

 

对树进行增删改后,刷新树

/**
 * 刷新树 刷新父节点,展开或不展开当前节点,选中当前节点
 * @param treePanel	
 * @param refreshRoot	是否刷新root
 * @param currentNodeId	当前节点id
 * @param parentNodeId	父节点id
 */
function refreshNode(treePanel, refreshRoot, currentNodeId, parentNodeId){
	var currentNode = treePanel.getStore().getNodeById(currentNodeId); //当前节点删除了就不存在了
	var parentNode = treePanel.getStore().getNodeById(parentNodeId);
	var rootNode = treePanel.getStore().getRootNode();
	
	var path;
	if(currentNode){
		path = currentNode.getPath('id'); //currentNode.getPath('text')
	} else if(parentNode){
		path = parentNode.getPath('id');
	} else {
		path = rootNode.getPath('id');
	}
	
	var loadNode;
	if(refreshRoot){
		loadNode = rootNode;
	} else {
		loadNode = parentNode;
	}
	//刷新节点,展开节点,选中节点
	treePanel.getStore().load({
			node: loadNode, 
			callback:function(){
				if(currentNode && currentNode.data.expanded){
					//之前展开的还是展开
					treePanel.expandPath(path);
				}
				treePanel.selectPath(path);
			}
		});
}

 

你可能感兴趣的:(Ext,tree,节点,node,刷新)