前台jquery+ajax请求往页面上添加树形的js代码

  1. //传入当前点击节点的id,在后台代码查询出parentid=当前节点id的记录数,从而实现点击当前节点,往后台发送ajax请求,查询出子节点的集合,往父节点下拼接页面
  2. function treeNode(pid){
  3.  
  4. //如果<li id="pid">标签下的<ul>的长度为1,则说明需要发送ajax请求,往其中添加子节点。如果长度大于1说明添加过了,不用再次发送ajax请求。直接进else中控制样式的显示和隐藏问题
  5. if($("#"+pid).find("ul").length <= 1){
  6. $.ax({
  7. type:"post",
  8. url:"<%=request.getContextPath() %>/master/sysGroup_queryFlorGroup.action",
  9. async:false,
  10. data:{"sysParentId":pid},
  11. dataType:"json",
  12. success:function(resp){
  13. //从后台响应回来数据,获取所有的组信息的json格式的数据
  14. var groups = resp["rows"];
  15.  
  16. //如果查询出来组信息的json数组的长度《=0为空,则说明该节点下,无自己点,不用进行拼接。
  17. if(groups.length>0){
  18. //遍历json数组的信息。拼接页面
  19. for(var i=0;i<groups.length;i++){
  20. var currentId = groups[i].sysGroupId;
  21. //判断子节点是否还有子节点,后台封装数据的时候,封装了一个状态码
  22. if(groups[i].hasChild == "1"){
  23. //pid等于零,是父节点,其余都是父节点下的子节点
  24. if("0" == pid){//第一次添加父节点
  25. $("#firstFlorGroup").append("<li id='"+currentId+"'><input type='checkbox' name='box' value='"+currentId+"' /><a onclick=\"treeNode
  26.  
  27. ('"+currentId+"')\" id='a"+currentId+"'>"+groups[i].sysGroupName+"</a></li>");
  28. $("#"+currentId).append("<div class=\"opt\"><a href='javascript:void(0)' onclick=\"updateGroup('"+groups[i].sysGroupId+"');\">编辑
  29.  
  30. </a>|<a href='javascript:void(0)' onclick=\"deleteGroup('"+groups[i].sysGroupId+"');\">删除</a></div><div class=\"opt\">"+groups[i].sysUpdateTime+"</div>");
  31. }else{
  32. $("#u"+pid).append("<li id='"+currentId+"'><input type='checkbox' name='box' value='"+currentId+"' /><a onclick=\"treeNode
  33.  
  34. ('"+currentId+"')\" id='a"+currentId+"'>"+groups[i].sysGroupName+"</a></li>");
  35. $("#"+currentId).append("<div class=\"opt\"><a href='javascript:void(0)' onclick=\"updateGroup('"+groups[i].sysGroupId+"');\">编辑
  36.  
  37. </a>|<a href='javascript:void(0)' onclick=\"deleteGroup('"+groups[i].sysGroupId+"');\">删除</a></div><div class=\"opt\">"+groups[i].sysUpdateTime+"</div>");
  38. }
  39. //给编辑的超链接添加伪协议
  40. $("#a"+currentId).attr("href","javascript:void(0)");
  41. //给还有子节点的子节点设置样式,使其变成绿色显示。
  42. $("#a"+currentId).attr("style","color: green;");
  43. //既然有子节点,就需要往<li>标签下,添加<ul>标签,方便添加子节点的子节点
  44. $("#"+currentId).append("<ul id='u"+currentId+"'></ul>");
  45. }else{
  46. //无子节点
  47. if("0" == pid){
  48. $("#firstFlorGroup").append("<li id='"+currentId+"'><input type='checkbox' name='box' value='"+currentId+"' />"+groups
  49.  
  50. [i].sysGroupName+"<div class=\"opt\"><a href='javascript:void(0)' onclick=\"updateGroup('"+groups[i].sysGroupId+"');\">编辑</a>|<a href='javascript:void(0)' onclick=\"deleteGroup('"+groups
  51.  
  52. [i].sysGroupId+"');\">删除</a></div><div class=\"opt\">"+groups[i].sysUpdateTime+"</div></li>");
  53. }else{
  54. $("#u"+pid).append("<li id='"+currentId+"'><input type='checkbox' name='box' value='"+currentId+"' />"+groups[i].sysGroupName+"<div
  55.  
  56. class=\"opt\"><a href='javascript:void(0)' onclick=\"updateGroup('"+groups[i].sysGroupId+"');\">编辑</a>|<a href='javascript:void(0)' onclick=\"deleteGroup('"+groups[i].sysGroupId+"');\">删除
  57.  
  58. </a></div><div class=\"opt\">"+groups[i].sysUpdateTime+"</div></li>");
  59. $("#"+currentId).append("<ul></ul>");
  60. }
  61. }
  62. }
  63. }
  64. }
  65. });
  66. }else{
  67. //当不需要发送ajax请求的时候,点击的时候都是改变其隐藏和显示的样式,实现动态效果
  68. if($("#"+pid).find("ul").css("display")=="block"){
  69. $("#"+pid).find("ul").css("display","none");
  70. } else {
  71. $("#"+pid).find("ul").css("display","block");
  72. }
  73. }
  74. }

