JavaScript的Ext JS框架中的GridPanel組件使用指南
1 最簡單的Grid Panel
Grid Panel是ExtJS的核心部分之一,通過Grid Panel可以對數(shù)據(jù)顯示、排序、分組和編輯。Model和Store是Grid Panel處理數(shù)據(jù)的核心,每個Grid Panel都必須設(shè)置Model和Store。要創(chuàng)建Grid Panel,首先要定義Model,Model包括了Grid Panel所有需要顯示的字段,相當(dāng)于數(shù)據(jù)庫中表字段的集合。Store可以看作是一行數(shù)據(jù)的集合或者是Model的實例集合,每個Store都包含一個或多個Model實例,Grid Panel顯示的數(shù)據(jù)都存儲在Store里面。Grid Panel通過Store獲取數(shù)據(jù)并顯示,Store則通過Proxy對數(shù)據(jù)進(jìn)行讀取和保存。
下面創(chuàng)建一個Grid Panel用來顯示用戶的基本信息,包括用戶名、郵箱、手機(jī)號(name、email、phone),首先創(chuàng)建用戶模型(User Model)。
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [ 'name', 'email', 'phone' ]
});
接下來創(chuàng)建Store,Store是User的集合,包括多個User實例。
var userStore = Ext.create('Ext.data.Store', {
model: 'User', //剛才創(chuàng)建的User Model
data: [
{ name: 'Lisa', email: 'lisa@simpsons.com', phone: '555-111-1224' },
{ name: 'Bart', email: 'bart@simpsons.com', phone: '555-222-1234' },
{ name: 'Homer', email: 'home@simpsons.com', phone: '555-222-1244' },
{ name: 'Marge', email: 'marge@simpsons.com', phone: '555-222-1254' }
]
});
Model和Store都創(chuàng)建好之后,就可以創(chuàng)建Grid Panel了。
Ext.create('Ext.grid.Panel', {
renderTo: Ext.getBody(),
store: userStore, //綁定上面創(chuàng)建的Store
width: 400,
height: 200,
title: 'Application Users',
columns: [
{
text: 'Name',
width: 100,
sortable: false,
hideable: false,
dataIndex: 'name' //Grid Panel中顯示的字段,必須要和User Model中的字段一致
},
{
text: 'Email Address',
width: 150,
dataIndex: 'email',
hidden: true
},
{
text: 'Phone Number',
flex: 1,
dataIndex: 'phone'
}
]
});
最后創(chuàng)建的用戶Grid Panel如圖所示。

2 Grid Panel數(shù)據(jù)分組(Grouping)
只要在Store中設(shè)置groupField屬性,就可以對Grid Panel顯示的數(shù)據(jù)進(jìn)行分組。假設(shè)公司有很多員工,需要對公司的員工在Grid Panel中按部門進(jìn)行分組顯示。首先在Store中設(shè)置groupField屬性為department。
Ext.create('Ext.data.Store', {
model: 'Employee',
data: ...,
groupField: 'department' //設(shè)置數(shù)據(jù)按照department分組
});
然后在Grid Panel中添加grouping Feature,實現(xiàn)分組顯示效果。
Ext.create('Ext.grid.Panel', {
...
features: [{ ftype: 'grouping' }]
});
分組顯示效果如下圖所示,點(diǎn)擊這里查看官方分組顯示代碼。

