使用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()也能夠獲取到這些節點。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章