Extjs4 TreeGrid

来源:互联网 发布:淘宝饰品店名字 编辑:程序博客网 时间:2024/05/18 02:52

Extjs4 TreeGrid


本文介绍Extjs4 TreeGrid的使用,实例中的树实现了多表头、使用AJAX请求载入数据、排序,隐藏和显示表头等Grid中的功能。实例来自Extjs官方网站

本文介绍Extjs4 TreeGrid的使用,实例中的树实现了多表头、使用AJAX请求载入数据、排序,隐藏和显示表头等Grid中的功能。实例来自Extjs官方网站

HTML代码:

 
  1. <div id="tree-example"></div>

JS代码:

 
  1. Ext.require([
  2.     'Ext.data.*',
  3.     'Ext.grid.*',
  4.     'Ext.tree.*'
  5. ]);
  6. Ext.onReady(function() {    
  7.     //we want to setup a model and store instead of using dataUrl
  8.     Ext.regModel('Task', {
  9.         fields: [
  10.             {name: 'task',     type: 'string'},
  11.             {name: 'user',     type: 'string'},
  12.             {name: 'duration', type: 'string'}
  13.         ]
  14.     });
  15.     
  16.     var store = new Ext.data.TreeStore({
  17.         model: 'Task',
  18.         proxy: {
  19.             type: 'ajax',
  20.             //the store will get the content from the .json file
  21.             url: 'treegrid.json'
  22.         },
  23.         root: {
  24.             expanded: true
  25.         }
  26.     });
  27.     
  28.     //Ext.ux.tree.TreeGrid is no longer a Ux. You can simply use a tree.TreePanel
  29.     var tree = new Ext.tree.TreePanel({
  30.         title: 'Core Team Projects',
  31.         
  32.         width: 500,
  33.         height: 300,
  34.         renderTo: Ext.getBody(),
  35.         useArrows: true,
  36.         rootVisible: false,
  37.         
  38.         store: store,
  39.         
  40.         //the 'columns' property is now 'headers'
  41.         headers: [{
  42.             xtype: 'treeheader'//this is so we know which column will show the tree
  43.             text: 'Task',
  44.             flex: 2,
  45.             dataIndex: 'task'
  46.         },{
  47.             //we must use the templateheader component so we can use a custom tpl
  48.             xtype: 'templateheader',
  49.             text: 'Duration',
  50.             flex: 1,
  51.             dataIndex: 'duration',
  52.             align: 'center',
  53.             //add in the custom tpl for the rows
  54.             tpl: new Ext.XTemplate('{duration:this.formatHours}', {
  55.                 formatHours: function(v) {
  56.                     if (v < 1) {
  57.                         return Math.round(v * 60) + ' mins';
  58.                     } else if (Math.floor(v) !== v) {
  59.                         var min = v - Math.floor(v);
  60.                         return Math.floor(v) + 'h ' + Math.round(min * 60) + 'm';
  61.                     } else {
  62.                         return v + ' hour' + (v === 1 ? '' : 's');
  63.                     }
  64.                 }
  65.             })
  66.         },{
  67.             text: 'Assigned To',
  68.             flex: 1,
  69.             dataIndex: 'user'
  70.         }]
  71.     });
  72. });

实例使用JSON数据格式,要求返回类似如下格式的数据:

 
  1. [{"text":".","children": [
  2.     {
  3.         task:'Project: Shopping',
  4.         duration:13.25,
  5.         user:'Tommy Maintz',
  6.         iconCls:'task-folder',
  7.         expanded: true,
  8.         children:[{
  9.             task:'Housewares',
  10.             duration:1.25,
  11.             user:'Tommy Maintz',
  12.             iconCls:'task-folder',
  13.             children:[{
  14.                 task:'Kitchen supplies',
  15.                 duration:0.25,
  16.                 user:'Tommy Maintz',
  17.                 leaf:true,
  18.                 iconCls:'task'
  19.             },{
  20.                 task:'Groceries',
  21.                 duration:.4,
  22.                 user:'Tommy Maintz',
  23.                 leaf:true,
  24.                 iconCls:'task'
  25.             },{
  26.                 task:'Cleaning supplies',
  27.                 duration:.4,
  28.                 user:'Tommy Maintz',
  29.                 leaf:true,
  30.                 iconCls:'task'
  31.             },{
  32.                 task: 'Office supplies',
  33.                 duration: .2,
  34.                 user: 'Tommy Maintz',
  35.                 leaf: true,
  36.                 iconCls: 'task'
  37.             }]
  38.         }]
  39.     }]
  40. }]

实例链接:dev.sencha.com/deploy/ext-4.0-pr5/examples/tree/treegrid.html

原创粉丝点击