easyui tree 把選中的值存到資料庫中並從資料庫中取出在tree上顯示出來

hiiyy發表於2013-09-03

jsp頁面:

選擇收文機構:         
        <a href="#" onclick="collapseAll()">全部關閉</a>
        <a href="#" onclick="expandAll()">全部展開</a>
        <input type="hidden" name="fileoperate.sendUnitId"
         id="sendUnitId" />
        <input type="hidden" name="fileoperate.sendUnitName"
         id="sendUnitName" />
        <br />
        <ul id="jigou">
        </ul>

後臺:(從資料庫中取出資訊拼接json串)

public String areaStorageAct() {
  // ----------------------------------------------------------------------------------
     Map session = ServletActionContext.getContext().getSession();
  InfoWebBean infoWebBean=(InfoWebBean) session.get("infobean");
  adminId=infoWebBean.getId();//結果:32位的Id
  adminUser=clubService.findAdminById(adminId);
  String adminArea="";
  String adminAreaName="";
    IAreaManage areaManage = AreaManageBean.getInstance();
    myAreaManageList = new ArrayList<AreaManage>();
          StringBuffer buffer=new StringBuffer();
  if (StringUtils.isNotBlank(adminUser.getXian())&&!"null".equals(adminUser.getXian())) {
   adminArea=adminUser.getXian();
    buffer.append("");
    treeData=buffer.toString();
             ServletActionContext.getRequest().setAttribute("treeData",treeData);
  }else if(StringUtils.isNotBlank(adminUser.getShi())&&!"null".equals(adminUser.getShi())) {
   adminArea=adminUser.getShi();
   adminAreaName=areaManage.getAreaName(adminArea);
    List<AreaManage>  tempMyAreaManageList = areaManage.getXianByShiID(adminArea);
     buffer.append("[");
    buffer.append("{\"id\":\""+adminArea+"\",\"text\":\""+adminAreaName+"\",\"state\":\"closed\",\"children\":[");
    for(int i = 0 ;i<tempMyAreaManageList.size();i++){
    buffer.append("{\"id\":\""+tempMyAreaManageList.get(i).getId()+"\",\"text\":\""+adminAreaName+tempMyAreaManageList.get(i).getName()+"\"}");
                  if(i<tempMyAreaManageList.size()-1){
                  buffer.append(",");
                  }
    }
                 buffer.append("]}]");
                 treeData=buffer.toString();
                 ServletActionContext.getRequest().setAttribute("treeData",treeData);
                 System.out.println("buffer--------------------------------------------------------"+buffer);
    }else if(StringUtils.isNotBlank(adminUser.getSheng())&&!"null".equals(adminUser.getSheng())){
   adminArea=adminUser.getSheng();
            List<AreaManage>  tempMyAreaManageList = areaManage.getShiByShengID(adminArea);
             buffer.append("[");
             if (tempMyAreaManageList.size()>0){
              for(int i = 0 ; i<tempMyAreaManageList.size();i++){
               String id=tempMyAreaManageList.get(i).getId();
               String name=tempMyAreaManageList.get(i).getName();
               buffer.append("{\"id\":\""+id+"\",\"text\":\""+name+"\",\"state\":\"closed\",\"children\":[");
                     myAreaManageList=areaManage.getXianByShiID(id);
                     for (int j=0;j<myAreaManageList.size();j++){
                      buffer.append("{\"id\":\""+myAreaManageList.get(j).getId()+"\",\"text\":\""+name+myAreaManageList.get(j).getName()+"\"}");
                      if(j<myAreaManageList.size()-1){
                      buffer.append(",");
                      }
                     }
                     buffer.append("]}");
                     if(i<tempMyAreaManageList.size()-1){
                       buffer.append(",");
                     }
              
                 }
             }
             buffer.append("]");
             treeData=buffer.toString();
             ServletActionContext.getRequest().setAttribute("treeData",treeData); 
  }
     
 }

前臺(js)

//從後臺取得json串並新增到jsp頁面中,載入成功後把資料庫中取出的值在tree上顯示出來(json串中必須有id)

$(function() {
    $("#jigou").tree({
  checkbox : true,
  cascadeCheck : false,
  url : "fileoperate_areaStorage.action",
  onLoadSuccess:function(){
  
 var vals='${fileoperate.sendUnitId}';
 var arrs=new Array();
 arrs=vals.split(",");
 for(var i=0;i<arrs.length;i++){
     var node=$('#jigou').tree('find',arrs[i]);
     $('#jigou').tree('check',node.target);
 }
  }
 });
 
 
});

//獲得選中的checkbox的id值
function getChecked() {
 var nodes = $("#jigou").tree('getChecked');
 var s = '';
 var tt = '';
 for ( var i = 0; i < nodes.length; i++) {
  s += nodes[i].id;
  tt += nodes[i].text;
  if (i < nodes.length - 1) {
   s += ',';
   tt += ',';
  }
 }
 document.getElementById("sendUnitId").value = s;
 document.getElementById('sendUnitName').value = tt;
}

//全部關閉

function collapseAll() {
 $('#jigou').tree('collapseAll');
}

//全部展開
function expandAll() {
 $('#jigou').tree('expandAll');
}

 

相關文章