代码改变世界

ExtJs Grid自动生成列

2009-07-14 15:56  午夜瞎想  阅读(3008)  评论(2编辑  收藏  举报

这个问题纠结了我很久,本来想找个插件来解决的,但好像很少拿Extjs做报表啊,这么常用的功能竟然没有好的解决办法,唯一找到的一个插件就是AutoGrid2,可并不能解决问题,最后在老外的一段代码下终于找到了解决方法,原来如此简单.

数据源:

   1:    {'data':[{'number':'1','text1': '3','info1': '4','special1': '5'},{'number':'1','text1': '3','info1': '4','special1': '5'}],
   2:      'columModle':[
   3:                  {'header': '序号','dataIndex': 'number','width':40},
   4:                  {'header': '编码','dataIndex': 'text1'},
   5:                  {'header': '名称','dataIndex': 'info1'},
   6:                  {'header': '金额','dataIndex': 'special1'}
   7:                  ],
   8:      'fieldsNames':[{name: 'number'},{name: 'text1'}, {name: 'info1'},{name: 'special1'}]
   9:    } 

js:

   1:  Ext.onReady(function() {
   2:   
   3:          var conn = new Ext.data.Connection();
   4:          conn.request({ url: '/Scripts/11.js', callback: function(options, success, response) {
   5:              var json = new Ext.util.JSON.decode(response.responseText);
   6:              var cm = new Ext.grid.ColumnModel(json.columModle);
   7:               var ds = new Ext.data.JsonStore({
   8:               data: json.data,
   9:               fields:json.fieldsNames
  10:               });
  11:              var grid = new Ext.grid.GridPanel({
  12:                  height: 200,
  13:                  width: 400,
  14:                  region: 'center',
  15:                  split: true,
  16:                  border: false,
  17:                  store: ds,
  18:                  cm: cm
  19:              });
  20:   
  21:              grid.render('grid-example');
  22:          }
  23:          });