使用Ext.grid.Panel產生表格Ext.grid.Panel繼承了Ext.panel.Panel,因此它的很多地方都類似於Ext.panel.Panel,但定義Ext.grid.Panel時必須指定如下兩個選項。Ø store:該選項指定的Store對象負責為該表格提供資料。Ø columns:指定Ext.grid.column.Column數組或一個包含items屬性(該屬性值為Ext.grid.column.Column數組)的對象,該Ext.grid.column.Column數組指定Ext.grid.Panel產生的表格包含的所有列。如下代碼示範了如何使用Ext.grid.Panel來產生表格。程式清單:codes\06\6.8\Ext.grid\Ext.grid.Panel.html<body><script type="text/javascript">Ext.onReady(function(){ Ext.define('Book', { extend: 'Ext.data.Model', fields: [ {name: 'name', type: 'string'}, {name: 'author', type: 'string'}, {name: 'price', type: 'float'}, {name: 'publishDate', type: 'date'} ] }); // 建立一個Ext.data.Store對象 var bookStore = Ext.create('Ext.data.Store', { // 指定使用Book Model管理記錄 model: 'Book', // 直接使用data指定資料 data : [ {"id": 1, name: "瘋狂Java講義", author:'李剛' , price: 109, publishDate:'2012-01-01'}, {"id": 2, name:"輕量級Java EE公司專屬應用程式實戰", author:'李剛' , price: 99, publishDate:'2011-09-02'}, {"id": 3, name: "瘋狂Ajax講義", author:'李剛' , price: 69, publishDate:'2012-09-08'}, ] }); Ext.create('Ext.grid.Panel', { title: '查看圖書', width: 550, // 指定表單寬度 renderTo: Ext.getBody(), // 定義該表格包含的所有資料列 columns: [ www.2cto.com { text: '書名', dataIndex: 'name' , flex: 1 }, // 第1個資料列 { text: '作者', dataIndex: 'author', flex: 1 }, // 第2個資料列 { text: '價格', dataIndex: 'price' , flex: 1 }, // 第3個資料列 // 第4個資料列 { text: '出版時間', dataIndex: 'publishDate', xtype:'datecolumn' , format:'Y年m月d日' , flex: 1}, ], store: bookStore });});</script></body> 上面的樣本建立了一個Ext.grid.Panel,這個Panel對象將會在頁面上產生一個表格。定義該Ext.grid.Panel對象時指定了columns、store兩個選項,其中columns選項指定該表格包含的資料列,以及各列所顯示的資料;store選項指定一個Store對象,該Store負責提供該表格顯示的資料。在瀏覽器中瀏覽該頁面,可以看到6.71所示效果。圖6.71 使用Ext.grid.Panel產生表格從圖6.71可以看出,使用Ext.grid.Panel產生的表格比普通HTML表格更美觀,而且該表格預設可以對各列進行排序,並且可以自由控制各列的顯示、隱藏。除此之外,Ext.grid.Panel產生表格的列預設允許通過拖動改變列寬、通過拖動改變列與列之間的排列順序。