侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

ajax请求后台得到json数据后动态生成树形下拉框的方法

前端  /  管理员 发布于 2年前   360

如下所示:

<select id="cc" class="easyui-combotree" style="width:580px;" name="rempId" data-options="required:true"></select>

<script>$(function(){$.ajax({url:"departmentAction_getAllDep.action",type:"post",success:function(result){//console.log(result);$("#cc").combotree('loadData',b1(result));}});$("#cc").combotree({animate:true,//选择树节点触发事件  onSelect : function(node) {  n = node;  //返回树对象   var tree = $(this).tree;   //选中的节点是否为叶子节点,如果不是叶子节点,清除选中   var isLeaf = tree('isLeaf', node.target);   if (!isLeaf) {    //清除选中    $("#cc").combotree('clear');   }  } });});var tree = {id:'',  text:'',  state:'',  checked:'',  iconCls:'', attributes:'',  children:''}function b1(result){var t = [];$.each(result,function(index,dept){t[index] = b2(dept);});return t;}function b2(dept){  var tree = new Object();tree.id = dept.depId;  tree.text = dept.depName;  tree.state = 'closed';  tree.checked = 'false'; if(dept.employees.length != 0){ tree.children = b3(dept.employees); }else{ tree.children = []; } return tree;}function b3(employees){  var easyTree = [];  $.each(employees,function(index,item){  easyTree[index] = b4(item);  });  return easyTree; }  function b4(item){var tree = new Object();tree.id = item.empId;tree.text = item.empName;if(item.empSex == "男"){tree.iconCls = 'icon-nan';}else{tree.iconCls = 'icon-female';}return tree;} </script>

department表中的dept_id作为employee表中有的外键,生成的Department.java类中有Set<employee>对象。从后台查询部门表,得到List<Department>集合,通过struts2配置:

<action name="departmentAction_*" class="com.chinasoft.action.DepartmentAction" method="{1}"><result name="getAllDep" type="json"><param name="root">list</param></result></action>

转成json格式后,传到jsp页面,在前台页面中处理json数据,动态生成下拉树。

以上这篇ajax请求后台得到json数据后动态生成树形下拉框的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。


  • 上一条:
    DIV+CSS实现的滑动门菜单特效代码
    下一条:
    ajax请求后台接口数据与返回值处理js的实例讲解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在js中mouseover和 mouseenter的区别浅析(0个评论)
    • uniapp调用手机实现打电话录音功能示例代码(0个评论)
    • 在uniapp开发微信小程序中图片大小显示不正常解决方法(0个评论)
    • 在vue2中如何实现v-model示例(0个评论)
    • js + Bolb实现文件流下载csv文件示例代码(0个评论)
    • 近期文章
    • 在Laravel应用程序如何减少代码重复编写(0个评论)
    • 在laravel项目中提高安全性方式推荐:CSP内容安全策略(0个评论)
    • 在go语言中从值中获取常量名称代码示例(0个评论)
    • 在go语言中如何通过名称获得结构字段和值代码示例(0个评论)
    • 在go语言中用JQuery + html2canvas实现拍摄浏览器的屏幕截图示例(0个评论)
    • 人生感悟分享:讲一个大学毕业生到社畜老狗的蜕变心路历程(0个评论)
    • laravel9框架报错Target class... does not exist解决方式(0个评论)
    • Laravel 9.48版本发布(0个评论)
    • Meta高级工程师现身说法:程序员干得越久,代码写得越少?(0个评论)
    • 本站zongscan祝大家除夕快乐,2023有奔头(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-03
    • 2018-04
    • 2018-05
    • 2018-06
    • 2018-09
    • 2018-11
    • 2018-12
    • 2019-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-04
    • 2021-05
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客