天天看點

jqgrid 靜态樹

js代碼:      
var lastsel = 0;
    
    jQuery("#list2").jqGrid({
            treeGrid: true,
            treeGridModel: 'adjacency', //treeGrid模式,跟json元數據有關 ,adjacency/nested
            ExpandColumn : 'username',
            datatype: 'jsonstring',
            colNames:['姓名','id','密碼','年齡','位址','出生日期'],
            colModel:[
                {name:'username',index:'username', width:110,sorttype:"int",editable: false},
                {name:'id',index:'id', width:90,sorttype:"int",hidden:true},
                {name:'password',index:'password', width:80,editable: true},
                {name:'age',index:'age', width:80,editable: true},
                {name:'address',index:'address', width:80,editable: true},
                {name:'time',index:'time', width:80,sorttype:"date",editable: true}
            ],
      onSelectRow: function(id){
        if(id && id!==lastsel){
          jQuery('#list2').jqGrid('restoreRow',lastsel);
          jQuery('#list2').jqGrid('editRow',id,true);
          lastsel=id;
        }
      },
            pager: "false",
      scroll: "true",
            jsonReader: {
                root: "dataRows",
                repeatitems : false
            },
            treeReader : {
                level_field: "level",
                parent_id_field: "parent",
                leaf_field: "isLeaf",
                expanded_field: "expanded"
            },
            caption: "jqGrid 同步樹表格",
            height: "auto"    // 設為具體數值則會根據實際記錄數出現垂直滾動條
            //rowNum : "-1",     // 顯示全部記錄
            //shrinkToFit:false  // 控制水準滾動條
        });
    
    var datas = null;
    jQuery.doAjax({
      url: '<%=basePath %>ajax/contract/treeJson.action',
      callback: function(_data){
        alert($.toJSONString(_data));
        datas = _data;
      }
    });
    jQuery("#list2").jqGrid('setGridParam',{datastr:datas}).trigger("reloadGrid");      

action代碼:

public void treeJson() {
    JSONObject jo = new JSONObject();
    Long nodeId = getLongParameter("nodeid");
    Long level = getLongParameter("n_level");
    try {

      JSONArray ja = getData(nodeId, level);
      jo.put("dataRows", ja);
    } catch (Exception e) {
      logger.error("合同查詢失敗," + e);
    }
    
    super.renderJson(jo);
  }

  private JSONArray getData(Long nodeId, Long level) {
    JSONArray ja = new JSONArray();
    
    JSONObject jo = new JSONObject();
    jo.put("id", "123");
    jo.put("username", "根節點");
    jo.put("age", "10");
    jo.put("password", "123213123");
    jo.put("address", "beijingshi...");
    jo.put("level", "0");
    jo.put("parent", "0");
    jo.put("isLeaf", false);
    jo.put("expanded", true);
    ja.add(jo);

    JSONObject jo1 = new JSONObject();
    jo1.put("id", "153");
    jo1.put("username", "廣告部");
    jo1.put("age", "40");
    jo1.put("password", "1sdf23213123");
    jo1.put("address", "keoukel...");
    jo1.put("level", "1");
    jo1.put("parent", "123");
    jo1.put("isLeaf", false);
    jo1.put("expanded", true);
    ja.add(jo1);
    
    JSONObject jo3 = new JSONObject();
    jo3.put("id", "173");
    jo3.put("username", "廣告_業務");
    jo3.put("age", "10");
    jo3.put("password", "123213123");
    jo3.put("address", "beijingshi...");
    jo3.put("level", "2");
    jo3.put("parent", "153");
    jo3.put("isLeaf", false);
    jo3.put("expanded", true);
    ja.add(jo3);
    
    JSONObject jo5 = new JSONObject();
    jo5.put("id", "193");
    jo5.put("username", "廣告_業務_小馬");
    jo5.put("age", "20");
    jo5.put("password", "asdfsa");
    jo5.put("address", "nanjing...");
    jo5.put("level", "3");
    jo5.put("parent", "173");
    jo5.put("isLeaf", true);
    jo5.put("expanded", false);
    ja.add(jo5);

    JSONObject jo2 = new JSONObject();
    jo2.put("id", "163");
    jo2.put("username", "無線部");
    jo2.put("age", "20");
    jo2.put("password", "asdfsa");
    jo2.put("address", "nanjing...");
    jo2.put("level", "1");
    jo2.put("parent", "123");
    jo2.put("isLeaf", false);
    jo2.put("expanded", true);
    ja.add(jo2);
    

    JSONObject jo4 = new JSONObject();
    jo4.put("id", "183");
    jo4.put("username", "無線_銷售");
    jo4.put("age", "40");
    jo4.put("password", "1sdf23213123");
    jo4.put("address", "keoukel...");
    jo4.put("level", "2");
    jo4.put("parent", "163");
    jo4.put("isLeaf", false);
    jo4.put("expanded", true);
    ja.add(jo4);

    JSONObject jp6 = new JSONObject();
    jp6.put("id", "203");
    jp6.put("username", "無線_銷售_小劉");
    jp6.put("age", "60");
    jp6.put("password", "777asdfsa");
    jp6.put("address", "shanxisheng...");
    jp6.put("level", "3");
    jp6.put("parent", "183");
    jp6.put("isLeaf", true);
    jp6.put("expanded", false);
    ja.add(jp6);
    
    return ja;
  }      

注:靜态加載時json的資料一定是按照樹的結構排序出來的。