ajax请求后台的action

  1. public String queryFlorGroup(){
  2. try{
  3. //生成一个装map的list集合
  4. List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
  5. //查询出指定父id的权限集合
  6. List<SysGroup> list = sysGroupService.queryByPId(parentId);
  7. //生成一个事件格式的对象
  8. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  9. String updateTime = "";
  10. //遍历查询出来的权限集合,进行包装数据
  11. for(SysGroup sysGroup:list){
  12. int hasChild = 0;
  13. //统计【遍历出来的权限是否有子权限】
  14. int num = sysGroupService.countChilds(sysGroup.getSysGroupId());
  15. if(num>0){
  16. hasChild = 1;
  17. }
  18. updateTime = sdf.format(sysGroup.getSysUpdateTime());
  19. Map<String,Object> map = new HashMap<String, Object>();
  20. map.put("hasChild", hasChild);
  21. map.put("sysGroupId", sysGroup.getSysGroupId());
  22. map.put("sysGroupName", sysGroup.getSysGroupName());
  23. map.put("parentId", sysGroup.getSysParentId());
  24. map.put("sysUpdateTime", updateTime);
  25. listMap.add(map);
  26. }
  27. jsonObject.put("rows", listMap);
  28. System.out.println(jsonObject);
  29. }catch(Exception e){
  30. e.printStackTrace();
  31. }finally{
  32. out.print(jsonObject);
  33. out.close();
  34. }
  35. return null;
  36. }

通过自己的id,是别人的父id,通过ajax请求+css样式,动态往页面上添加树形。

