在使用easyUI 的treeGrid的时候,很多时候我们从数据库取出来的数据treeGrid却不能读取显示成一个树:如下
{ menuCode: "a00", menuName: "管理员首页", menuUrl: "/main/admin/index.action", menuPCode: "-1", isLeaf: "0", status: "1", icon: null, createUser: "system", createDate: "2014-01-24 17:10:25", modifyUser: "system", modifyDate: "2014-01-24 17:10:25" }, { menuCode: "a0001", menuName: "修改密码", menuUrl: "/main/admin/modifyPwd.action", menuPCode: "a00", isLeaf: "1", status: "1", icon: null, createUser: "system", createDate: "2014-01-24 17:10:25", modifyUser: "system", modifyDate: "2014-01-24 17:10:25" }, { menuCode: "a0002", menuName: "图片上传", menuUrl: "/image/admin/uploadImage.action", menuPCode: "a00", isLeaf: "1", status: "1", icon: null, createUser: "system", createDate: "2014-01-24 17:10:25", modifyUser: "system", modifyDate: "2014-01-24 17:10:25" }
例如以上是我从数据库表中取的值并转化为json格式,逻辑上我是靠menuCode和menuPcode来表示父子的继承关系,但是easyUI的treeGrid不买账,她可不会理解你的父子关系是通过menuCode和menuPcode表现的
于是乎有人觉得json格式应该如下:
通过设置children来方便treeGrid解析,于是乎有人开始对取出来的表数据进行遍历啊,拼接字符串啦之类的操作,往这个格式上拼凑,其实完全没必要
解决办法:
使用谷歌浏览器打开如下链接
http://www.jeasyui.net/demo/581.html
F12查看该例子树形数据表格的数据格式:
数据格式为:
{"total":9,"rows":[ {"id":1,"region":"Wyoming"}, {"id":11,"region":"Albin","f1":2000,"f2":1800,"f3":1903,"f4":2183,"f5":2133,"f6":1923,"f7":2018,"f8":1838,"_parentId":1}, {"id":12,"region":"Canon","f1":2000,"f2":1800,"f3":1903,"f4":2183,"f5":2133,"f6":1923,"f7":2018,"f8":1838,"_parentId":1}, {"id":13,"region":"Egbert","f1":2000,"f2":1800,"f3":1903,"f4":2183,"f5":2133,"f6":1923,"f7":2018,"f8":1838,"_parentId":1}, {"id":2,"region":"Washington"}, {"id":21,"region":"Bellingham","f1":2000,"f2":1800,"f3":1903,"f4":2183,"f5":2133,"f6":1923,"f7":2018,"f8":1838,"_parentId":2}, {"id":22,"region":"Chehalis","f1":2000,"f2":1800,"f3":1903,"f4":2183,"f5":2133,"f6":1923,"f7":2018,"f8":1838,"_parentId":2}, {"id":23,"region":"Ellensburg","f1":2000,"f2":1800,"f3":1903,"f4":2183,"f5":2133,"f6":1923,"f7":2018,"f8":1838,"_parentId":2}, {"id":24,"region":"Monroe","f1":2000,"f2":1800,"f3":1903,"f4":2183,"f5":2133,"f6":1923,"f7":2018,"f8":1838,"_parentId":2} ],"footer":[ {"region":"Total","f1":14000,"f2":12600,"f3":13321,"f4":15281,"f5":14931,"f6":13461,"f7":14126,"f8":12866} ]}
属性表格加载正确的前提是json数据中要正确初始化_parentId节点。