3 Grid Panel分頁顯示
當(dāng)數(shù)據(jù)比較多一頁顯示不完的時候,就需要對數(shù)據(jù)進(jìn)行分頁顯示。Grid Panel可以通過Paging Toolbar和Paging Scroller兩種方式實現(xiàn)分頁顯示顯示,Paging Toolbar有上一頁/下一頁按鈕,Paging Scroller是當(dāng)Grid Panel滾動顯示到底部的時候動態(tài)加載數(shù)據(jù)。
要實現(xiàn)分頁顯示,首先要設(shè)置Store支持分頁,在Store中設(shè)置pageSize,pageSize默認(rèn)是25。在reader中設(shè)置數(shù)據(jù)總數(shù)量totalProperty,分頁插件根據(jù)pageSize和totalProperty進(jìn)行分頁。下面的代碼pageSize設(shè)置為4,totalProperty則從返回的json數(shù)據(jù)中total屬性獲取。
Ext.create('Ext.data.Store', {
model: 'User',
autoLoad: true,
pageSize: 4, //設(shè)置每頁顯示的數(shù)據(jù)數(shù)量
proxy: {
type: 'ajax',
url : 'data/users.json',
reader: {
type: 'json',
root: 'users', //指定從json的哪個屬性獲取數(shù)據(jù),如果不指定,則從json的跟屬性獲取
totalProperty: 'total' //總數(shù)據(jù)數(shù)量
}
}
});
假設(shè)json數(shù)據(jù)格式如下
{
"success": true,
"total": 12,
"users": [
{ "name": "Lisa", "email": "lisa@simpsons.com", "phone": "555-111-1224" },
{ "name": "Bart", "email": "bart@simpsons.com", "phone": "555-222-1234" },
{ "name": "Homer", "email": "home@simpsons.com", "phone": "555-222-1244" },
{ "name": "Marge", "email": "marge@simpsons.com", "phone": "555-222-1254" }
]
}
Store的分頁已經(jīng)設(shè)置完畢,下面在Grid Panel中實現(xiàn)Paging Toolbar分頁功能。
Ext.create('Ext.grid.Panel', {
store: userStore,
columns: ...,
dockedItems: [{
xtype: 'pagingtoolbar', //在Grid Panel中添加paging toolbar
store: userStore, //把paging toolbar的Store設(shè)置成和Grid Panel的Store一樣
dock: 'bottom',
displayInfo: true
}]
});
Paging Toolbar的分頁效果如下圖所示,點(diǎn)擊這里查看官方Paging Toolbar分頁功能代碼。

