jstree的简单使用例子

最近使用到了jstree,感觉是一款灵活的、可多项定制的tree插件;

我这边使用过程记录下;

参考的jstree api网站,以及demo介绍:

https://www.jstree.com/api/#/

jstree api github:


使用中的例子介绍:

html代码:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!-- 搜索框 -->  
  2.  <div class="search_input">  
  3.      <input type="text" id="search_ay"  />   
  4.      <img src="/sfytj/dist/images/icon/ss_search.png" />  
  5.   </div>  
  6.   
  7. <!-- 案由列表 -->  
  8. <div class="reason_list">  
  9.   <div id="treeview1" class="treeview">  
  10.   
  11.   </div>  
  12.  </div>  

js代码:

1)生成jstree:

[javascript]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. $("#treeview1").jstree({  
  2.             'core' : {  
  3.                 "multiple" : false,  
  4.                 'data' : ay_mssys,  
  5.                 'dblclick_toggle'false          //禁用tree的双击展开  
  6.             },  
  7.             "plugins" : ["search"]   
  8. });  
  9.   
  10. var ay_mssys =   
  11.     [  
  12.         {  
  13.             "id""1",  
  14.             "text""民事案由(2008版)",  
  15.             "state": {  
  16.                         "opened"true,          //展示第一个层级下面的node  
  17.                         "disabled"true         //该根节点不可点击  
  18.                      },  
  19.             "children":   
  20.                         [  
  21.                             {  
  22.                             "id""2",  
  23.                             "text""人格权纠纷",  
  24.                             "children":   
  25.                                         [  
  26.                                             {  
  27.                                                 "id""3",  
  28.                                                 "text""人格权纠纷",  
  29.                                                 "children": [  
  30.                                                     {  
  31.                                                         "id""4",  
  32.                                                         "text""生命权、健康权、身体权纠纷",  
  33.                                                         "children":   
  34.                                                                     [  
  35.                                                                         {  
  36.                                                                             "id""5",  
  37.                                                                             "text""道路交通事故人身损害赔偿纠纷"  
  38.                                                                          }  
  39.                                                                     ]  
  40.                                                     }  
  41.                                                  ]  
  42.                                             }  
  43.                                         ]  
  44.                             }  
  45.                         ]  
  46.             }  
  47.     ]  
  48.   
  49. //core:整个jstree显示的核心,里面包括多种项配置:  
  50. //data: 这里是使用json格式的数据;还可以使用html或者ajax请求等  
  51. //plugins: 这个jstree引用了哪些插件  
  52. //multiple : false  不可多选  

2)点击jstree的每个子项,获取该节点的text、id等信息:

[javascript]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //tree change时事件  
  2. $('#treeview1').on("changed.jstree"function (e, data) {  
  3.     console.log("The selected nodes are:");  
  4.     console.log(data.node.id);               //选择的node id  
  5.     console.log(data.node.text);            //选择的node text  
  6.   
  7.     form_data.ay = data.node.text;  
  8.     form_data.ay_id = data.node.id;  
  9.   
  10. });  
  11.   
  12. //changed.jstree,jstree改变时发生的事件,类似的还有select_node.jstree等,api中有。  

3)点击jstree子项,控制该节点展开、收缩等:

[javascript]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //jstree单击事件  
  2. $("#treeview1").bind("select_node.jstree"function (e, data) {  
  3.   
  4.     if(data.node.id !=1 ){                           //排除第一个节点(2011民事案由)  
  5.         data.instance.toggle_node(data.node);        //单击展开下面的节点  
  6.     }  
  7.       
  8. });   

4)使用插件search搜索(jstree自带的插件):

[javascript]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //输入框输入定时自动搜索  
  2. var to = false;  
  3. $('#search_ay').keyup(function () {  
  4.     if(to) {  
  5.       clearTimeout(to);   
  6.     }  
  7.   
  8.     to = setTimeout(function () {  
  9.         $('#treeview1').jstree(true).search($('#search_ay').val());  
  10.   
  11.     }, 250);  
  12. });  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值