天天看點

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);
			}
		});
}
           

繼續閱讀