随笔 - 165, 文章 - 0, 评论 - 18, 阅读 - 22万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

【转】ExtJs Grid自动生成列

Posted on   火冰·瓶  阅读(1045)  评论(0编辑  收藏  举报

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

数据源:'/Scripts/11.js'   也可以是aspx、jsp页面或者servlet

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: }

 

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: });

 

-------------------------------------------------

 

以参看下:
var cmItems = [];
var cmConfig = {};
cmItems.push(new Ext.grid.RowNumberer());
cmItems.push(sm);
cmItems.push({header : 'id',dataIndex : 'id',hidden : true,sortable : true});
cmItems.push({header : '单据编号',dataIndex : 'no',sortable : true});
cmItems.push({header : '组织',dataIndex : 'orgName',sortable : true});
cmItems.push({header : '上报日期',dataIndex : 'reportDate',sortable : true});
var priceTypeList = results.root
for(var i=0;i<priceTypeList.length;i++){
     cmConfig = {header : priceTypeList[i].priceType,dataIndex : 'priceType'+priceTypeList[i].id,width : 100,sortable : true}
     cmItems.push(cmConfig);
}
cmItems.push({header : ' 备注',dataIndex : 'note',width : 150,sortable : true});
cmItems.push({header : '创建人',dataIndex : 'createUserName',sortable : true});
cmItems.push({header : '创建日期',dataIndex : 'createDate',width : 120,sortable : true});
cmItems.push({header : '修改人',dataIndex : 'updateUserName',sortable : true});
cmItems.push({header : '修改日期',dataIndex : 'updateDate',width : 120,sortable : true});
cmItems.push({header : '状态',dataIndex : 'state',sortable : true});
// 信息列
var cm = new Ext.grid.ColumnModel(cmItems);

编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示