Ext2.2-用XML做数据源,可编辑Grid的例子

本文介绍了一个使用 ExtJS 实现的可编辑表格示例,包括自定义列、数据验证等功能,并提供了完整的 HTML 源代码及数据文件。
原文地址:http://www.java2000.net/p8972

先看看运行效果
redirectImage.jsp?id=1062

html源代码
  1. <html>
  2. <head>
  3. <metahttp-equiv="Content-Type"c>
  4. <title>EditorGridExample</title>
  5. <linkrel="stylesheet"type="text/css"href="../resources/css/ext-all.css"/>
  6. <scripttype="text/javascript"src="../adapter/ext/ext-base.js"></script>
  7. <scripttype="text/javascript"src="../ext-all.js"></script>
  8. <scripttype="text/javascript">
  9. Ext.onReady(function(){
  10. Ext.QuickTips.init();
  11. //日期格式化
  12. functionformatDate(value){
  13. returnvalue?value.dateFormat('Y年m月d日'):'';
  14. };
  15. //shorthandalias
  16. varfm=Ext.form;
  17. //自定义的字段
  18. varcheckColumn=newExt.grid.CheckColumn({
  19. header:"婚否?",
  20. dataIndex:'married',
  21. width:55
  22. });
  23. //列数据的模型
  24. //dataIndex对应着数据里面的字段
  25. varcm=newExt.grid.ColumnModel([{
  26. id:'name',//数据模型的唯一编号
  27. header:"姓名",//标题
  28. dataIndex:'name',//对应于数据源里面的字段
  29. width:220,//宽度
  30. editor:newfm.TextField({//编辑的类型
  31. allowBlank:false//不允许为空,如果为空,则会显示红色波浪线警告且恢复原始数值
  32. })
  33. },{
  34. header:"学位",//学位的标题
  35. dataIndex:'degree',//对应于学位
  36. width:130,
  37. editor:newExt.form.ComboBox({//使用自定义的下拉框
  38. typeAhead:true,
  39. triggerAction:'all',
  40. transform:'degree',//对应的下拉列表ID
  41. lazyRender:true,
  42. listClass:'x-combo-list-small'
  43. })
  44. },{
  45. header:"薪资(元)",
  46. dataIndex:'salary',
  47. width:70,
  48. align:'right',//右对齐
  49. editor:newfm.NumberField({//数字编辑框
  50. decimalPrecision:0,//默认的小数点位数
  51. allowBlank:false,
  52. allowNegative:false,//不允许为负数
  53. maxValue:100000//最大值为10万
  54. })
  55. },{
  56. header:"出生日期",
  57. dataIndex:'birthday',
  58. width:95,
  59. renderer:formatDate,
  60. editor:newfm.DateField({//日期的编辑框
  61. format:'Y-m-d',//格式
  62. minValue:'1908-08-08'
  63. })
  64. },
  65. checkColumn//自定义的婚否列
  66. ]);
  67. //默认列是可以排序的
  68. cm.defaultSortable=true;
  69. //创建数据源的记录,代表一个员工
  70. varEmployee=Ext.data.Record.create([
  71. //name对应着数据里面对应的标签,birthday例外,对应着birth
  72. {name:'name',type:'string'},
  73. {name:'address',type:'string'},
  74. {name:'degree'},
  75. {name:'salary',type:'int'},
  76. //日期自动转换
  77. {name:'birthday',mapping:'birth',type:'date',dateFormat:'m/d/Y'},
  78. {name:'married',type:'bool'}
  79. ]);
  80. //创建数据集,读取员工数据
  81. varstore=newExt.data.Store({
  82. //使用HTTP协议获得
  83. url:'employees.xml',
  84. //thereturnwillbeXML,soletssetupareader
  85. //返回的是一个XML数据,我们解析为我们定义的记录格式Employee
  86. reader:newExt.data.XmlReader({
  87. //记录里面有个"employee"的标签
  88. record:'employee'
  89. },Employee),
  90. sortInfo:{field:'name',direction:'ASC'}//默认按照姓名正向排序
  91. });
  92. //创建可编辑的Grid
  93. vargrid=newExt.grid.EditorGridPanel({
  94. store:store,//指定数据源
  95. cm:cm,
  96. renderTo:'editor-grid',//目标的id位置
  97. width:600,
  98. height:300,
  99. autoExpandColumn:'name',//默认自动扩展宽度的字段
  100. title:'人员信息编辑',//标题
  101. frame:true,
  102. plugins:checkColumn,
  103. clicksToEdit:0,//默认为双击编辑,如果为1则单击就编辑
  104. tbar:[{//顶部的工具栏toolsbar
  105. text:'增加新员工',
  106. handler:function(){
  107. varp=newEmployee({
  108. name:'输入员工姓名',
  109. degree:'学士',
  110. salary:0,
  111. birthday:(newDate()).clearTime(),
  112. married:false
  113. });
  114. grid.stopEditing();
  115. store.insert(0,p);
  116. grid.startEditing(0,0);
  117. }
  118. }]
  119. });
  120. //装载数据
  121. store.load();
  122. });
  123. Ext.grid.CheckColumn=function(config){
  124. Ext.apply(this,config);
  125. if(!this.id){
  126. this.id=Ext.id();
  127. }
  128. thisthis.renderer=this.renderer.createDelegate(this);
  129. };
  130. Ext.grid.CheckColumn.prototype={
  131. init:function(grid){
  132. this.grid=grid;
  133. this.grid.on('render',function(){
  134. varview=this.grid.getView();
  135. view.mainBody.on('mousedown',this.onMouseDown,this);
  136. },this);
  137. },
  138. onMouseDown:function(e,t){
  139. if(t.className&&t.className.indexOf('x-grid3-cc-'+this.id)!=-1){
  140. e.stopEvent();
  141. varindex=this.grid.getView().findRowIndex(t);
  142. varrecord=this.grid.store.getAt(index);
  143. record.set(this.dataIndex,!record.data[this.dataIndex]);
  144. }
  145. },
  146. renderer:function(v,p,record){
  147. p.css+='x-grid3-check-col-td';
  148. return'<divclass="x-grid3-check-col'+(v?'-on':'')+'x-grid3-cc-'+this.id+'"></div>';
  149. }
  150. };
  151. </script>
  152. </head>
  153. <body>
  154. <h1>可编辑的Grid</h1>
  155. <!--自定义的数据下拉框-->
  156. <selectname="degree"id="degree"style="display:none;">
  157. <optionvalue="博士">博士</option>
  158. <optionvalue="硕士">硕士</option>
  159. <optionvalue="双学士">双学士</option>
  160. <optionvalue="学士">学士</option>
  161. <optionvalue="其它">其它</option>
  162. </select>
  163. <divid="editor-grid"></div>
  164. </body>
  165. </html>