js+html代码

  1. //加载用户组
  2. function treeNode(pid){
  3. //判断该节点下是否已经加载了子节点。如果加载,则不执行ajax请求,而是执行else{}中样式的显示和隐藏
  4. if($("#"+pid).find("ul").length == 0){
  5. $.ax({
  6. type:"post",
  7. url:"<%=request.getContextPath() %>/master/sysGroup_queryFlorGroup.action",
  8. async:false,
  9. data:{"parentId":pid},
  10. dataType:"json",
  11. success:function(resp){
  12. var groups = resp["rows"];
  13. if(groups.length>0){
  14. //如果响应回来的包装有组信息的json数组长度大于零,则遍历
  15. for(var i=0;i<groups.length;i++){
  16. currentId = groups[i].sysGroupId;//组的id
  17. //如果父id不等于0,说明是二级或二级以下的节点
  18. if("0" != pid){
  19. //添加一个ul标签,用来装响应回来的json数据的组信息
  20. $("#"+pid).append("<ul id='u"+pid+"'></ul>");
  21. }
  22. //如果该json数据中的组信息,显示其有子节点。添加的时候,同时给节点绑定onclick事件,用于自身调用自身方法,再次发送ajax请求,加载子节点
  23. if(groups[i].hasChild == "1"){
  24. if("0" == pid){
  25. $("#groupTree").append("<li id='"+currentId+"'><a onclick=\"treeNode('"+currentId+"')\" id='a"+currentId+"'><cite></cite></a><a href='<%=request.getContextPath() %>/master/sysGroupRolePower_queryRolesByGroupId.action?groupId="+currentId+"' target='topFrame'>"+groups[i].sysGroupName+"</a><i></i></li>");
  26. }else{
  27. $("#u"+pid).append("<li id='"+currentId+"'><a onclick=\"treeNode('"+currentId+"')\" id='a"+currentId+"'><cite></cite></a><a href='<%=request.getContextPath() %>/master/sysGroupRolePower_queryRolesByGroupId.action?groupId="+currentId+"' target='topFrame'>"+groups[i].sysGroupName+"</a><i></i></li>");
  28. }
  29. $("#a"+currentId).attr("href","javascript:void(0)");
  30. $("#a"+currentId).attr("style","color: green;");
  31.  
  32. }else{
  33. //显示没有子节点,添加节点时,这里边有一个图标的不同,遮盖总的css样式。不在绑定onclick事件。
  34. if("0" == pid){
  35. //初始位置添加
  36. $("#groupTree").append("<li id='"+currentId+"'><cite></cite><a href='<%=request.getContextPath() %>/master/sysGroupRolePower_queryRolesByGroupId.action?groupId="+currentId+"' target='topFrame'>"+groups[i].sysGroupName+"</a><i></i></li>");
  37. $("#"+currentId).children("cite").css("background","url(../images/rlist.gif) no-repeat");
  38. }else{
  39. //动态添加完成的节点下,添加子节点
  40. $("#u"+pid).append("<li id='"+currentId+"'><cite></cite><a href='<%=request.getContextPath() %>/master/sysGroupRolePower_queryRolesByGroupId.action?groupId="+currentId+"' target='topFrame'>"+groups[i].sysGroupName+"</a><i></i></li>");
  41. $("#"+currentId).children("cite").css("background","url(../images/rlist.gif) no-repeat");
  42. }
  43. }
  44. }
  45. }
  46. }
  47. });
  48.  
  49. }else{
  50. //当树形加载完毕后,再次点击,就是控制显示和隐藏,从而实现动态效果
  51. if($("#"+pid).find("ul").css("display")=="block"){
  52. $("#"+pid).find("ul").css("display","none");
  53. $("#a"+pid).children("cite").css("background","url(../images/list.gif) no-repeat");
  54. } else {
  55. $("#"+pid).find("ul").css("display","block");
  56. $("#a"+pid).children("cite").css("background","url(../images/clist.png) no-repeat");
  57. }
  58. }
  59. }
  60.  
  61. //当页面加载完毕时,首先自动加载父id为0的组的信息,动态添加到页面上
  62. $(document).ready(function(){
  63. treeNode(0);
  64. });
  65.  
  66. <body style="background:#f0f9fd;">
  67. <div class="lefttop"><span></span>权限分配</div>
  68.  
  69. <dl class="leftmenu">
  70.  
  71. <dd><div class="title"><span><img src="<%=request.getContextPath() %>/master/images/leftico04.png" /></span>用户组</div>
  72. <ul id="groupTree" class="menuson">
  73. </ul>
  74.  
  75. </dd>
  76.  
  77. </dl>
  78.  
  79. <script type="text/javascript">
  80. $('.tablelist tbody tr:odd').addClass('odd');
  81. </script>
  82.  
  83. </body>

