工作的原因,以后主要做一些JavaWeb的开发工作,开辟这个文章集主要用来记录学习的过程和分享遇到的问题,方便日后回顾或许还能帮助网友解决相同的问题。
easyUi的TreeGrid的问题
easyui是个不错的前端库,以后好好学习学习。最近在做业务遇到了TreeGrid的加载问题。官方的手册的加载数据有三种是:
No.1:
[{
"id":1,
"name":"C",
"size":"",
"date":"02/19/2010",
"children":[{
"id":2,
"name":"Program Files",
"size":"120 MB",
"date":"03/20/2010",
"children":[{
"id":21,
"name":"Java",
"size":"",
"date":"01/13/2010",
"state":"closed",
"children":[{
"id":211,
"name":"java.exe",
"size":"142 KB",
"date":"01/13/2010"
},{
"id":212,
"name":"jawt.dll",
"size":"5 KB",
"date":"01/13/2010"
}]
},{
"id":22,
"name":"MySQL",
"size":"",
"date":"01/13/2010",
"state":"closed",
"children":[{
"id":221,
"name":"my.ini",
"size":"10 KB",
"date":"02/26/2009"
},{
"id":222,
"name":"my-huge.ini",
"size":"5 KB",
"date":"02/26/2009"
},{
"id":223,
"name":"my-large.ini",
"size":"5 KB",
"date":"02/26/2009"
}]
}]
},{
"id":3,
"name":"eclipse",
"size":"",
"date":"01/20/2010",
"children":[{
"id":31,
"name":"eclipse.exe",
"size":"56 KB",
"date":"05/19/2009"
},{
"id":32,
"name":"eclipse.ini",
"size":"1 KB",
"date":"04/20/2010"
},{
"id":33,
"name":"notice.html",
"size":"7 KB",
"date":"03/17/2005"
}]
}]
}]
如上第一种是采用json嵌套的形式加载全部数据,这样很好理解,但是数据格式复杂,后台也不好返回这种数据。关键词 Children
No.2
{"total":7,"rows":[
{"id":1,"name":"All Tasks","begin":"3/4/2010","end":"3/20/2010","progress":60,"iconCls":"icon-ok"},
{"id":2,"name":"Designing","begin":"3/4/2010","end":"3/10/2010","progress":100,"_parentId":1,"state":"closed"},
{"id":21,"name":"Database","persons":2,"begin":"3/4/2010","end":"3/6/2010","progress":100,"_parentId":2},
{"id":22,"name":"UML","persons":1,"begin":"3/7/2010","end":"3/8/2010","progress":100,"_parentId":2},
{"id":23,"name":"Export Document","persons":1,"begin":"3/9/2010","end":"3/10/2010","progress":100,"_parentId":2},
{"id":3,"name":"Coding","persons":2,"begin":"3/11/2010","end":"3/18/2010","progress":80},
{"id":4,"name":"Testing","persons":1,"begin":"3/19/2010","end":"3/20/2010","progress":20}
],"footer":[
{"name":"Total Persons:","persons":7,"iconCls":"icon-sum"}
]}
第二种的Json数据没有嵌套关系,使用_parentId 来指定父节点。数据平行,可以直接从数据库中拿数据。且_parentId关键词被指定,不能改。
** !!!着重看第一行,也就是根节点,并没有** _parentId ** ,有时根节点指定了该属性等于某值,如 ** _parentId=0 ,前端treegrid为空白,但是数据库中不可取消该属性,所以可以将根节点 _parentId=null, 即可,但是用html+js测试却没有该问题。自己看着办吧。
该类数据除了没有嵌套外,比第一种还多了 row和total和footer,不可去掉,否则也无法显示。后台返回数据时再封装一层吧。
No.3
[
{"id":1,"name":"All Tasks","begin":"3/4/2010","end":"3/20/2010","progress":60,"iconCls":"icon-ok",state":"closed"},
{"id":2,"name":"Designing","begin":"3/4/2010","end":"3/10/2010","progress":100,"state":"closed"},
{"id":21,"name":"Database","persons":2,"begin":"3/4/2010","end":"3/6/2010","progress":100,state":"closed"},
{"id":22,"name":"UML","persons":1,"begin":"3/7/2010","end":"3/8/2010","progress":100,state":"closed"},
{"id":23,"name":"Export Document","persons":1,"begin":"3/9/2010","end":"3/10/2010","progress":100,"state":"closed"},
{"id":3,"name":"Coding","persons":2,"begin":"3/11/2010","end":"3/18/2010","progress":80,"state":"closed"},
{"id":4,"name":"Testing","persons":1,"begin":"3/19/2010","end":"3/20/2010","progress":20,"state":"closed"}
]
第三种,其实就是没有Childer和_parentId关键词,这样会有问题,怎么表示子父关系呢,对了,所以这中格式一般作为异步加载,即一次请求数据加载一层,就是把该层的id作为请求参数去后台拿数据,后台返回的就是该id的子节点,返回的数据时平级的,所以不用表示层级关系,一般在点击展开“+”时。有个关键词"state":"closed",否则看到返回的数据有"state":"open",会自动触发再去请求下一层。就达不到异步加载,就是点击展开就加载,这样可以避免一次全部数据性能跟不上,造成卡顿。
关于 .treeGrid('laod',{param:"value"})
在做后台的树结构查询时,用该函数带入表单的数据时,以后每次向后台取数据时,如点击展开的“+”按钮时,请求都会带进这个参数,因为easyUI会把该参数给记录。
看jquery.easyui.min.js源码:
function _670(_753,_754,cb){
var opts=$.data(_753,"datagrid").options;
if(_754){
opts.queryParams=_754;
}
其中,_754就是该参数,储存在options.queryParams属性中,以后的请求数据都会和作为post请求参数。
点击“+”展开时,又是一次查询函数的调用,导致无法展开。在查询函数里,最后添加:
$(#'xxxxx_treeGrid').treegrid('options').queryParams = null;
将该属性重置下。