原文地址:http://www.java2000.net/p8972
先看看運行效果
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();
- }
- thisthis.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
- <?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 確實不錯。 <script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>