Ext2.2-用XML做數據源,可編輯Grid的例子

原文地址:http://www.java2000.net/p8972

先看看運行效果







html源代碼

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" c>
  4. <title>Editor Grid Example</title>
  5. <link rel="stylesheet" type="text/css" href="../resources/css/ext-all.css" />
  6.          <script type="text/javascript" src="../adapter/ext/ext-base.js"></script>
  7.     <script type="text/javascript" src="../ext-all.js"></script>
  8.     <script type="text/javascript">
  9.         Ext.onReady(function(){
  10.             Ext.QuickTips.init();
  11.                 // 日期格式化
  12.             function formatDate(value){
  13.                 return value ? value.dateFormat('Y年m月d日') : '';
  14.             };
  15.             // shorthand alias
  16.             var fm = Ext.form;
  17.             // 自定義的字段
  18.             var checkColumn = new Ext.grid.CheckColumn({
  19.                header: "婚否?",
  20.                dataIndex: 'married',
  21.                width: 55
  22.             });
  23.             // 列數據的模型
  24.             // dataIndex 對應着數據裏面的字段
  25.             var cm = new Ext.grid.ColumnModel([{
  26.                    id:'name', // 數據模型的唯一編號
  27.                    header: "姓名", // 標題
  28.                    dataIndex: 'name', // 對應於數據源裏面的字段
  29.                    width: 220, // 寬度
  30.                    editor: new fm.TextField({ // 編輯的類型
  31.                        allowBlank: false // 不允許爲空,如果爲空,則會顯示紅色波浪線警告且恢復原始數值
  32.                    })
  33.                 },{
  34.                    header: "學位", // 學位的標題
  35.                    dataIndex: 'degree', // 對應於學位
  36.                    width: 130,
  37.                    editor: new Ext.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: new fm.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: new fm.DateField({ // 日期的編輯框
  61.                         format: 'Y-m-d', // 格式
  62.                         minValue: '1908-08-08'
  63.                     })
  64.                 },
  65.                 checkColumn // 自定義的婚否列
  66.             ]);
  67.             // 默認列是可以排序的
  68.             cm.defaultSortable = true;
  69.             // 創建數據源的記錄,代表一個員工
  70.             var Employee = 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.             var store = new Ext.data.Store({
  82.                 // 使用HTTP協議獲得
  83.                 url: 'employees.xml',
  84.                 // the return will be XML, so lets set up a reader
  85.                 // 返回的是一個XML數據,我們解析爲我們定義的記錄格式 Employee
  86.                 reader: new Ext.data.XmlReader({
  87.                        // 記錄裏面有個 "employee" 的標籤
  88.                        record: 'employee'
  89.                    }, Employee),
  90.                 sortInfo:{field:'name', direction:'ASC'}  // 默認按照姓名正向排序
  91.             });
  92.             // 創建可編輯的 Grid
  93.             var grid = new Ext.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: [{ // 頂部的工具欄 tools bar
  105.                     text: '增加新員工',
  106.                     handler : function(){
  107.                         var p = new Employee({
  108.                             name: '輸入員工姓名',
  109.                             degree: '學士',
  110.                             salary: 0,
  111.                             birthday: (new Date()).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.                     var view = 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.                     var index = this.grid.getView().findRowIndex(t);
  142.                     var record = 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 '<div class="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. <select name="degree" id="degree" style="display: none;">
  157.         <option value="博士">博士</option>
  158.         <option value="碩士">碩士</option>
  159.         <option value="雙學士">雙學士</option>
  160.         <option value="學士">學士</option>
  161.         <option value="其它">其它</option>
  162. </select>
  163. <div id="editor-grid"></div>
  164. </body>
  165. </html>


用到的 employees.xml
  1. <?xml version="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>


結論: Extjs 確實不錯。 <script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>