Skip to content

Commit

Permalink
fix: get filtered grid root items from the data provider (#3021) (#3023)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaadin-bot committed Nov 11, 2021
1 parent 2232031 commit 9f83d15
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
50 changes: 33 additions & 17 deletions packages/grid/src/vaadin-grid-selection-column.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ class GridSelectionColumn extends GridColumn {
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 @@ -250,26 +254,22 @@ class GridSelectionColumn extends GridColumn {
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 @@ -278,6 +278,22 @@ class GridSelectionColumn extends GridColumn {
__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(GridSelectionColumn.is, GridSelectionColumn);
Expand Down
8 changes: 8 additions & 0 deletions packages/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 9f83d15

Please sign in to comment.