1. ajax请求生成jsTree

  1. <span style="font-size:14px;"><script>
  2. var r = []; // 权限树中被选中的叶子节点
  3. var currentGroupId;
  4. function showPermitTree(id) {
  5. currentGroupId = id;
  6. $.ajax({
  7. data : "currentGroupId=" + id,
  8. type : "POST",
  9. //dataType : 'json',
  10. url : "/test/permittree",
  11. error : function(data) {
  12. alert("出错了!!:" + data);
  13. },
  14. success : function(data) {
  15. //alert("success:" + data);
  16. createPermitTree(data);
  17. }
  18. });
  19. ${'buttonDiv'}.style.display="";
  20. }
  21. function createPermitTree(datastr) {
  22. datastr = eval("" + datastr + "");
  23. $('#permitTree').jstree({
  24. 'plugins' : [ "wholerow", "checkbox", "types" ],
  25. 'core' : {
  26. "themes" : {
  27. "responsive" : false
  28. },
  29. 'data' : datastr
  30. },
  31. "types" : {
  32. "default" : {
  33. "icon" : "fa fa-folder icon-state-warning icon-lg"
  34. },
  35. "file" : {
  36. "icon" : "fa fa-file icon-state-warning icon-lg"
  37. }
  38. }
  39. });
  40. }
  41. // listen for event
  42. $('#permitTree').on('changed.jstree', function(e, data) {
  43. r = [];
  44. var i, j;
  45. for (i = 0, j = data.selected.length; i < j; i++) {
  46. var node = data.instance.get_node(data.selected[i]);
  47. if (data.instance.is_leaf(node)) {
  48. r.push(node.id);
  49. }
  50. }
  51. //alert('Selected: ' + r.join('@@'));
  52. })
  53. function saveTree() {
  54. $.ajax({
  55. data : {'currentGroupId' : currentGroupId,
  56. 'selectedNodes' : r.join('@@')},
  57. type : "POST",
  58. //dataType : 'json',
  59. url : "/test/savetree",
  60. error : function(data) {
  61. alert("出错了!!:" + data);
  62. },
  63. success : function(data) {
  64. alert("保存成功!");
  65. }
  66. });
  67. }
  68. </script></span><span style="font-size:24px;">
  69. </span>

直接把测试项目中一段代码copy过来了,这是一棵带复选框的树。页面有地方点击之后触发showPermitTree(id)函数,发送ajax请求给后台,项目使用的是springmvc框架,后台返回JSONArray.toString。

2. jsTree change事件

上面代码中含change事件。把所有选中的节点的id放到一个数组中。

页面上有个按钮,点击后触发saveTree函数,发请求给后台,把选中的节点的id发给后台。

3.jsTree自定义contextmenu

  1. <script>
  2. $('#jstree').jstree({
  3. core : {
  4. check_callback : true,
  5. data : [
  6. { "id" : "1", "parent" : "#", "text" : "root" },
  7. { "id" : "2", "parent" : "1", "text" : "child 1" },
  8. { "id" : "3", "parent" : "1", "text" : "child 2" }
  9. ],
  10. },
  11. plugins : ["wholerow","contextmenu"],
  12. "contextmenu": {
  13. "items": {
  14. "create": null,
  15. "rename": null,
  16. "remove": null,
  17. "ccp": null,
  18. "add": {
  19. "label": "add",
  20. "action": function (obj) {
  21. var inst = jQuery.jstree.reference(obj.reference);
  22. var clickedNode = inst.get_node(obj.reference);
  23. alert("add operation--clickedNode's id is:" + clickedNode.id);
  24. }
  25. },
  26. "delete": {
  27. "label": "delete",
  28. "action": function (obj) {
  29. var inst = jQuery.jstree.reference(obj.reference);
  30. var clickedNode = inst.get_node(obj.reference);
  31. alert("delete operation--clickedNode's id is:" + clickedNode.id);
  32. }
  33. }
  34. }
  35. }
  36. }).on("ready.jstree", function (e, data) {
  37. data.instance.open_all();
  38. });
  39. </script>

这段代码使用jsTree的contextmenu plugin,去掉jsTree自带的菜单,并自定义菜单

 
8

