ExtJS4学习笔记(十八)---带复选框的树(Checkbox tree)

50 篇文章 0 订阅
22 篇文章 1 订阅

实例显示了一个简单的带复选框的的树,通过返回的JSON数据来显示复选框是否是选中/不选中状态。例子中没有采用异步的方式展开节点。如果想使用异步方式加载节点,可以参考本站另一片文章来实现此功能,链接地址为:www.mhzg.net/a/20112/2011222109040.html


效果图

js代码:

 
  1. Ext.require([
  2.     'Ext.tree.*',
  3.     'Ext.data.*',
  4.     'Ext.window.MessageBox'
  5. ]);

  6. Ext.onReady(function() {
  7.     var store = Ext.create('Ext.data.TreeStore', {
  8.         proxy: {
  9.             type: 'ajax',
  10.             url: 'check-nodes.json'
  11.         },
  12.         sorters: [{
  13.             property: 'leaf',
  14.             direction: 'ASC'
  15.         }, {
  16.             property: 'text',
  17.             direction: 'ASC'
  18.         }]
  19.     });

  20.     var tree = Ext.create('Ext.tree.Panel', {
  21.         store: store,
  22.         rootVisible: false,
  23.         useArrows: true,
  24.         frame: true,
  25.         title: 'Check Tree',
  26.         renderTo: 'tree-div',
  27.         width: 289,
  28.         height: 220,
  29.         dockedItems: [{
  30.             xtype: 'toolbar',
  31.             items: {
  32.                 text: 'Get checked nodes',
  33.                 handler: function(){
  34.                     var records = tree.getView().getChecked(),
  35.                         names = [];
  36.                     
  37.                     Ext.Array.each(records, function(rec){
  38.                         names.push(rec.get('text'));
  39.                     });
  40.                     
  41.                     Ext.MessageBox.show({
  42.                         title: 'Selected Nodes',
  43.                         msg: names.join('<br />'),
  44.                         icon: Ext.MessageBox.INFO
  45.                     });
  46.                 }
  47.             }
  48.         }]
  49.     });
  50. });
 

