jsTree默认展开并选中所有checkbox复选框节点

前言:最近写了个小demo,使用 jstree 来加载一棵树,网上也能搜到一部分资料,但是大都讲的不太明白,本文展示了一个简单但完整的示例。
整个过程可以分为四步:
  0.前期准备
       主要是引入一下jstree的css和js,以及jquery。
  1.数据准备
  2.初始化树
  3.加载事件
废话不多说,直接上代码:
 JavaScript Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//数组准备
var  arr = [
    {
        id: 
"root1" ,
        text: 
"根节点1" ,
        parent: 
"#" ,
        state:
        {
            opened: 
true ,
            selected: 
true ,
        }
    },
    {
        id: 
"root1_sub1" ,
        text: 
"子节点1" ,
        parent: 
"root1" ,
        state:
        {
            opened: 
true ,
            selected: 
true ,
        }
    },
    {
        id: 
"root1_sub2" ,
        text: 
"子节点2" ,
        parent: 
"root1" ,
        state:
        {
            opened: 
true ,
            selected: 
true ,
        }
    },
    {
        id: 
"root2" ,
        text: 
"根节点2" ,
        parent: 
"#" ,
        state:
        {
            opened: 
true ,
            selected: 
true ,
        }
    },
    {
        id: 
"root2_sub1" ,
        text: 
"子节点1" ,
        parent: 
"root2" ,
        state:
        {
            opened: 
true ,
            selected: 
true ,
        }
    },
    {
        id: 
"root2_sub2" ,
        text: 
"子节点2" ,
        parent: 
"root2" ,
        state:
        {
            opened: 
true ,
            selected: 
true ,
        }
    }
];

//初始化树
$( function  ()
{
    $(
"#treebox" ).jstree(
    {
        core:
        {
            data: arr,
            check_callback: 
true //是否允许动态增加删除节点
            expand_selected_onload:  true //加载完成后是否展开所有选中的节点
            themes:
            {
                dots: 
true //是否显示连接线
                icons:  true //是否显示图标
            }
        },
        plugins: [
            
"checkbox"
        ],
        checkbox:
        {
            keep_selected_style: 
true //是否默认选中
            three_state:  true //是否父子级别级联
            tie_selection:  true //复选框是否与树的选择绑定
            whole_node:  true   //复选框是否与节点关联
        }
    }
    );
}
);

var  allLayers = [];
map.layers.forEach(
function  (item)
{
    allLayers.push(item.id);
}
);

//绑定事件
$( "#treebox" ).on( "changed.jstree" function  (event, data)
{
    console.log(data);
    
//获取所有选中的节点,这些节点对应的图层要显示
     var  needShowLayer = $( "#treebox" ).jstree().get_checked();
    needShowLayer.forEach(
function  (item)
    {
        
//TODO:展示
    }
    );
    
//找到所有要隐藏的图层
    allLayers.forEach( function  (item)
    {
        
if  (needShowLayer.indexOf(item) == - 1 )
        {
            
//TODO:隐藏
        }
    }
    );
}
);
结语:如果复选框不能出现,要看console里面有没有资源的报错说明,jstree的资源文件主要是两个,一个是雪碧图32px.png和一个loading的gif图标throbber.gif,所以这里面的复选框不是真正的复选框,而是一个图片。
最后,我们来看下32px.png长啥样:
jsTree默认展开并选中所有checkbox复选框节点
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AIGIS.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值