ajax请求后台的action

  1. /**
  2. * 根据自动注入的parentId来查出子组,并且返回所查出的子组是否还有子组
  3. * @Title: queryFlorGroup
  4. * @Description: TODO(这里用一句话描述这个方法的作用)
  5. * @return
  6. * @return String 返回类型
  7. * @author 王兴兴
  8. * @date 2014-7-1 下午3:21:10
  9. */
  10.  
  11. public String queryFlorGroup(){
  12. try{
  13.  
  14. List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
  15.  
  16. List<SysGroup> list = sysGroupService.queryByPId(parentId);
  17.  
  18. for(SysGroup sysGroup:list){
  19. int hasChild = 0;
  20. //通过改组的组id,去后台查询改组下是否还有子组
  21. int num = sysGroupService.countChilds(sysGroup.getSysGroupId());
  22. if(num>0){
  23. hasChild = 1;
  24. }
  25. Map<String,Object> map = new HashMap<String, Object>();
  26. map.put("hasChild", hasChild);
  27. map.put("sysGroupId", sysGroup.getSysGroupId());
  28. map.put("sysGroupName", sysGroup.getSysGroupName());
  29. map.put("parentId", sysGroup.getSysParentId());
  30. map.put("sysUpdateTime", sysGroup.getSysUpdateTime());
  31. listMap.add(map);
  32. }
  33. jsonObject.put("rows", listMap);
  34. System.out.println(jsonObject);
  35. }catch(Exception e){
  36. e.printStackTrace();
  37. }finally{
  38. out.print(jsonObject);
  39. out.close();
  40. }
  41. return null;
  42. }
  43.  
  44. /**
  45. * 查询指定用户组下的子用户组个数
  46. * @Title: countChilds
  47. * @Description: TODO(这里用一句话描述这个方法的作用)
  48. * @param parentId
  49. * @return
  50. * @return Integer 返回类型
  51. * @author 王兴兴
  52. * @date 2014-6-30 下午4:39:10
  53. */
  54. public Integer countChilds(String parentId){
  55. return groupDao.count("sysParentId", parentId);
  56. }
  57.  
  58. /**
  59. * 统计
  60. * @Title: count
  61. * @Description: TODO(这里用一句话描述这个方法的作用)
  62. * @param conds
  63. * @return
  64. * @return Integer 返回类型
  65. * @author 郝鹏
  66. * @date 2014-3-27 下午3:22:18
  67. */
  68. public Integer count(String property,Object value){
  69. Map<String, Object> conds=new HashMap<String, Object>();
  70. conds.put(property, value);
  71. return this.count(conds);
  72. }
  73.  
  74. /**
  75. * 统计
  76. * @Title: count
  77. * @Description: TODO(这里用一句话描述这个方法的作用)
  78. * @param conds
  79. * @return
  80. * @return Integer 返回类型
  81. * @author 郝鹏
  82. * @date 2014-3-27 下午3:22:18
  83. */
  84. public Integer count(final Map<String, Object> conds){
  85. return this.hibernateTemplate.execute(new HibernateCallback<Integer>() {
  86.  
  87. public Integer doInHibernate(Session session)
  88. throws HibernateException, SQLException {
  89. if(conds==null || conds.isEmpty()){
  90. return null;
  91. }
  92. StringBuilder sb=new StringBuilder();
  93. sb.append("select count(*) from "+entityClass.getName()+" where ");
  94. //设置条件参数
  95. Set<String> condKeys=conds.keySet();
  96. int i=0;
  97. for(String key:condKeys){
  98. String k=key.replaceAll("\\.", "")+"w";
  99. sb.append(key+"=:"+k);
  100. if(i<condKeys.size()-1){
  101. sb.append(" and ");
  102. }
  103. i++;
  104. }
  105. Query q=session.createQuery(sb.toString());
  106.  
  107. for(String key:condKeys){
  108. String k=key.replaceAll("\\.", "")+"w";
  109. q.setParameter(k, conds.get(key));
  110. }
  111. Number number=(Number) q.uniqueResult();
  112. return number.intValue();
  113. }
  114.  
  115. });
  116. }

