1. 程式人生 > >easyui的combotree同步樹和非同步樹實現方法

easyui的combotree同步樹和非同步樹實現方法

<select class="easyui-combotree" id="acceptOrgNo" name="acceptOrgNo" style="width:300px" data-options="url:'glCaseInfo.ered?reqCode=zfjOrgTree',method:'get',required:false,panelHeight:400,multiple:true"></select>

1 非同步樹

$("#"+treeId).combotree({
url : treeUrl,
panelWidth:'200',
onClick : function(node) {
   onBeforeExpand:function(node,param){
       $(this).tree('options').url = treeUrl+'&id='+node.id;         
   },//這個地方就是實現每次非同步載入子類的方法,每次展開之前去查尋這個子類的資料
   onLoadSuccess:function(node,data){
       var defVal=formData[orgParam.idName];
           var defText=formData[orgParam.textName];
var combotree =$("#"+treeId);
var tree = combotree.combotree('tree');
var defNode = tree.tree("find",defVal);
if(!defNode){
tree.tree('append', {
data: [{
id: defVal,
text: defText
}]
});
defNode = tree.tree("find",defVal);
combotree.combotree('setValue',defVal);
tree.tree('select',defNode.target);
defNode.target.style.display='none';
}else{
combotree.combotree('setValue',defVal);
}    
   }
});

Action實現

public ActionForward zfjOrgTree(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
Dto qryDto = new BaseDto();
String orgId= request.getParameter("id");
if(G4Utils.isEmpty(orgId)){
orgId="20";
}
qryDto.put("orgId", orgId);
ArrayList nodeList= new  ArrayList();
List orgList = g4Reader.queryForList("ZFJOrgInfo.qryZfjTree", qryDto);
Dto orgDto = null;
Dto nodeDto= null;
String orgType="";
for(int i = 0; i < orgList.size(); i++){
orgDto = (BaseDto)orgList.get(i);
nodeDto= new BaseDto();
nodeDto.put("id", orgDto.get("orgId"));
nodeDto.put("text", orgDto.get("orgName"));
if(orgDto.getAsInteger(("childNum")).intValue()>0){
nodeDto.put("state","closed");
} else {
nodeDto.put("state","open");
} 
orgType=orgDto.getAsString("ORGTYPE");
nodeDto.put("linkTel", orgDto.get("linkTel"));
nodeDto.put("linkMan", orgDto.get("linkMan"));
nodeDto.put("attributes",JsonHelper.encodeObject2Json(orgDto));
nodeList.add(nodeDto); 
}
String jsonString = JsonHelper.encodeObject2Json(nodeList);
super.write(jsonString, response);
return mapping.findForward(null);
} 

ibaits實現

<select id="qryZfjTree" parameterClass="map" resultClass="dto">
SELECT orgNo as  orgId, orgName , parentNo as parentId,linkMan,linkTel,
 (select count(*) from EX_ZZJ_ORGINFO a where a.parentNo=EX_ZZJ_ORGINFO.orgNo) as childNum
FROM EX_ZZJ_ORGINFO WHERE 1=1 
<dynamic prepend="">
<isNotEmpty prepend=" AND " property="orgName"> orgName like '%$orgName$%'</isNotEmpty>
<isNotEmpty prepend=" AND " property="orgId"> parentNo=#orgId#</isNotEmpty>
</dynamic>
ORDER BY orgNo 
</select>
這樣就可以實現非同步樹了,每次點選的時候再去查詢子類,我們只是初始化以及大類

2 同步樹(我們如果要是實現樹的多選回顯,就要用同步樹了,應為我們要第一步要初始化之前儲存的值也即是id)

<select class="easyui-combotree" id="acceptOrgNo" name="acceptOrgNo" style="width:300px" data-options="required:false,panelHeight:400,multiple:true"></select>

js實現

function initZfjData(data){
if(!isEmpty(data.orgNos)){
$("#DB_OP_FLAG").val("1");   //0代表新增,1代表修改
var orgNos=data.orgNos;
var treeUrl ="org.ered?reqCode=zfjOrgTree";
$("#acceptOrgNo").combotree({
url : treeUrl,
panelWidth:'300',
panelHeight:'400',
cascadeCheck:false,
onLoadSuccess:function(node,data){
var t = $("#acceptOrgNo").combotree('tree');//獲取tree  
for (var i=0;i<data.length ;i++ ){  
node= t.tree("find",data[i].id);  
t.tree('expandAll',node.target);//展開所有節點    
}  
$("#acceptOrgNo").combotree("setValues",orgNos.split(","));
}
}); 
}
}

裡面的data是查詢資料庫要回顯的節點id集(1,2,3,4,5,6)

Action實現

public ActionForward zfjOrgTree(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
 Dto qryDto = new BaseDto();
 String orgId= "20";
     qryDto.put("orgId", orgId);
 ArrayList nodeList= new  ArrayList();
 List orgList = g4Reader.queryForList("ZFJOrgInfo.qryZfjTree", qryDto);
 Dto orgDto = null;
 Dto nodeDto= null;
   if(orgList.size()>0&&orgList!=null){
    for(int i=0;i<orgList.size();i++){
    orgDto = (BaseDto)orgList.get(i);
    nodeDto= new BaseDto();
nodeDto.put("id", orgDto.get("orgId"));
nodeDto.put("text", orgDto.get("orgName"));
if(orgDto.getAsInteger(("childNum")).intValue()>0){
nodeDto.put("state","closed");
nodeDto.put("children",getChildNode(orgDto.getAsString("orgId")));
} else {
nodeDto.put("state","open");
}
nodeList.add(nodeDto);
    }
     
   }
String jsonString = JsonHelper.encodeObject2Json(nodeList);
super.write(jsonString, response);
return mapping.findForward(null);
}
/**遞迴獲取全部的子節點
* @return
*/ 
public ArrayList getChildNode (String orgId){
Dto qryDto = new BaseDto();
qryDto.put("orgId", orgId);
List orgList = g4Reader.queryForList("ZFJOrgInfo.qryZfjTree", qryDto);
ArrayList nodeList= new  ArrayList();
Dto orgDto = null;
Dto nodeDto= null;  
if(orgList.size()>0&&orgList!=null){
for(int i=0;i<orgList.size();i++){
orgDto = (BaseDto)orgList.get(i);
    nodeDto= new BaseDto();
nodeDto.put("id", orgDto.get("orgId"));
nodeDto.put("text", orgDto.get("orgName"));
if(orgDto.getAsInteger(("childNum")).intValue()>0){
nodeDto.put("state","closed");
nodeDto.put("children",getChildNode(orgDto.getAsString("orgId")));
} else {
nodeDto.put("state","open");
}
nodeList.add(nodeDto); 
}

}
return nodeList;
}


ibaits也是上面同樣的語句

主要是我搞了兩天,記著防止以後忘了,