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的数据一定是按照树的结构排序出来的。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值