`
clarancepeng
  • 浏览: 189430 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

使用xml或者json方式生成dhtmlxtree

阅读更多
1. dao
private static ParameterizedRowMapper<MenuInfo> menuInfoMapper = new ParameterizedRowMapper<MenuInfo>() {
public MenuInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
MenuInfo menuInfo = new MenuInfo();
menuInfo.setHasit(rs.getString("hasit"));
menuInfo.setPmenuid(rs.getLong("pmenuid"));
menuInfo.setLevel(rs.getLong("level"));
menuInfo.setId(rs.getLong("id"));
menuInfo.setMsrtno(rs.getLong("msrtno"));
menuInfo.setMenunm(rs.getString("menunm"));

menuInfo.setLocation(rs.getString("location"));
menuInfo.setActiontyp(rs.getLong("actiontyp"));
menuInfo.setHlpid(rs.getLong("hlpid"));
menuInfo.setLfabl(rs.getString("lfabl"));
menuInfo.setFbdst(rs.getString("fbdst"));
return menuInfo;
}
};
public Collection<MenuInfo> testPrivilege() {
final String sql = "select nvl((select 'a' from wpv0105_rlprv b where b.lbl='0' and b.rlid=? and b.prvid=a.id),'b') as hasit, "
+" a.pmenuid,level, a.id, a.msrtno, a.menunm, a.location, a.actiontyp, a.hlpid, a.lfabl,a.fbdst "
+" from wpv0101_menu a start with pmenuid=-1 connect by prior id = pmenuid and a.id<>100031401 "
+" group by pmenuid,level, id, msrtno, menunm, location, actiontyp, hlpid, lfabl,fbdst "
+" order by id||'0', msrtno";
return jdbcTemplate.query(sql, menuInfoMapper, new Object[]{new Long(0)});
}

2. 数据封装
(1) xml
@Test public void testMenu() throws Exception {
java.util.Collection<MenuInfo> menuInfoList = mobileDao.testPrivilege();
java.io.OutputStream out = null;
java.io.OutputStreamWriter dataout = null;
out = new java.io.FileOutputStream("aaa.xml");
dataout = new java.io.OutputStreamWriter(out, "UTF-8");
dataout.write("<?xml version='1.0' encoding='UTF-8'?>\n<tree id='0'>");
int level=0;
for(MenuInfo vo : menuInfoList)
{
if(level == Integer.parseInt(vo.getLevel()+"")) {
dataout.write("</item>\r\n");
final String chk = "a".equals(vo.getHasit())?"checked=\"1\"":"";
dataout.write("<item " + chk + " text=\""+ vo.getMenunm() +"\" id=\"" + vo.getId() + "\" im0=\"leaf.gif\" im1=\"folderOpen.gif\" im2=\"folderClosed.gif\">");

}
else if(Integer.parseInt(vo.getLevel()+"") > level) {
final String chk = "a".equals(vo.getHasit())?"checked=\"1\"":"";
dataout.write("  <item " + chk + " text=\""+ vo.getMenunm() +"\" id=\"" + vo.getId() + "\" im0=\"leaf.gif\" im1=\"folderOpen.gif\" im2=\"folderClosed.gif\">");
}
else {
for(int i =0; i < level + 1 - Integer.parseInt(vo.getLevel()+""); i++)
dataout.append("</item>\r\n");
final String chk = "a".equals(vo.getHasit())?"checked=\"1\"":"";
dataout.write("<item " + chk + " text=\""+ vo.getMenunm() +"\" id=\"" + vo.getId() + "\" im0=\"leaf.gif\" im1=\"folderOpen.gif\" im2=\"folderClosed.gif\">");
}
level = Integer.parseInt(vo.getLevel()+"");
}
for(int j =0; j <level; j++)
dataout.write("</item>\r\n");
dataout.write("</tree>");
dataout.flush();
dataout.close();
out.close();
}

(2) json
@Test public void testMenuJSON() throws Exception {
java.util.Collection<MenuInfo> menuInfoList = mobileDao.testPrivilege();
java.io.OutputStream out = null;
java.io.OutputStreamWriter dataout = null;
out = new java.io.FileOutputStream("bbb.JSON");
dataout = new java.io.OutputStreamWriter(out, "UTF-8");
dataout.write("{id:'0' ");
int level=0;
for(MenuInfo vo : menuInfoList)
{
if(level == Integer.parseInt(vo.getLevel()+"")) {
dataout.write("}\r\n");
final String chk = "a".equals(vo.getHasit())?"checked:'1', ":"";
dataout.write(",{" + "id:'" + vo.getId()+ "', " + chk + "text:'"+ vo.getMenunm() + "', im0:'leaf.gif', im1:'folderOpen.gif', im2:'folderClosed.gif'");

}
else if(Integer.parseInt(vo.getLevel()+"") > level) {
final String chk = "a".equals(vo.getHasit())?"checked:'1', ":"";
dataout.write(", item:[{" + "id:'" + vo.getId()+ "', " + chk + "text:'"+ vo.getMenunm() + "', im0:'leaf.gif', im1:'folderOpen.gif', im2:'folderClosed.gif'");
}
else {
for(int i =0; i < level - Integer.parseInt(vo.getLevel()+""); i++)
dataout.append("}\r\n]\r\n");
final String chk = "a".equals(vo.getHasit())?"checked:'1', ":"";
dataout.write("}\r\n,{" + "id:'" + vo.getId()+ "', " + chk + "text:'"+ vo.getMenunm() + "', im0:'leaf.gif', im1:'folderOpen.gif', im2:'folderClosed.gif'");
}
level = Integer.parseInt(vo.getLevel()+"");
}
for(int j =0; j <level; j++)
dataout.write("}]\r\n");
dataout.write("}");
dataout.flush();
dataout.close();
out.close();
}

3. 我对比了一下,若不使用专业版的dhtmltree的情况下,都加载大数据,使用xml和json的速度一样慢,几乎没有区别。主要还是渲染生成树的计算太复杂,若使用专业版(太贵了,都要800多美元),并把它的 tree.enableSmartXMLParsing(true) 因为它在你查看该节点时才加载它的子节点,所以树能很快显示,它还有一点做的特别好就是在树的有些节点没有被加载的情况下,通过tree.getAllChecked()也能够获取到这些节点。
  • b.rar (114.3 KB)
  • 下载次数: 487
分享到:
评论
2 楼 yueliangwolf 2010-07-12  
这个dhtmlxtree.js里面有enableSmartXMLParsing(true)这个方法吗?有的话发我一个,找了好多,都没有这个方法...
1 楼 filix 2009-02-07  
谢谢分享!看了真是豁然开朗!

相关推荐

Global site tag (gtag.js) - Google Analytics