帶復(fù)選框(checkbox)的樹
官方實例
中有介紹如何將json數(shù)據(jù)變成一棵樹的
簡單例子
。
該例子可以滿足我們的一般需求。很多時候我們需要用到帶checkbox的樹。
其實加個checkbox框先簡單,只需要在返回來的json數(shù)據(jù)中加上 checked 項(為true/false)。要實現(xiàn)我們想要的選中父節(jié)點的checkbox后,自動選中子節(jié)點也很簡單,只需要加上下面這一段代碼就可以了。
01.
tree.on(
'checkchange'
,
function
(node, checked) {???
02.
????????
node.expand();???
03.
????????
node.attributes.checked = checked;???
04.
????????
node.eachChild(
function
(child) {???
05.
????????????
child.ui.toggleCheck(checked);???
06.
????????????
child.attributes.checked = checked;???
07.
????????????
child.fireEvent(
'checkchange'
, child, checked);???
08.
????????
});???
09.
????
}, tree);
?
演示(demo)地址在文章最后.
源代碼包括 0005_checkbox_reorder.html,0005_checkbox_reorder_tree.php.
0005_checkbox_reorder.html
01.
<html>
02.
<head>
03.
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
>
04.
<title>帶復(fù)選框(checkbox)的樹</title>
05.
<link rel=
"stylesheet"
type=
"text/css"
href=
"../../resources/css/ext-all.css"
/>
06.
<script type=
"text/javascript"
src=
"../../adapter/ext/ext-base.js"
></script>
07.
<script type=
"text/javascript"
src=
"../../ext-all.js"
></script>
08.
<link rel=
"stylesheet"
type=
"text/css"
href=
"../../examples/examples.css"
/>
09.
<script type=
"text/javascript"
src=
"../../examples.js"
></script>
10.
<script type=
"text/javascript"
>
11.
Ext.onReady(
function
(){
12.
????
Ext.BLANK_IMAGE_URL =
"../../resources/images/default/s.gif"
????
13.
????
// shorthand
14.
????
var
Tree = Ext.tree;
15.
?????
?
16.
????
var
tree =
new
Tree.TreePanel({
17.
????????
el:
'tree-div'
,
18.
????????
useArrows:
true
,
19.
????????
autoScroll:
true
,
20.
????????
animate:
true
,
21.
????????
enableDD:
true
,
22.
????????
containerScroll:
true
,?
23.
????????
loader:
new
Tree.TreeLoader({
24.
????????????
dataUrl:
'0005_checkbox_reorder_tree.php'
25.
????????
})
26.
????
});
27.
?????
?
28.
????
tree.on(
'checkchange'
,
function
(node, checked) {???
29.
????????
node.expand();???
30.
????????
node.attributes.checked = checked;???
31.
????????
node.eachChild(
function
(child) {???
32.
????????????
child.ui.toggleCheck(checked);???
33.
????????????
child.attributes.checked = checked;???
34.
????????????
child.fireEvent(
'checkchange'
, child, checked);???
35.
????????
});???
36.
????
}, tree);??
37.
?
?
38.
????
// set the root node
39.
????
var
root =
new
Tree.AsyncTreeNode({
40.
????????
text:
'Ext JS'
,
41.
????????
draggable:
false
,
42.
????????
id:
'source'
43.
????
});
44.
????
tree.setRootNode(root);
45.
?
?
46.
????
// render the tree
47.
????
tree.render();
48.
????
root.expand();
49.
});
50.
</script>
51.
?
?
52.
</head>
53.
<body>
54.
<h1>帶復(fù)選框(checkbox)的樹</h1>
55.
<p></p>
56.
<p>改編自ExtJs 自帶的tree例子,選中父節(jié)點后,所有子節(jié)點會自動選上。</p>
57.
?
?
58.
<p>該例子點擊父節(jié)點如果速度過快,有時候不會自動選中子節(jié)點! </p>
59.
?
?
60.
<div id=
"tree-div"
style=
"overflow:auto; height:300px;width:250px;border:1px solid #c3daf9;"
></div>
61.
?
?
62.
</body>
63.
</html>
?
0005_checkbox_reorder_tree.php
1.
[{
"cls"
:
"folder"
,
"id"
:10,
"leaf"
:
false
,checked:
false
,
"children"
:[{
"cls"
:
"file"
,
"id"
:11,
"leaf"
:
true
,checked:
false
,
"children"
:
null
,
"text"
:
"S600"
},{
"cls"
:
"file"
,
"id"
:12,
"leaf"
:
true
,checked:
false
,
"children"
:
null
,
"text"
:
"SLK200"
}],
"text"
:
"Benz"
}]
?
演示地址: http://extjs.org.cn/extjs/mydemo/tree/0005_checkbox_reorder.html
(版權(quán)聲明:本篇文章版權(quán)屬于 extjs.org.cn 所有,可以在互聯(lián)網(wǎng)上進行轉(zhuǎn)載,轉(zhuǎn)載必須保留作者版權(quán)聲明及鏈接;也可以文章用于出版、發(fā)行或其它商業(yè)用途,僅僅需要保留作者版權(quán)聲明及鏈接。)
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
