首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

jquery easyUI tree 后盾实现代码

2012-08-28 
jquery easyUI tree 后台实现代码根据demojquery easyUI tree的树形格式的json形式为[{?id:1,?text:F

jquery easyUI tree 后台实现代码

根据demo

jquery easyUI tree的树形格式的json形式为

[{
?"id":1,
?"text":"Folder1",
?"iconCls":"icon-ok",
?"children":[{
??"id":2,
??"text":"File1",
??"checked":true
?},{
??"id":3,
??"text":"Folder2",
??"state":"open",
??"children":[{
???"id":4,
???"text":"File3",
???"attributes":{
????"p1":"value1",
????"p2":"value2"
???},
???"checked":true,
???"iconCls":"icon-reload"
??},{
???"id": 8,
???"text":"Async Folder",
???"state":"closed"
??}]
?}]
},{
?"text":"Languages",
?"state":"closed",
?"children":[{
??"id":"j1",
??"text":"Java"
?},{
??"id":"j2",
??"text":"C#"
?}]
}]

前台调用代码:

<%@ page language="java" contentType="text/html;charset=UTF-8"
?errorPage=""%>
?<%@ include file="/admin/CommonFiles/struts.jsp" %>
<html>
?<head>
??<title>信息发布平台</title>
??<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
??<link rel="stylesheet" type="text/css" href="${ctx}/admin/css/easyui.css"></link>
??<script type="text/javascript" src="${ctx}/admin/js/jquery-1.4.2.js"></script>
??<script type="text/javascript" src="${ctx}/admin/js/jquery.easyui.min.js"></script>
??<link rel="stylesheet" href="${ctx}/admin/style.css" type="text/css">
??<script type="text/javascript">
??
??$(function(){
???$('#tt2').tree({
????checkbox: true,
????url: '${ctx}/ItemTree.do?method=createTree',
????onClick:function(node){
?????$(this).tree('toggle', node.target);
?????var ss=node.attributes.cas.toString();//p1
?????alert('you click '+ss);
????},
????onContextMenu: function(e, node){
?????e.preventDefault();
?????$('#tt2').tree('select', node.target);
?????$('#mm').menu('show', {
??????left: e.pageX,
??????top: e.pageY
?????});
????}
???});
??});
??
??</script>
?</head>
?<body bgcolor="#EBF1FA" leftmargin="5" topmargin="5" marginwidth="0"
??marginheight="0">
???<ul id="tt2"></ul>
?</body>
</html>

后台服务端的递归代码(遍历每一个节点):

/**
? * @param mapping
? * @param form
? * @param request
? * @param response
? * @return
? * @throws Exception
? */
?public ActionForward createTree(ActionMapping mapping, ActionForm form,
???HttpServletRequest request, HttpServletResponse response)
???throws Exception {
??m_objsa = (SessionAdmin) request.getSession().getAttribute("amUser");
??int parentid=NumberUtils.stringToInt(request.getParameter("xxxx"));
??String state="";
??String sel="";
??
??List<TreeDto> list = new ArrayList<TreeDto>();
??
??createTree(parentid,state,sel,list);
??
??
??String str =JSONArray.fromObject(list).toString();
??

??//返回结果到前台
??response.getWriter().write(str);
??response.getWriter().flush();
??response.getWriter().close();
??
??return null;
??
?}
?//显示树型主函数,递归,选择所属栏目用,sel已选中的ID,curitem当前栏目ID
?//添加内容的时候,选择所属栏目
?public List<TreeDto> createTree(int parentid,String state,String sel,List<TreeDto> list2) throws SQLException
?{
??
//??list2=new ArrayList<TreeDto>();
??ArrayList<TreeDto> al = getPrivateSubItem(parentid);
??if(al != null && !al.isEmpty())
??{
???int n = al.size();
???for(int i = 0; i < n;i++)
???{
????TreeDto o = (TreeDto)al.get(i);
????
????//不包含子菜单
????if(!isHaveSubItem(o.getId()))
????{
????}else{
?????List<TreeDto> list333=new ArrayList<TreeDto>();
?????o.setChildren(createTree(o.getId(), state, sel,list333));
????}
????
????list2.add(o);
????
???}
??}
??
??return al;
?}

//检索

private ArrayList<TreeDto> getPrivateSubItem(int parentid) throws SQLException
?{
??m_strSql = new StringBuffer();
??m_strSql.append("Select ID,Name From Item Where IsAuditing = 1");
??m_strSql.append("And WebID = " + m_objsa.getWebID());
//??if(!m_objsa.isWebSuperAdmin())
//???m_strSql.append(" And '" + m_objsa.getItemPower() + "' Like '%' + CONVERT(varchar,ID) + '_0%'");
??m_strSql.append(" And ParentID = " + parentid);
??
??
??Connection conn = null;
??PreparedStatement stmt = null;
??ResultSet rs = null;?
??ArrayList<TreeDto> al = new ArrayList<TreeDto>();
??try {
???conn = this.getSqlSession().getConnection();
???stmt = conn.prepareStatement(m_strSql.toString());
???rs = stmt.executeQuery();
???
???if(rs!=null){
????while(rs.next())
????{
?????TreeDto o = new TreeDto();
?????o.setId(rs.getInt("ID"));
?????o.setText(rs.getString("Name"));
?????al.add(o);
????}
???}
???

??}catch(Exception se){
???se.printStackTrace();
??}finally{
???DBUtils.close(rs, stmt, conn);
??}
??
??return al;
?}

//判断是否具有子节点

private boolean isHaveSubItem(int parentid) throws SQLException
?{
??m_strSql = new StringBuffer();
??m_strSql.append("Select Count(ID) as C From Item Where IsAuditing = 1 And WebID = " + m_objsa.getWebID());
??m_strSql.append(" And ParentID = " + parentid);
??
??Connection conn = null;
??PreparedStatement stmt = null;
??ResultSet rs = null;?
??
??try {
???conn = this.getSqlSession().getConnection();
???stmt = conn.prepareStatement(m_strSql.toString());
???rs = stmt.executeQuery();
???
???if(rs.next() && rs != null)
???{
????int i = rs.getInt("C");
????rs.close();
????return i > 0;
???}

??}catch(Exception se){
???se.printStackTrace();
??}finally{
???DBUtils.close(rs, stmt, conn);
??}
??
??return false;??
?}

热点排行