ExtJS:GridPanel之renderer:function()和itemdblclick : function()方法參數詳解,extjsgridpanel
要使用GridPanel,首先要定義Store,而在建立Store的時候必須要有Model,因此我們首先來定義Model:
Ext.define("Gpsdata", { extend: "Ext.data.Model", fields: [ { name: 'gpstime', type: 'string' }, { name: 'gpstemp', type: 'string' } ]});
然後建立Store:
var surfacestore = new Ext.data.Store({ model : 'Gpsdata', pageSize : 22, proxy : { type : 'ajax', url : 'gpsdata/grid', reader : { type : 'json', root : 'dataOfAll', totalProperty : 'total' } }, listeners : {beforeload : function(store, operation, opts) {Ext.MessageBox.show({title : '請等待',msg : '資料讀取中……',width : 240,progress : true,closable : false});Ext.MessageBox.wait('資料讀取中……', '', {duration : 900000,interval : 500,increment : 10,scope : this,fn : function() {}});},load : function(store, records, successful, opts) {if (successful) {Ext.MessageBox.hide();} else {Ext.MessageBox.hide();}}} });
建立GridPanel
var surfacegrid = Ext.create('Ext.grid.Panel', { id: 'surface', title: '資料列表展示', store: surfacestore, disableSelection: true, loadMask: true, viewConfig: { trackOver: false, stripeRows: false }, columns:[ new Ext.grid.RowNumberer(),{ text: "時間", dataIndex: 'gpstime', sortable: true, flex: 3 },{ text: "水溫"+'(℃)', renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){ //value="<span style='color:red;font-weight:bold;'>紅男</span>"; return value; }, dataIndex: 'gpstemp', flex: 2 }], listeners: { itemdblclick : function(view, record, item, index, e, eOpts) { var gpsname=record.get('gpsname'); //alert(gpsname); }}, bbar: Ext.create('Ext.PagingToolbar', { store: surfacestore, displayInfo: true, displayMsg: '顯示監測資料 {0} - {1} of {2}', emptyMsg: "無資料" }) });
renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){}
解析:
value:將要像是儲存格裡的值,即dataIndex的值
cellmeta:儲存格的相關屬性,主要是id和CSS
record:這行的資料對象。通過record.data['id']方式得到其他列的值
rowIndex:行號
columnIndex:當前列的行號
store:構造表格時傳遞的ds,也就是說,表格裡所有的資料都可以通過store獲得。
listeners: {itemdblclick : function(view, record, item, index, e, eOpts) { }}
解析:
view:Ext.view.View
record : Ext.data.ModelThe record that belongs to the item
item : HTMLElementThe item's element
index : NumberThe item's index
e : Ext.EventObjectThe raw event object
eOpts : ObjectThe options object passed to Ext.util.Observable.addListener.
Extjs GridPanel的列
Extjs GridPanel的列有多種類型,例如:文本列、數字列、日期列、選擇框列、操作列等。我們可以通過xtype來指定不同的列類型。
下面是列的常用配置項:
xtype:列類型
text:列頭顯示的文字
dataIndex:綁定的欄位名
width:寬度
flex:自動適應的寬度
sortable:是否可排序,預設為是
hideable:是否可隱藏,預設為是
locked:鎖定列,將列鎖定在grid的開頭,當grid出現捲軸的時候該屬性比較有用。預設為否
lockable:是否可鎖定,預設為否
format:格式化字串,常用於日期、數位格式化。日期:'Y-m-d';日期時間:'Y-m-d H:i:s';數字:'0,000.00'(帶有千位分隔字元、保留兩位小數)、'0.00'(保留兩位小數),'0'(不保留小數)
renderer:自訂繪製方法,可以是Ext.util.Format中定義好的方法名稱,也可以是自訂否function,該方法接收下面的參數:value、metadata、record、rowIndex、colIndex、store、view,並需要一個用來顯示的傳回值。
editor:編輯器,當使用編輯外掛程式的時候才會起作用。
Extjs GridPanel行選擇模型(SelectionModel)
控制Extjs GridPanel行選擇模型的兩個配置項是selType和selModel。預設情況下,selType為rowmodel,對應的Ext.selection.Model。這種選擇模型不會在grid中添加複選框,它通過點擊行進行選中,預設為多選“MULTI”。
如果我們要使用複選框來選擇行,我們需要使用下面的配置:
selType: "checkboxmodel",
然後,我們可以通過selModel來配置selType:
selModel: {
injectCheckbox: 0,
mode: "SIMPLE", //"SINGLE"/"SIMPLE"/"MULTI"
checkOnly: true //只能通過checkbox選擇
},
Extjs GridPanel行選擇
除了介面操作來選中行,我們還可以通過代碼來選中行:
//選擇行,並保持其他行的選擇狀態
grid.getSelectionModel().select(records, true);
//選擇所有
grid.getSelectionModel().selectAll();
//根據row index選擇
grid.getSelectionModel().selectRange(startRow, endRow, true)
Extjs GridPanel擷取選中行
擷取選中行,仍然需要通過SelectionModel來完成:
var records = grid.getSelectionModel().getSelection();