-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid-panel-events-example.html
executable file
·221 lines (186 loc) · 6.98 KB
/
grid-panel-events-example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<html>
<head>
<title>Stores</title>
<link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-all.css" />
<style>
body{
background-color:#aeaeae;
}
#container{
background-color:#ccc;
}
</style>
<script src="extjs/adapter/ext/ext-base.js"></script>
<script src="extjs/ext-all-debug.js"></script>
<script>
Ext.BLANK_IMAGE_URL = 'extjs/resources/images/default/s.gif';
// mapping of the fields as we'll get them from the remote source
// could also just list off each fieldname (not as a config object) if the name matches...
// recordFields = ['id', 'firstname', 'lastname']...
var recordFields = [
{name: 'id', mapping: 'id'},
{name: 'firstname', mapping: 'firstname'},
{name: 'lastname', mapping: 'lastname'},
{name: 'street', mapping: 'street'},
{name: 'city', mapping: 'city'},
{name: 'state', mapping: 'state'},
{name: 'zip', mapping: 'zip'},
{name: 'department', mapping: 'department'}
];
var remoteJsonStore = new Ext.data.JsonStore({
fields : recordFields, // for field mapping
// by specifying the url property, the store will auto-create a Proxy for us
// in this case, a ScriptTagProxy.. but it didnt' work
//url : 'http://extjsinaction.com/dataQuery.php',
proxy : new Ext.data.ScriptTagProxy( {
url : 'http://extjsinaction.com/dataQuery.php'
}),
totalProperty : 'totalCount', // optional field for the "total" results, for pagination
root : 'records', // important: the property where the data is found
id : 'ourRemoteStore',
autoLoad : false, // tell store NOT to auto-fetch upon initialization
remoteSort : true // tell store NOt to sort locally, due to paginated results
});
// not using an xtype for the store (xtype: jsonstore) because we will later bind it to GridPanel and PagingToolbar...
// we'll set up some styling functions for later
var colorTextBlue = function(id) {
return '<span style="color: #0000FF;">' + id + '</span>';
};
// custom renderers are called with 6 arguments... we'll only use the first and third.
var stylizeAddress = function(street, column, record) {
var city = record.get('city');
var state = record.get('state');
var zip = record.get('zip');
return String.format('{0}<br>{1} {2}, {3}', street, city, state, zip );
};
// now for our column model, mapping fields (in the store) to columns
// instead of an instance of Ext.grid.ColumnModel however we're just keeping
// track of an array of the Column configs to use later..
var columnModel = [
{
header: 'ID',
dataIndex : 'id',
sortable : true,
width : 50,
resizable : false,
hidden : true,
renderer : colorTextBlue // an example of a custom renderer function
},
{
header : 'Last Name',
dataIndex : 'lastname',
sortable : true,
hideable : false,
width: 75
},
{
header : 'First Name',
dataIndex : 'firstname',
sortable : true,
hideable : false,
width :75
},
{
header : 'Address',
dataIndex : 'street',
sortable : false,
id : 'addressCol',
renderer : stylizeAddress
},
{
header : 'Department',
dataIndex : 'department',
sortable : true,
width : 150
}
];
// paging toolbar for pagination, as an Xtype config object
var pagingToolbar = {
xtype : 'paging',
store : remoteJsonStore,
pageSize : 50, // will use this and the "totalCount" property from server to set up pagination counts
displayInfo : true // display current page + total pages
};
// Events...
// a few event handlers
// display a message box
var doMsgBoxAlert = function(thisGrid) {
// "selModel" is the RowSelectionModel, which has a method getSelected() for the Record
var record = thisGrid.selModel.getSelected();
var firstName = record.get('firstname');
var lastName = record.get('lastname');
var msg = String.format(
'The record you chose:<br /> {0}, {1}',
lastName ,
firstName
);
Ext.MessageBox.alert('', msg);
};
var doRowDblClick = function(thisGrid) {
doMsgBoxAlert(thisGrid);
};
var doRowCtxMenu = function(thisGrid, rowIndex, evtObj) {
evtObj.stopEvent();
// another means of getting the SelectionModel
// and in this case we are selecting a row at the index that was clicked
thisGrid.getSelectionModel().selectRow(rowIndex);
// this is a technique to avoid re-instantiating objects more than once
if (!thisGrid.rowCtxMenu) {
thisGrid.rowCtxMenu = new Ext.menu.Menu({
items : {
text : 'View Record',
handler : function() {
doMsgBoxAlert(thisGrid);
}
}
});
}
thisGrid.rowCtxMenu.showAt(evtObj.getXY());
};
//
var grid = {
xtype : 'grid', // GridPanel xtype
columns : columnModel,
store : remoteJsonStore,
loadMask : true, // auto mask when loading and paginating
bbar : pagingToolbar,
autoExpandColumn : 'addressCol',
selModel : new Ext.grid.RowSelectionModel({
singleSelect: true
}),
stripRows: true,
listeners: {
rowdblclick: doRowDblClick,
rowcontextmenu: doRowCtxMenu,
// you might want to also destroy this UI widget if it was instantiated
destroy: function(thisGrid) {
// always passes the component being destroyed
if (thisGrid.rowCtxMenu) {
thisGrid.rowCtxMenu.destroy();
}
}
}
};
Ext.onReady(function () {
// now a container for our grid!
new Ext.Window({
height : 350,
width : 550,
border : false,
layout : 'fit',
items : grid
}).show();
// and we should tell the Store to load up since we disabled auto loading
Ext.StoreMgr.get('ourRemoteStore').load({
params : {
start : 0,
limit : 50
}
});
});
</script>
</head>
<body>
<div id="container"></div>
</body>
</html>