jsTree使用记录的更多相关文章

  1. jstree的基本应用----记录

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  2. 基于Metronic的Bootstrap开发框架经验总结(2)--列表分页处理和插件JSTree的使用

    在上篇<基于Metronic的Bootstrap开发框架经验总结(1)-框架总览及菜单模块的处理>介绍了Bootstrap开发框架的一些基础性概括,包括总体界面效果,以及布局.菜单等内容, ...

  3. 使用插件bootstrap-table实现表格记录的查询、分页、排序等处理

    在业务系统开发中,对表格记录的查询.分页.排序等处理是非常常见的,在Web开发中,可以采用很多功能强大的插件来满足要求,且能极大的提高开发效率,本随笔介绍这个bootstrap-table是一款非常有 ...

  4. 在Bootstrap开发框架中使用bootstrapTable表格插件和jstree树形列表插件时候,对树列表条件和查询条件的处理

    在我Boostrap框架中,很多地方需要使用bootstrapTable表格插件和jstree树形列表插件来共同构建一个比较常见的查询界面,bootstrapTable表格插件主要用来实现数据的分页和 ...

  5. 基于Metronic的Bootstrap开发框架经验总结(16)-- 使用插件bootstrap-table实现表格记录的查询、分页、排序等处理

    在业务系统开发中,对表格记录的查询.分页.排序等处理是非常常见的,在Web开发中,可以采用很多功能强大的插件来满足要求,且能极大的提高开发效率,本随笔介绍这个bootstrap-table是一款非常有 ...

  6. (转)基于Metronic的Bootstrap开发框架经验总结(2)--列表分页处理和插件JSTree的使用

    http://www.cnblogs.com/wuhuacong/p/4759564.html 在上篇<基于Metronic的Bootstrap开发框架经验总结(1)-框架总览及菜单模块的处理& ...

  7. 树组件——jstree使用

    本文记录的只是我自己当时的代码,每行的注释很清楚了,你自己可以做相应变通 一.使用前提: 1.下载jstree依赖包 2.相关页面引入样式["jstree/themes/default/st ...

  8. 前端-jstree 一些常用功能

    最近使用到了jstree(v3.3.4)这个插件(官网:https://www.jstree.com/),在这里记录下我的使用过程的一些技巧和问题. 1. 获取数据 一般实际项目中用到的数据都是aja ...

  9. 记一次debug记录:Uncaught SyntaxError: Unexpected token ILLEGAL

    在使用FIS3搭建项目的时候,遇到了一些问题,这里记录下. 这里是发布搭建代码: // 代码发布时 fis.media('qa') .match('*.{js,css,png}', { useHash ...

随机推荐

  1. MySQL常用函数(转)

    一.数学函数 ABS(x):返回x的绝对值 BIN(x):返回x的二进制(OCT返回八进制,HEX返回十六进制) CEILING(x):返回大于x的最小整数值 EXP(x):返回值e(自然对数的底)的 ...

  2. one troubleshooting case about em access issue

    Today when trying to open my Oracle EM url, I can not open it. So I thought may be the network is ha ...

  3. 用JQuery实现选中select里面的option显示对应的div

    HTML: <select name=""  onchange="select(this)"> <option value="1&q ...

  4. HDU1813:Escape from Tetris(IDA)

    Problem Description 因为整日整夜地对着这个棋盘,Lele最终走火入魔.每天一睡觉.他就会梦到自己会被人被扔进一个棋盘中,一直找不到出路,然后从梦中惊醒.久而久之,Lele被搞得精神 ...

  5. LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  6. [Unity 设计模式]IOC依赖倒置

    1.前言 最近在看<游戏开发与设计模式>一书,看到控制反转设计模式,作者说:上层模块不应该依赖于下层模块,上层模块和下层模块都应该依赖于接口,这样能减少耦合.然后附带举了个例子,我觉得特别 ...

  7. luogu 1045 麦森数

    题目大意: 从文件中输入P(1000<P<3100000),计算2^P−1的位数和最后500位数字(用十进制高精度数表示) 思路: 一道高精度练习题 其中位数是一个结论 位数=[P*log ...

  8. [Codeplus 2017] 晨跑

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5105 [算法] 答案为三个数的最小公倍数 [代码] #include<bits ...

  9. poi导出excel改变标题颜色

    在excelutil类里面添加 public class ExcelUtil { public static Workbook fillExcelData(ResultSet rs, Workbook ...

  10. idea ssm项目移包报错问题

    写完代码之后发现包结构太乱了  想要规划一下  结果报错 这里面的包路径都可以点进去,还是报找不到com.lf.company.entity.Business 后来发现是 在移动前和移动后都存在这个m ...