jquery,从后台查数据,给页面上添加树形。的更多相关文章

  1. WordPress怎么在页面上添加目录

    要实现的如下功能,在页面上添加一个文章目录: 步骤:   1)在wordpress中,在Posts----Categories中建立目录, 2) 3)add new post,指定post所属的cat ...

  2. Inno Setup技巧[界面]欢迎页面上添加文字

    原文:Inno Setup技巧[界面]欢迎页面上添加文字 本文介绍在"欢迎页面添加文字"的两种方法. 界面预览: Setup技巧[界面]欢迎页面上添加文字" title= ...

  3. 紧接上篇,jQuery调用jsonp,并且在页面上展示

    在上篇中提到了spring4.1+支持jsonp的调用,做了个例子,用来在页面上展示jsonp: (js写的丑了点,本人后端出生,前端大侠们轻拍~) var Menu = function () { ...

  4. jquery 获取后台实时数据

    第一步.提醒后台处理数据1.$.ajax({}) 提交数据,2.后台返回状态3.后台开始处理数据,并每秒记录状态到 data.json 文件4.前台每秒请求 data.json 文件,直到处理完成 第 ...

  5. JS从后台获取数据,前台动态添加tr标签中的td标签

    功能描述: 要求从后台查询该省份的所有城市,然后动态的再前台固定的tr标签中添加相应的td标签来展示城市基本信息: 文章目录 #一.前台jsp及js源码 jsp:在固定的tr标签中添加一个id,通过j ...

  6. 05 HTML字符串转换成jQuery对象、绑定数据到元素上

    1 要求 将一段 HTML脚本 封装成一个字符串,将这个字符串转换成一个jQuery对象:然后将这个jQuery对象添加到指定的元素中去 2 步骤 定义字符串 var str = '<div i ...

  7. vue从后台拿数据渲染页面图片

    <div class="list-content"> <div v-for="goods in goodsList" class=" ...

  8. 在h5页面上添加音乐播放

    接到需求说要做一个h5轮播图,同时配上背景音乐. Html部分: <!--音乐开始--> <div id="music"> <div id=" ...

  9. 在rabbitmq操作页面上添加队列、交换器及绑定示图

    1.添加队列 2.添加交换器 3.绑定

随机推荐

  1. PHP标准库SPL

    SPL是Standard PHP Library(PHP标准库)的缩写.用来解决典型(常见)问题(common problems)的一组接口与类的集合 典型问题(common problems) - ...

  2. 3.6 MIPS指令简介

    计算机组成 3 指令系统体系结构 3.6 MIPS指令简介 MIPS秉承着指令数量少,指令功能简单的设计理念.那这样的设计理念是如何实现的呢?在这一节,我们就将来分析MIPS指令的特点. 相比于X86 ...

  3. sgu 108 Self-numbers 2

    题意:这样的数有几个? 模仿筛法就能解出,但是内存不够.这就需要重复利用数组,用100大小的数组,所有的数对100取模.对于一个数,比如71,就在arr[78]=71记录下来.到78时,检查78-71 ...

  4. PC端,移动端分离,如何结合??

    <script type="text/javascript"> function mobile_device_detect(url) { var thisOS = na ...

  5. Nginx配置https, 80端口重定向443

    server { listen 443 ssl; server_name 域名; charset utf-8; access_log /var/log/nginx/webhook.iminho.me/ ...

  6. 『cs231n』循环神经网络RNN

    循环神经网络 循环神经网络介绍摘抄自莫凡博士的教程 序列数据 我们想象现在有一组序列数据 data 0,1,2,3. 在当预测 result0 的时候,我们基于的是 data0, 同样在预测其他数据的 ...

  7. OAF 动态创建组件以及动态绑定属性

    在开发中,我们遇到以下一个需求. 一个表格左侧有5列是固定存在的,右侧有N列是动态生成的,并且该N列中第一列可输入,第二列是不可编辑的,但是是数字,如果小于0,那么就要显示为红色,重点标识出来. 首先 ...

  8. 原生JS和jQuery版实现文件上传功能

    <!doctype html> <html lang="zh"> <head> <meta charset="utf-8&quo ...

  9. 小程序数组型图片自适应效果的实现(交流QQ群:604788754)

    //本例代码如有问题,请加群,下载今日日期文件,测试.(如对本例有疑问,也可加群咨询群主) WXML: <view class="imgbox"> <block ...

  10. restful 初探

    1.restful 是一种编程规范,能够实现现在丰富的客户端(安卓,ios,桌面等)平等的访问服务器提供的服务. 2.重要的是利用restful来设计实现 符合该编程规范的api.