Paging Scroller的分頁實現(xiàn)比較簡單,只要在Grid Panel中設(shè)置如下代碼即可,點(diǎn)擊這里查看官方Paging Scroller分頁功能代碼。
Ext.create('Ext.grid.Panel', {
//使用Paging Scroller分頁插件
verticalScroller: 'paginggridscroller',
// do not reset the scrollbar when the view refreshs
invalidateScrollerOnRefresh: false,
// infinite scrolling does not support selection
disableSelection: true,
// ...
});
4 Grid Panel添加Checkbox
只要設(shè)置Grid Panel的selModel屬性為Ext.selection.CheckboxModel,點(diǎn)擊這里查看官方代碼實例。
Ext.create('Ext.grid.Panel', {
selModel: Ext.create('Ext.selection.CheckboxModel'), //設(shè)置Grid Panel的選擇模式為Checkbox
store: userStore,
columns: ...
});
5 綜合示例
var grid = new Ext.grid.GridPanel({
store //數(shù)據(jù)源
cm //Ext.grid.columnModel
columns //功能和Ext.grid.columnModel一樣。與cm有一個就行
autoWidth:true
width
title
border:false
columnLines: true,
renderTo //顯示div標(biāo)簽的id
animCollapse:false //True 表示為面板閉合過程附有動畫效果 (默認(rèn)為true,在類 Ext.Fx 可用的情況下).
collapsible: true, //true 表示面板可以閉合
columnLines:true, //true 表示有格邊框
loadMask:true //獲取數(shù)據(jù)里時有等待界面
stripeRows: true, //雙色表格
plugins:true,
bbar:new Ext.PagingToolbar({
pageSize:10,
store:store, //數(shù)據(jù)源
displayInfo:true, //為true時下面的才顯示
displayMsg:'顯示第 {0} 條到 {1} 條記錄,一共 {2} 條',
emptyMsg:'沒有記錄'
}),
tbar:[{
text:'查詢',
icon:'/trade/images/delete.gif',
cls:'x-btn-text-icon',
handler:function(){win.show();}
}
})
//**********************************************
//PagingToolbar分頁
//傳到服務(wù)器數(shù)據(jù) start開始查詢位置, limit要查詢多少條
//排序
//服務(wù)器 sort,dir
//**********************************************
var com = new Ext.grid.ColumnModel([
new Ext.grid.RowNumberer(),
{header: "客戶ID", width: 50, sortable: true, dataIndex: 'memid'},
{header: "客戶姓名", width: 75, sortable: true, dataIndex: 'memName'},
{header:'姓別', width:50, dataIndex:'sex', align:'center', sortable:true, renderer:function(v){return (v == '1')?'<img src="images/user_suit.png">':'<img src="images/user_female.png">';}}
{header: '跟蹤號',width:150,dataIndex:'code'},
{header: '日期', width:150, dataIndex: 'kd_time'}
]);
/***********************************************
grid tbar
樣式
cls:'x-btn-text-icon' 添加
**************************************************/
EditorGridPanel
chickToEdit:1 //點(diǎn)擊次數(shù)
ColumnModel 中要加editor editor:new Ext.form.TextField({
})
//獲取修改后的數(shù)據(jù)
var storeEdit = grid.getStore(); //
var modified = storeEdit.modified.slice(0); //
Ext.each(modified,function(m){
alert(m.data.id); //數(shù)據(jù)就在m.data中 id 為字段名
})
//獲取grid數(shù)據(jù)
var selModel = grid.getSelectionModel(); 獲取選擇模式
var record;
if(!selModel.hasSelection()){
Ext.Msg.alert('警告','請選擇要修改的行!');
return;
}
selModel.getSelections().length; //選擇的行數(shù)
record = selModel.getSelected(); //獲取選擇行的數(shù)據(jù)
(1)獲取數(shù)據(jù) :
單行
id = record.get('id');
或者
id = record.data.id;
多行
record[i].get('ddd')
(2)刪除數(shù)據(jù) :
var obj = grid.getSelectionModel().getSelected(); store.remove(obj); grid.getView().refresh();
(3)查詢
store.baseParams['memid'] = fb.form.findField('memid').getValue();
store.baseParams['start'] = 0;
store.load();
(4)添加一行列 :
Ext自己帶的一個插件
需要文件 RowExpander.js
var expand = new Ext.ux.grid.RowExpander({
tpl : new Ext.Template(
'<p>{address}</p>'
)
});
在grid的columns中加 expand,
并在grid屬性中加 plugins: expand
- ExtJs中g(shù)ridpanel分組后組名排序?qū)嵗a
- Extjs4實現(xiàn)兩個GridPanel之間數(shù)據(jù)拖拽功能具體方法
- ExtJs設(shè)置GridPanel表格文本垂直居中示例
- Extjs4 GridPanel 的幾種樣式使用介紹
- Extjs4 GridPanel的主要配置參數(shù)詳細(xì)介紹
- Extjs中的GridPanel隱藏列會顯示在menuDisabled中解決方法
- Extjs EditorGridPanel中ComboBox列的顯示問題
- Extjs gridpanel 出現(xiàn)橫向滾動條問題的解決方法
- ExtJs GridPanel簡單的增刪改實現(xiàn)代碼
- ExtJS 2.0 GridPanel基本表格簡明教程
- ExtJS GridPanel 根據(jù)條件改變字體顏色
相關(guān)文章
javascript Ext JS 狀態(tài)默認(rèn)存儲時間
通過ExtJS的源碼可以知道,ExtJS將Grid的自定義顯示列等自定義狀態(tài)信息存入Cookie中,默認(rèn)時間為7天2009-02-02
extjs 學(xué)習(xí)筆記 四 帶分頁的grid
很多時候,我們需要顯示在grid中的數(shù)據(jù)不是短短的幾條或者幾十條,而是成千上萬條。如果讓大量的數(shù)據(jù)一股腦全都顯示在一個頁面中,可以想象會造成什么樣的用戶體驗。2009-10-10
Extjs Ext.MessageBox.confirm 確認(rèn)對話框詳解
顯示一個確認(rèn)對話框,用來代替JavaScript標(biāo)準(zhǔn)的confirm()方法,具有兩個按鈕“是”和“否”如果為其提供一個回調(diào)函數(shù),則該函數(shù)將在單擊按鈕后被調(diào)用(包括右上角的推出按鈕),所單擊按鈕的id將被作為唯一的參數(shù)傳遞到回調(diào)函數(shù)中。2010-04-04
Ext JS框架程序中阻止鍵盤觸發(fā)回退或者刷新頁面的代碼分享
鍵盤上的F5或者退格鍵等按鍵一般來說會觸發(fā)頁面的后退或者刷新事件,然而這些在前端用代碼是可以給屏蔽掉的,這里我們就來看一下Ext JS框架程序中阻止鍵盤觸發(fā)回退或者刷新頁面的代碼分享2016-06-06
基于ExtJs在頁面上window再調(diào)用Window的事件處理方法
下面小編就為大家?guī)硪黄贓xtJs在頁面上window再調(diào)用Window的事件處理方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
ExtJS 2.0實用簡明教程 之Border區(qū)域布局
Border布局由類Ext.layout.BorderLayout定義,布局名稱為border。2009-04-04
ExtJs 學(xué)習(xí)筆記 Hello World!
最近學(xué)ajax,接觸到了Extjs這個強(qiáng)大的框架。我想通過我的學(xué)習(xí)筆記,最后可以讓大家上手在項目中使用Ext。首先我會寫一些基本的用于入門Ext的文章,打好基礎(chǔ)是很重要的。2008-12-12
Extjs中DisplayField的日期或者數(shù)字格式化擴(kuò)展
在用Extjs的時候,有時需要對 Ext.form.DisplyField 進(jìn)行格式化。2010-09-09

