BootStrap Jstree 树形菜单的增删改查的实现源码

时间:2021-05-20

1.首先需下载jstree的插件点击打开链接

2.在页面引入插件js文件css文件

<link rel="stylesheet" href="plugins/jstree/themes/classic/style.css" rel="external nofollow" > <script type="text/javascript" src="plugins/jstree/_lib/jquery.js"></script> <script type="text/javascript" src="plugins/jstree/_lib/jquery.cookie.js"></script> <script type="text/javascript" src="plugins/jstree/_lib/jquery.hotkeys.js"></script> <script type="text/javascript" src="plugins/jstree/jquery.jstree.js"></script>

3.初始化控件

1)html

<div id="demo2" class="demo" style="height:100px;"></div>

2)js 使用 demo2来初始化树形控件

<script type="text/javascript" class="source below"> $(function() { $("#demo2").jstree( { "json_data" : { "ajax" : { "url" : "http://localhost:8080/MemberManager/DepartmentTreeJson", "data" : function(n) { // the result is fed to the AJAX request `data` option return { "operation" : "get_children", "id" : n.attr ? n .attr( "id") .replace( "node_", "") : 1 }; } } }, "plugins" : [ "themes", "json_data", "ui", "crrm", "contextmenu", "search" ], }) .bind("loaded.jstree", function(event, data) { }) .bind( "select_node.jstree", function(event, data) { if (data.rslt.obj .attr("id") != undefined) { } }) .bind( "remove.jstree", function(e, data) { data.rslt.obj.each(function() { $.ajax({ async : false, type : 'POST', url : "http://localhost:8080/MemberManager/CreateNodeForDepartment", data : { "operation" : "remove_node", "id" : this.id.replace("node_", "") }, success : function(r) { if (!r.status) { data.inst.refresh(); } } }); }); }) .bind( "remove.jstree", function(e, data) { data.rslt.obj.each(function() { $.ajax({ async : false, type : 'POST', url : "http://localhost:8080/MemberManager/CreateNodeForDepartment", data : { "operation" : "remove_node", "id" : this.id .replace( "node_", "") }, success : function( r) { if (!r.status) { data.inst.refresh(); } } }); }); }) .bind( "create.jstree", function(e, data) { $.post( "http://localhost:8080/MemberManager/CreateNodeForDepartment", { "operation" : "create_node", "id" : data.rslt.parent .attr( "id") .replace( "node_", ""), "position" : data.rslt.position, "title" : data.rslt.name, "type" : data.rslt.obj .attr("rel") }, function(r) { if (r.status) { $(data.rslt.obj).attr("id", "node_" + r.id); } else { data.inst.refresh(); $.jstree.rollback(data.rlbk); } }); }) .bind( "rename.jstree", function(e, data) { $.post( "http://localhost:8080/MemberManager/CreateNodeForDepartment", { "operation" : "rename_node", "id" : data.rslt.obj .attr( "id") .replace( "node_", ""), "title" : data.rslt.new_name }, function(r) { if (!r.status) { data.inst.refresh(); $.jstree.rollback(data.rlbk); } }); }) // 1) the loaded event fires as soon as data is parsed and inserted // 2) but if you are using the cookie plugin or the core `initially_open` option: .one("reopen.jstree", function(event, data) { }) // 3) but if you are using the cookie plugin or the UI `initially_select` option: .one("reselect.jstree", function(event, data) { }); }); </script> </pre>4.代码解析<p></p><p><pre name="code" class="java"> import java.util.List; public class Department { // 部门id private String departmentid; // 部门名称 private String name; // 父级部门ID private String parentid; //同级之间的排序。排序id 小的排前面 private String orders; //子节点 private List<Department> childNode; public List<Department> getChildNode() { return childNode; } public void setChildNode(List<Department> childNode) { this.childNode = childNode; } public String getDepartmentid() { return departmentid; } public void setDepartmentid(String departmentid) { this.departmentid = departmentid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getParentid() { return parentid; } public void setParentid(String parentid) { this.parentid = parentid; } public String getOrders() { return orders; } public void setOrders(String orders) { this.orders = orders; } @Override public String toString(){ return "[departmentID:"+this.departmentid+ "departmentName:"+this.name+ "parentID:"+this.parentid+"orders:"+this.orders+"]"; } }

4.代码解析

插件初始化我这里使用了插件的两个参数json_data,以及plugins注意看代码结构

4.1上图两个部分即初始化部分重点解释下plugins这个参数是jstree整合插件的地方theme表示主题,json_data将上文定义的json_data也就

是Ajax从后要获取json数据返回到前台页面。contextmenu,是鼠标右键在树形节点上会弹出增删改查的菜单。

4.2 json数据的格式

先看展示

这是一个可以无限极下分的菜单,我们可以根据上图的目录结构对照下面的json数据结构来看,这样会更清晰。

{"data":"软件及数据","attr":{"id":"e59365b9-7b2a-43a3-b10a-cfe03d5eb004"}, "children":[ {"data":"创新组","attr":{"id":"73919359-2a1b-4ee7-bcc2-56949e8560e8"}, "children":[ {"data":"大数据","attr":{"id":"a7ea6a0f-0b78-4064-803b-f2e0a95d914f"}, "children":[ {"data":"研发","attr":{"id":"fc20e438-e7b9-4cca-be6a-49965daab528"},"children":[]}]}]}, {"data":"项目管理","attr":{"id":"e1bdae71-6cfb-4e8c-ab29-a3eb03b9961d"},"children":[]} ] },

4.4组装json数据,我使用的是首先查找到所有的父节点即parentid=1的时候,然后递归将所有的子节点加到List<chiledren>对象里面,紧接着再通过循环递归组装无限极菜单json数据下面看数据库数据结构

import java.util.List; public class Department { // 部门id private String departmentid; // 部门名称 private String name; // 父级部门ID private String parentid; //同级之间的排序。排序id 小的排前面 private String orders; //子节点 private List<Department> childNode; public List<Department> getChildNode() { return childNode; } public void setChildNode(List<Department> childNode) { this.childNode = childNode; } public String getDepartmentid() { return departmentid; } public void setDepartmentid(String departmentid) { this.departmentid = departmentid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getParentid() { return parentid; } public void setParentid(String parentid) { this.parentid = parentid; } public String getOrders() { return orders; } public void setOrders(String orders) { this.orders = orders; } @Override public String toString(){ return "[departmentID:"+this.departmentid+ "departmentName:"+this.name+ "parentID:"+this.parentid+"orders:"+this.orders+"]"; } }

4.5 此处servlet为客户端提供jstree的json_data。就是在初始化控件时候有ajax调用这个servlet获取json数据并返回给json_data

import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.goldwind..goldwind.www.serviceimpl.DepartmentServiceImpl; public class CreateNodeForDepartment extends HttpServlet { private static final long serialVersionUID = 1L; public CreateNodeForDepartment() { super(); } @Override public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); DepartmentService deptService=new DepartmentServiceImpl(); if("rename_node".equals(request.getParameter("operation"))){ String id=request.getParameter("id"); String title=request.getParameter("title"); Department dept=new Department(); dept.setDepartmentid(id); deptService.modifyDepartment(dept, title); }else if("create_node".equals(request.getParameter("operation"))){ String id=request.getParameter("id"); String title=request.getParameter("title"); Department dept=new Department(); dept.setDepartmentid(UUID.randomUUID().toString()); dept.setName(title); dept.setParentid(id); deptService.insertDepartment(dept); }else if("remove_node".equals(request.getParameter("operation"))){ String id=request.getParameter("id"); Department dept=new Department(); dept.setDepartmentid(id); deptService.deleteDepartment(dept); } out.flush(); out.close(); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } /** * Initialization of the servlet. <br> * * @throws ServletException * if an error occurs */ @Override public void init() throws ServletException { // Put your code here } }

好了这就完成了,当然这里面的树也是可以自定义图标,自定义按钮等操作,具体可以自己去探究。

以上所述是小编给大家介绍的BootStrap Jstree 树形菜单的增删改查的实现源码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章