Skip to content

Commit

Permalink
fix: get filtered grid root items from the data provider (#3021) (#3024)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomivirkki authored Nov 11, 2021
1 parent 53ee536 commit 54f7dfe
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
50 changes: 33 additions & 17 deletions packages/vaadin-grid/src/vaadin-grid-selection-column.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ class GridSelectionColumnElement extends GridColumnElement {
return;
}

this._grid.selectedItems = selectAll && Array.isArray(this._grid.items) ? this.__getRootLevelItems() : [];
if (selectAll && Array.isArray(this._grid.items)) {
this.__withFilteredItemsArray((items) => (this._grid.selectedItems = items));
} else {
this._grid.selectedItems = [];
}
}

/**
Expand Down Expand Up @@ -251,26 +255,22 @@ class GridSelectionColumnElement extends GridColumnElement {
this.__previousActiveItem = activeItem;
}

/** @private */
__getRootLevelItems() {
const rootCache = this._grid._cache;
return [...Array(rootCache.size)].map((_, idx) => rootCache.items[idx]);
}

/** @private */
__onSelectedItemsChanged() {
this._selectAllChangeLock = true;
if (Array.isArray(this._grid.items)) {
if (!this._grid.selectedItems.length) {
this.selectAll = false;
this.__indeterminate = false;
} else if (this.__arrayContains(this._grid.selectedItems, this.__getRootLevelItems())) {
this.selectAll = true;
this.__indeterminate = false;
} else {
this.selectAll = false;
this.__indeterminate = true;
}
this.__withFilteredItemsArray((items) => {
if (!this._grid.selectedItems.length) {
this.selectAll = false;
this.__indeterminate = false;
} else if (this.__arrayContains(this._grid.selectedItems, items)) {
this.selectAll = true;
this.__indeterminate = false;
} else {
this.selectAll = false;
this.__indeterminate = true;
}
});
}
this._selectAllChangeLock = false;
}
Expand All @@ -279,6 +279,22 @@ class GridSelectionColumnElement extends GridColumnElement {
__onDataProviderChanged() {
this.__selectAllHidden = !Array.isArray(this._grid.items);
}

/**
* Assuming the grid uses an items array data provider, fetches all the filtered items
* from the data provider and invokes the callback with the resulting array.
*
* @private
*/
__withFilteredItemsArray(callback) {
const params = {
page: 0,
pageSize: Infinity,
sortOrders: [],
filters: this._grid._mapFilters()
};
this._grid.dataProvider(params, (items) => callback(items));
}
}

customElements.define(GridSelectionColumnElement.is, GridSelectionColumnElement);
Expand Down
8 changes: 8 additions & 0 deletions packages/vaadin-grid/test/selection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,4 +495,12 @@ describe('multi selection column', () => {
expect(firstBodyCellContent.textContent).to.equal('foo');
expect(firstHeaderCellContent.firstElementChild).to.equal(selectAllCheckbox);
});

it('should select all items when select all is set', () => {
grid.items = Array.from({ length: 60 }, (_, key) => ++key);

selectionColumn.selectAll = true;

expect(grid.selectedItems).to.eql(grid.items);
});
});

0 comments on commit 54f7dfe

Please sign in to comment.