Skip to content

Commit 713f230

Browse files
committed
refactor: cleanup selection column changes
1 parent 2d4d2fb commit 713f230

File tree

2 files changed

+22
-44
lines changed

2 files changed

+22
-44
lines changed

packages/vaadin-grid/src/vaadin-grid-array-data-provider-mixin.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const ArrayDataProviderMixin = (superClass) =>
2626
return ['__dataProviderOrItemsChanged(dataProvider, items, isAttached, items.*, _filters, _sorters)'];
2727
}
2828

29+
/** @private */
2930
__setArrayDataProvider(items) {
3031
const arrayDataProvider = createArrayDataProvider(this.items, {});
3132
arrayDataProvider.__items = items;
@@ -36,13 +37,15 @@ export const ArrayDataProviderMixin = (superClass) =>
3637
});
3738
}
3839

40+
/** @private */
3941
__unsetArrayDataProvider() {
4042
this.setProperties({
4143
_arrayDataProvider: undefined,
4244
items: undefined // TODO: selection column just checks for grid.items in some logic. Add a test which adds items and then a custom dp.
4345
});
4446
}
4547

48+
/** @private */
4649
__dataProviderOrItemsChanged(dataProvider, items, isAttached) {
4750
if (!isAttached) {
4851
return;

packages/vaadin-grid/src/vaadin-grid-selection-column.js

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,7 @@ class GridSelectionColumnElement extends GridColumnElement {
178178
return;
179179
}
180180

181-
if (this.__gridUsesArrayDataProvider() && selectAll) {
182-
this.__withFilteredItemsArray((items) => (this._grid.selectedItems = items));
183-
} else {
184-
this._grid.selectedItems = [];
185-
}
181+
this._grid.selectedItems = selectAll && Array.isArray(this._grid.items) ? this.__getRootLevelItems() : [];
186182
}
187183

188184
/**
@@ -249,54 +245,33 @@ class GridSelectionColumnElement extends GridColumnElement {
249245
this.__previousActiveItem = activeItem;
250246
}
251247

248+
/** @private */
249+
__getRootLevelItems() {
250+
const rootCache = this._grid._cache;
251+
return [...Array(rootCache.size)].map((_, idx) => rootCache.items[idx]);
252+
}
253+
252254
/** @private */
253255
__onSelectedItemsChanged() {
254256
this._selectAllChangeLock = true;
255-
if (this.__gridUsesArrayDataProvider()) {
256-
this.__withFilteredItemsArray((items) => {
257-
if (!this._grid.selectedItems.length) {
258-
this.selectAll = false;
259-
this.__indeterminate = false;
260-
} else if (this.__arrayContains(this._grid.selectedItems, items)) {
261-
this.selectAll = true;
262-
this.__indeterminate = false;
263-
} else {
264-
this.selectAll = false;
265-
this.__indeterminate = true;
266-
}
267-
});
257+
if (Array.isArray(this._grid.items)) {
258+
if (!this._grid.selectedItems.length) {
259+
this.selectAll = false;
260+
this.__indeterminate = false;
261+
} else if (this.__arrayContains(this._grid.selectedItems, this.__getRootLevelItems())) {
262+
this.selectAll = true;
263+
this.__indeterminate = false;
264+
} else {
265+
this.selectAll = false;
266+
this.__indeterminate = true;
267+
}
268268
}
269269
this._selectAllChangeLock = false;
270270
}
271271

272272
/** @private */
273273
__onDataProviderChanged() {
274-
this.__selectAllHidden = !this.__gridUsesArrayDataProvider();
275-
}
276-
277-
/**
278-
* Assuming the grid uses an items array data provider, fetches all the filtered items
279-
* from the data provider and invokes the callback with the resulting array.
280-
*
281-
* @private
282-
**/
283-
__withFilteredItemsArray(callback) {
284-
const params = {
285-
page: 0,
286-
pageSize: Infinity,
287-
sortOrders: [],
288-
filters: this._grid._mapFilters()
289-
};
290-
this._grid.dataProvider(params, (items) => callback(items));
291-
}
292-
293-
/**
294-
* Checks whether the grid's dataProvider is an array data provider
295-
*
296-
* @private
297-
**/
298-
__gridUsesArrayDataProvider() {
299-
return this._grid._arrayDataProvider;
274+
this.__selectAllHidden = !Array.isArray(this._grid.items);
300275
}
301276
}
302277

0 commit comments

Comments
 (0)