-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPagingSelectionPersistence.js
310 lines (255 loc) · 9.44 KB
/
PagingSelectionPersistence.js
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**
* Grid PagingSelectionPersistence plugin
*
* Maintains row selection state when moving between pages of a paginated grid
*
* Public Methods: getPersistedSelection() - retrieve the array of selected
* records across all pages clearPersistedSelection() - deselect records across
* all pages
*
* {@link http://www.sencha.com/forum/showthread.php?263871-Selection-of-records-over-multiple-pages-in-a-grid-Select-All}
*
* Antoine Verron :
*
* <pre>
* - add getPersistedSelection method to the grid
* - put selection and selected in instance attribute instead of class attribute
* </pre>
*
* @class Ext.ux.grid.plugin.PagingSelectionPersistence
* @extends Ext.AbstractPlugin
* @author Bill Dami, Antoine Verron
* @date December 20th, 2011
*
*/
Ext.define('Ext.ux.grid.plugin.PagingSelectionPersistence', {
alias: 'plugin.pagingselectpersist',
extend: 'Ext.plugin.Abstract',
pluginId: 'pagingselectpersist',
init: function(grid) {
var me = this;
//array of selected records
me.selection = [];
//hash map of record id to selected state
me.selected = {};
me.grid = grid;
me.selModel = me.grid.getSelectionModel();
me.isCheckboxModel = me.selModel.$className == 'Ext.selection.CheckboxModel';
// me.origOnHeaderClick = me.selModel.onHeaderClick; // NOT NEEDED ANYMORE in ExtJs 6
me.bindListeners();
// Psycho
me.grid.getPersistedSelection = Ext.bind(me.getPersistedSelection, me);
me.grid.setPersistedSelection = Ext.bind(me.setPersistedSelection, me);
me.grid.deslectAllPersistedSelection = Ext.bind(me.deselectAll, me);
},
clearSelection: function() {
this.selection = [];
this.selected = {};
},
destroy: function() {
this.clearSelection();
this.disable();
},
enable: function() {
var me = this;
if (me.disabled && me.grid) {
me.grid.getView().on('refresh', me.onViewRefresh, me);
me.selModel.on({
'select': 'onRowSelect',
'deselect': 'onRowDeselect',
scope: me
});
if (me.isCheckboxModel) {
// NOT NEEDED ANYMORE in ExtJs 6
// //For CheckboxModel, we need to detect when the header deselect/select page checkbox
// //is clicked, to make sure the plugin's selection array is updated. This is because Ext.selection.CheckboxModel
// //interally supresses event firings for selectAll/deselectAll when its clicked
// me.selModel.onHeaderClick = function(headerCt, header, e) {
// var isChecked = header.el.hasCls(Ext.baseCSSPrefix + 'grid-hd-checker-on');
// me.origOnHeaderClick.apply(me, arguments);
//
// if (isChecked) {
// me.onDeselectPage();
// }
// else {
// me.onSelectPage();
// }
// };
}
}
me.callParent();
},
disable: function() {
var me = this;
if (me.grid) {
me.grid.getView().un('refresh', me.onViewRefresh, me);
me.selModel.un('select', me.onRowSelect, me);
me.selModel.un('deselect', me.onRowDeselect, me);
// me.selModel.onHeaderClick = me.origOnHeaderClick;
}
me.callParent();
},
bindListeners: function() {
var disabled = this.disabled;
this.disable();
if (!disabled) {
this.enable();
}
},
onViewRefresh: function(view, eOpts) {
var me = this, //
store = me.grid.getStore(), sel = [], hdSelectState, rec, i;
me.ignoreChanges = true;
for (i = store.getCount() - 1; i >= 0; i--) {
rec = store.getAt(i);
if (me.selected[rec.getId()]) {
sel.push(rec);
}
}
me.selModel.select(sel, false);
if (me.isCheckboxModel) {
//For CheckboxModel, make sure the header checkbox is correctly
//checked/unchecked when the view is refreshed depending on the
//selection state of the rows on that page (workaround for possible bug in Ext 4.0.7?)
hdSelectState = me.selModel.selected.getCount() === me.grid.getStore().getCount();
me.selModel.toggleUiHeader(hdSelectState);
}
me.ignoreChanges = false;
},
onRowSelect: function(sm, rec, idx, eOpts) {
// console.debug('onRowSelect', sm, rec, idx, this.ignoreChanges, this.selection, this.selected[rec.getId()]);
if (this.ignoreChanges === true) {
return;
}
this.selectRecord(rec);
},
onRowDeselect: function(sm, rec, idx, eOpts) {
var me = this, i;
// console.debug('onRowDeselect', sm, rec, idx, this.ignoreChanges, this.selection, this.selected[rec.getId()]);
if (me.ignoreChanges === true) {
return;
}
if (me.selected[rec.getId()]) {
for (i = me.selection.length - 1; i >= 0; i--) {
if (me.selection[i].getId() == rec.getId()) {
me.selection.splice(i, 1);
me.selected[rec.getId()] = false;
break;
}
}
}
},
onSelectPage: function() {
var sel = this.selModel.getSelection(), len = this.selection.length, i;
for (i = 0; i < sel.length; i++) {
//alert(i + sel[i]);
this.onRowSelect(this.selModel, sel[i]);
}
if (len !== this.selection.length) {
this.selModel.fireEvent('selectionchange', this.selModel, [], {});
}
},
onDeselectPage: function() {
var store = this.grid.getStore(), len = this.selection.length, i;
for (i = store.getCount() - 1; i >= 0; i--) {
this.onRowDeselect(this.selModel, store.getAt(i));
}
if (len !== this.selection.length) {
this.selModel.fireEvent('selectionchange', this.selModel, [], {});
}
},
setPersistedSelection: function(records) {
this.selectRecords(records);
this.selModel.select(records, true, true);
},
getPersistedSelection: function() {
return [].concat(this.selection);
},
deselectAll: function(suppressEvent) {
this.selModel.deselectAll();
this.clearSelection();
},
getCount: function() {
return this.selection.length;
},
// private
onSelectionClear: function() {
if (!this.ignoreSelectionChanges) {
// selection cleared by user
// also called internally when the selection replaces the old selection
this.selection = [];
this.selected = {};
}
}, // end onSelectionClear
clearPersistedSelection: function() {
var changed = this.selection.length > 0;
this.selection = [];
this.selected = {};
this.onViewRefresh();
if (changed) {
this.selModel.fireEvent('selectionchange', this.selModel, [], {});
}
},
/**
* Selects all the rows in the grid, including those on other page Be very
* careful using this on very large datasets
*/
selectAll: function() {
var storeB = this.grid.getStore();
storeB.suspendEvents();
//alert(storeB.getTotalCount());
storeB.load({
params: {
start: 0,
limit: storeB.getTotalCount()
},
callback: function(records, operation, success) {
if (records.length > 0) { // Issue is here: Records returns as NULL
//alert('Num Records: ' + records.length);
this.selectRecords(records);
storeB.resumeEvents();
this.onViewRefresh();
/*
* this.selection = storeB.data.items.slice(0);
* this.selected = {}; for (var i = this.selection.length -
* 1; i >= 0; i--) { this.selected[this.selection[i].id] =
* true; };
*/
}
else {
console.warn('No records');
}
},
scope: this
});
// this.selectRecord(rec);
},
countAll: function() {
var storeA = this.grid.getStore();
// <debug>
console.info('store count ' + storeA.getCount());
console.info('store count ' + storeA.getTotalCount());
// </debug>
return storeA.getCount();
},
privates: {
selectRecords: function(records) {
records = records || [];
if (!Ext.isArray(records)) {
records = [
records
];
}
for (var i = records.length - 1; i >= 0; i--) {
this.selectRecord(records[i]);
}
},
selectRecord: function(record) {
var id = record.getId();
if (!this.selected[id]) {
this.selection.push(record);
this.selected[id] = true;
}
}
}
});