<html> <head> <meta http-equiv="Content-Type" c> <title>Editor Grid Example</title> <link rel="stylesheet" type="text/css" href="../resources/css/ext-all.css" /> <script type="text/javascript" src="../adapter/ext/ext-base.js"></script> <script type="text/javascript" src="../ext-all.js"></script> <script type="text/javascript"> Ext.onReady(function(){ Ext.QuickTips.init(); // 日期格式化 function formatDate(value){ return value ? value.dateFormat('Y年m月d日') : ''; }; // shorthand alias var fm = Ext.form; // 自定义的字段 var checkColumn = new Ext.grid.CheckColumn({ header: "婚否?", dataIndex: 'married', width: 55 }); // 列数据的模型 // dataIndex 对应着数据里面的字段 var cm = new Ext.grid.ColumnModel([{ id:'name', // 数据模型的唯一编号 header: "姓名", // 标题 dataIndex: 'name', // 对应于数据源里面的字段 width: 220, // 宽度 editor: new fm.TextField({ // 编辑的类型 allowBlank: false // 不允许为空,如果为空,则会显示红色波浪线警告且恢复原始数值 }) },{ header: "学位", // 学位的标题 dataIndex: 'degree', // 对应于学位 width: 130, editor: new Ext.form.ComboBox({ // 使用自定义的下拉框 typeAhead: true, triggerAction: 'all', transform:'degree', // 对应的下拉列表ID lazyRender:true, listClass: 'x-combo-list-small' }) },{ header: "薪资(元)", dataIndex: 'salary', width: 70, align: 'right', // 右对齐 editor: new fm.NumberField({ // 数字编辑框 decimalPrecision: 0, // 默认的小数点位数 allowBlank: false, allowNegative: false, // 不允许为负数 maxValue: 100000 // 最大值为10万 }) },{ header: "出生日期", dataIndex: 'birthday', width: 95, renderer: formatDate, editor: new fm.DateField({ // 日期的编辑框 format: 'Y-m-d', // 格式 minValue: '1908-08-08' }) }, checkColumn // 自定义的婚否列 ]); // 默认列是可以排序的 cm.defaultSortable = true; // 创建数据源的记录,代表一个员工 var Employee = Ext.data.Record.create([ // name对应着数据里面对应的标签,birthday例外,对应着birth {name: 'name', type: 'string'}, {name: 'address', type: 'string'}, {name: 'degree'}, {name: 'salary', type: 'int'}, // 日期自动转换 {name: 'birthday', mapping: 'birth', type: 'date', dateFormat: 'm/d/Y'}, {name: 'married', type: 'bool'} ]); // 创建数据集,读取员工数据 var store = new Ext.data.Store({ // 使用HTTP协议获得 url: 'employees.xml', // the return will be XML, so lets set up a reader // 返回的是一个XML数据,我们解析为我们定义的记录格式 Employee reader: new Ext.data.XmlReader({ // 记录里面有个 "employee" 的标签 record: 'employee' }, Employee), sortInfo:{field:'name', direction:'ASC'} // 默认按照姓名正向排序 }); // 创建可编辑的 Grid var grid = new Ext.grid.EditorGridPanel({ store: store, // 指定数据源 cm: cm, renderTo: 'editor-grid', // 目标的id位置 width:600, height:300, autoExpandColumn:'name', // 默认自动扩展宽度的字段 title:'人员信息编辑', // 标题 frame:true, plugins:checkColumn, clicksToEdit: 0, // 默认为双击编辑,如果为1则单击就编辑 tbar: [{ // 顶部的工具栏 tools bar text: '增加新员工', handler : function(){ var p = new Employee({ name: '输入员工姓名', degree: '学士', salary: 0, birthday: (new Date()).clearTime(), married: false }); grid.stopEditing(); store.insert(0, p); grid.startEditing(0, 0); } }] }); // 装载数据 store.load(); }); Ext.grid.CheckColumn = function(config){ Ext.apply(this, config); if(!this.id){ this.id = Ext.id(); } this.renderer = this.renderer.createDelegate(this); }; Ext.grid.CheckColumn.prototype ={ init : function(grid){ this.grid = grid; this.grid.on('render', function(){ var view = this.grid.getView(); view.mainBody.on('mousedown', this.onMouseDown, this); }, this); }, onMouseDown : function(e, t){ if(t.className && t.className.indexOf('x-grid3-cc-'+this.id) != -1){ e.stopEvent(); var index = this.grid.getView().findRowIndex(t); var record = this.grid.store.getAt(index); record.set(this.dataIndex, !record.data[this.dataIndex]); } }, renderer : function(v, p, record){ p.css += ' x-grid3-check-col-td'; return '<div class="x-grid3-check-col'+(v?'-on':'')+' x-grid3-cc-'+this.id+'"> </div>'; } }; </script> </head> <body> <h1>可编辑的 Grid</h1> <!-- 自定义的数据下拉框 --> <select name="degree" id="degree" style="display: none;"> <option value="博士">博士</option> <option value="硕士">硕士</option> <option value="双学士">双学士</option> <option value="学士">学士</option> <option value="其它">其它</option> </select> <div id="editor-grid"></div> </body> </html>

用到的 employees.xml
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <catalog>
  3. <employee>
  4. <name>张三</name>
  5. <address>天津市第一大街</address>
  6. <zone>4</zone>
  7. <degree>学士</degree>
  8. <salary>2440</salary>
  9. <birth>03/15/2006</birth>
  10. <married>true</married>
  11. </employee>
  12. <employee>
  13. <name>李四</name>
  14. <address>北京市朝阳区</address>
  15. <zone>3</zone>
  16. <degree>学士</degree>
  17. <salary>9370</salary>
  18. <birth>03/06/2006</birth>
  19. <married>true</married>
  20. </employee>
  21. <employee>
  22. <name>王五</name>
  23. <address>上海浦东</address>
  24. <zone>4</zone>
  25. <degree>博士</degree>
  26. <salary>6810</salary>
  27. <birth>05/17/2006</birth>
  28. <married>false</married>
  29. </employee>
  30. <employee>
  31. <name>赵六</name>
  32. <address>广州</address>
  33. <zone>4</zone>
  34. <degree>学士</degree>
  35. <salary>9900</salary>
  36. <birth>03/06/2006</birth>
  37. <married>true</married>
  38. </employee>
  39. <employee>
  40. <name>孙武</name>
  41. <address>四川成都</address>
  42. <zone>3</zone>
  43. <degree>学士</degree>
  44. <salary>6440</salary>
  45. <birth>01/20/2006</birth>
  46. <married>true</married>
  47. </employee>
  48. </catalog>
<?xml version="1.0" encoding="UTF-8"?> <catalog> <employee> <name>张三</name> <address>天津市第一大街</address> <zone>4</zone> <degree>学士</degree> <salary>2440</salary> <birth>03/15/2006</birth> <married>true</married> </employee> <employee> <name>李四</name> <address>北京市朝阳区</address> <zone>3</zone> <degree>学士</degree> <salary>9370</salary> <birth>03/06/2006</birth> <married>true</married> </employee> <employee> <name>王五</name> <address>上海浦东</address> <zone>4</zone> <degree>博士</degree> <salary>6810</salary> <birth>05/17/2006</birth> <married>false</married> </employee> <employee> <name>赵六</name> <address>广州</address> <zone>4</zone> <degree>学士</degree> <salary>9900</salary> <birth>03/06/2006</birth> <married>true</married> </employee> <employee> <name>孙武</name> <address>四川成都</address> <zone>3</zone> <degree>学士</degree> <salary>6440</salary> <birth>01/20/2006</birth> <married>true</married> </employee> </catalog>

结论:
Extjs 确实不错。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值