使用spring mvc 注解 异步加载一棵树
jsp:
<ul id="orgInfoTree"></ul>
$(function(){ loadOrgTree(); }); function loadOrgTree(){ $('#orgInfoTree').tree({ checkbox: true, lines:true, url: '<%=request.getContextPath()%>/FNCG_PD_QRY/loadOrgTree', 这是第一次加载树的url 加载根节点 onBeforeExpand:function(node){ $("#orgInfoTree").tree('options').url='<%=request.getContextPath()%>/FNCG_PD_QRY/loadOrgChildrenTree?FID='+node.id; 这是点击根节点的时候发送请求去加载子节点 }, onClick:function(node){ $("#orgInfoTree").tree('options').url='<%=request.getContextPath()%>/FNCG_PD_QRY/loadOrgChildrenTree?FID='+node.id; } }); }
java代码部分
controller部分:
@RequestMapping("/loadOrgTree") @ResponseBody public String loadOrgTree(){ List<OrgTree> OTList = fncg_PD_QRY_Service.loadOrgTree(); StringBuffer sb = new StringBuffer(); sb.append("["); for (OrgTree orgTree : OTList) { sb.append("{\"id\":" + orgTree.getORGN_CODE() + " ,\"text\":" + "\"" + orgTree.getORGN_NAME() + "\"" + " ,\"state\":" + " \"closed\" " + " ,\"attributes\":" + "\"" + orgTree.getORGN_LVL() + "\"" + "},"); } sb.deleteCharAt(sb.length() - 1); sb.append("]"); return sb.toString(); } @RequestMapping("/loadOrgChildrenTree") @ResponseBody public String loadOrgChildrenTree(String FID){ List<OrgTree> OTList = fncg_PD_QRY_Service.loadOrgChildrenTree(FID); StringBuffer sb = new StringBuffer(); sb.append("["); for (OrgTree orgTree : OTList) { sb.append("{\"id\":" + orgTree.getORGN_CODE() + " ,\"text\":" + "\"" + orgTree.getORGN_NAME() + "\"" + " ,\"state\":" + " \"closed\" " + " ,\"attributes\":" + "\"" + orgTree.getORGN_LVL() + "\"" + "},"); } sb.deleteCharAt(sb.length() - 1); sb.append("]"); return sb.toString(); }
service部分:
public List<OrgTree> loadOrgTree(){ return fncg_PD_QRY_Dao.loadOrgTree(); } public List<OrgTree> loadOrgChildrenTree(String FID){ return fncg_PD_QRY_Dao.loadOrgChildrenTree(FID); }
dao部分:
public List<OrgTree> loadOrgTree(); public List<OrgTree> loadOrgChildrenTree(@Param("FID") String FID);
xml,接口对应的sql部分
<select id="loadOrgTree" resultMap="com.cvicin.products.productManage.fngg.mapper.poRes.OrgTree"> SELECT B.ID AS ID,B.ORGN_CODE AS ORGN_CODE,B.F_ORGN_ID AS F_ORGN_ID,B.ORGN_NAME AS ORGN_NAME,B.ORGN_LVL AS ORGN_LVL FROM ORGN_BASIC B WHERE B.ORGN_LVL = (SELECT min(A.ORGN_LVL) FROM ORGN_BASIC A) </select> <select id="loadOrgChildrenTree" parameterType="java.lang.String" resultMap="com.cvicin.products.productManage.fngg.mapper.poRes.OrgTree"> SELECT B.ID AS ID,B.ORGN_CODE AS ORGN_CODE,B.F_ORGN_ID AS F_ORGN_ID,B.ORGN_NAME AS ORGN_NAME,B.ORGN_LVL AS ORGN_LVL FROM ORGN_BASIC B WHERE B.F_ORGN_ID = #{FID,jdbcType=VARCHAR} </select>
javaBean和对应的xml:
public class OrgTree extends BasePO{ private int ID; private String ORGN_CODE; private String F_ORGN_ID; private String ORGN_NAME; private String ORGN_LVL; public int getID() { return ID; } public void setID(int iD) { ID = iD; } public String getORGN_CODE() { return ORGN_CODE; } public void setORGN_CODE(String oRGN_CODE) { ORGN_CODE = oRGN_CODE; } public String getF_ORGN_ID() { return F_ORGN_ID; } public void setF_ORGN_ID(String f_ORGN_ID) { F_ORGN_ID = f_ORGN_ID; } public String getORGN_NAME() { return ORGN_NAME; } public void setORGN_NAME(String oRGN_NAME) { ORGN_NAME = oRGN_NAME; } public String getORGN_LVL() { return ORGN_LVL; } public void setORGN_LVL(String oRGN_LVL) { ORGN_LVL = oRGN_LVL; } }
<resultMap id="OrgTree" type="com.cvicin.products.productManage.OrgTree"> <result property="ID" column="ID" /> <result property="ORGN_CODE" column="ORGN_CODE" /> <result property="F_ORGN_ID" column="F_ORGN_ID" /> <result property="ORGN_NAME" column="ORGN_NAME" /> <result property="ORGN_LVL" column="ORGN_LVL" /> </resultMap>
一颗无刷新的,异步加载的tree