HTML代码:

 
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5.     <title>Tree Example</title>
  6.     <link rel="stylesheet" type="text/css" href="../../resources/css/ext-all.css" />
  7.     <link rel="stylesheet" type="text/css" href="../shared/example.css" />

  8.     <script type="text/javascript" src="../../bootstrap.js"></script>
  9.     <script type="text/javascript" src="check-tree.js"></script>
  10.     
  11.     <style>
  12.         .x-tree-checked {
  13.             text-decoration: line-through;
  14.             color: #777;
  15.         }
  16.         .x-grid-row-selected .x-grid-cell {
  17.             background-color: #efefef !important;
  18.         }
  19.     </style>
  20. </head>
  21. <body>
  22.    <div id="tree-div"></div>
  23. </body>
  24. </html>

  

 

 JSON:

 
  1. [{
  2.     "text""To Do"
  3.     "cls""folder",
  4.     "expanded"true,
  5.     "children": [{
  6.         "text""Go jogging",
  7.         "leaf"true,
  8.         "checked"true
  9.     },{
  10.         "text""Take a nap",
  11.         "leaf"true,
  12.         "checked"false
  13.     },{
  14.         "text""Climb Everest",
  15.         "leaf"true,
  16.         "checked"false
  17.     }]
  18. },{
  19.     "text""Grocery List",
  20.     "cls""folder",
  21.     "children": [{
  22.         "text""Bananas",
  23.         "leaf"true,
  24.         "checked"false
  25.     },{
  26.         "text""Milk",
  27.         "leaf"true,
  28.         "checked"false
  29.     },{
  30.         "text""Cereal",
  31.         "leaf"true,
  32.         "checked"false
  33.     },{
  34.         "text""Energy foods",
  35.         "cls""folder",
  36.         "children": [{
  37.             "text""Coffee",
  38.             "leaf"true,
  39.             "checked"false
  40.         },{
  41.             "text""Red Bull",
  42.             "leaf"true,
  43.             "checked"false
  44.         }]
  45.     }]
  46. },{
  47.     "text""Remodel Project"
  48.     "cls""folder",
  49.     "children": [{
  50.         "text""Finish the budget",
  51.         "leaf"true,
  52.         "checked"false
  53.     },{
  54.         "text""Call contractors",
  55.         "leaf"true,
  56.         "checked"false
  57.     },{
  58.         "text""Choose design",
  59.         "leaf"true,
  60.         "checked"false
  61.     }]
  62. }]
 

 OK。。实例中,当我们选择了某个节点的复选框,点击 Get checked nodes 按钮,我们可以得到此节点的text值,本例当点击Get checked nodes按钮,弹出的对话框会显示Go jogging。可我们在实际操作中,显示的文本并不能满足我们的需求,一般来说,我们需要获取此节点的ID值,然后传到服务端,进行操作。下面改造下我们的JS代码:

 
  1. Ext.require([
  2.     'Ext.tree.*',
  3.     'Ext.data.*',
  4.     'Ext.window.MessageBox'
  5. ]);

  6. Ext.onReady(function() {
  7.     var store = Ext.create('Ext.data.TreeStore', {
  8.         proxy: {
  9.             type: 'ajax',
  10.             url: 'check-nodes.json'
  11.         },
  12.         sorters: [{
  13.             property: 'leaf',
  14.             direction: 'ASC'
  15.         }, {
  16.             property: 'text',
  17.             direction: 'ASC'
  18.         }]
  19.     });

  20.     var tree = Ext.create('Ext.tree.Panel', {
  21.         store: store,
  22.         rootVisible: false,
  23.         useArrows: true,
  24.         frame: true,
  25.         title: 'Check Tree',
  26.         renderTo: 'tree-div',
  27.         width: 289,
  28.         height: 220,
  29.         dockedItems: [{
  30.             xtype: 'toolbar',
  31.             items: {
  32.                 text: 'Get checked nodes',
  33.                 handler: function(){
  34.                     var records = tree.getView().getChecked(),
  35.                         names = [];
  36.                         //为存储ID而创建数组
  37.                         ids   = [];
  38.                     
  39.                     Ext.Array.each(records, function(rec){
  40.                         names.push(rec.get('text'));
  41.                         //将选中的节点的ID保存
  42.                         ids.push(rec.get('id'));
  43.                     });
  44.                     
  45.                     Ext.MessageBox.show({
  46.                         title: 'Selected Nodes',
  47.                         msg: names.join('<br />')+ids.join('<br />'),
  48.                         icon: Ext.MessageBox.INFO
  49.                     });
  50.                 }
  51.             }
  52.         }]
  53.     });
  54. });

 例子中我们增加了一个数组ids,并在循环中将被选中的节点压入ids中,最后在对话框中显示选中节点的text值和id值。在尝试之前,我们还需要将我们的JSON数据做下调整。调整后的JSON数据如下:

 
  1. [{
  2.     "text""To Do"
  3.     "cls""folder",
  4.     "expanded"true,
  5.     "children": [{
  6.         "text""Go jogging",
  7.         "id"  : "3",
  8.         "leaf"true,
  9.         "checked"true
  10.     },{
  11.         "text""Take a nap",
  12.         "leaf"true,
  13.         "id"  : "4",
  14.         "checked"false
  15.     },{
  16.         "text""Climb Everest",
  17.         "leaf"true,
  18.         "id"  : "5",
  19.         "checked"false
  20.     }]
  21. },{
  22.     "text""Grocery List",
  23.     "cls""folder",
  24.     "children": [{
  25.         "text""Bananas",
  26.         "leaf"true,
  27.         "id"  : "8",
  28.         "checked"false
  29.     },{
  30.         "text""Milk",
  31.         "leaf"true,
  32.         "id"  : "10",
  33.         "checked"false
  34.     },{
  35.         "text""Cereal",
  36.         "leaf"true,
  37.         "id"  : "15",
  38.         "checked"false
  39.     },{
  40.         "text""Energy foods",
  41.         "cls""folder",
  42.         "children": [{
  43.             "text""Coffee",
  44.             "leaf"true,
  45.             "id"  : "52",
  46.             "checked"false
  47.         },{
  48.             "text""Red Bull",
  49.             "leaf"true,
  50.             "id"  : "13",
  51.             "checked"false
  52.         }]
  53.     }]
  54. },{
  55.     "text""Remodel Project"
  56.     "cls""folder",
  57.     "children": [{
  58.         "text""Finish the budget",
  59.         "leaf"true,
  60.         "id"  : "34",
  61.         "checked"false
  62.     },{
  63.         "text""Call contractors",
  64.         "leaf"true,
  65.         "id"  : "103",
  66.         "checked"false
  67.     },{
  68.         "text""Choose design",
  69.         "leaf"true,
  70.         "id"  : "6",
  71.         "checked"false
  72.     }]
  73. }]

 是的,在JSON数据中,要返回id的值,这样才能确保代码可以获取正确的值。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值