Skip to content

Commit 5a7d5b7

Browse files
committed
fix: prevent unnecessary page fetches when scrolling to index
1 parent bec0993 commit 5a7d5b7

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/vaadin-grid-scroller.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@
8282
this._scrollingToIndex = true;
8383
index = Math.min(Math.max(index, 0), this._effectiveSize - 1);
8484
this.$.table.scrollTop = index / this._effectiveSize * (this.$.table.scrollHeight - this.$.table.offsetHeight);
85+
86+
// We need to run the iron-list scroll handler here in order to recalculate the scaling from effective size to
87+
// virtual size after changing the scroll position. However we don't want to trigger updates to the items / rows
88+
// in this step, which could result in data provider requests for the previous viewport
89+
this._preventItemUpdates = true;
8590
this._scrollHandler();
91+
this._preventItemUpdates = false;
8692

8793
if (this._accessIronListAPI(() => this._maxScrollTop) && this._virtualCount < this._effectiveSize) {
8894
this._adjustVirtualIndexOffset(1000000);
@@ -259,6 +265,10 @@
259265
* @protected
260266
*/
261267
_assignModels(itemSet) {
268+
// Skip here if internal flag for preventing item updates is set
269+
if (this._preventItemUpdates) {
270+
return;
271+
}
262272
this._iterateItems((pidx, vidx) => {
263273
const el = this._physicalItems[pidx];
264274
this._toggleAttribute('hidden', vidx >= this._effectiveSize, el);

test/scroll-to-index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,24 @@
207207
};
208208
});
209209

210+
it('should not request to load page for previous viewport when clearing items from cache', () => {
211+
grid.dataProvider = sinon.spy(infiniteDataProvider);
212+
// Clear initial data provider request from spy
213+
grid.dataProvider.reset();
214+
// Remove item from current viewport from cache
215+
// This simulates Flow component behavior, which clears pages for the
216+
// previous viewport after loading pages for the new viewport
217+
delete grid._cache.items[0];
218+
219+
// Scroll to new viewport
220+
grid.scrollToIndex(1000);
221+
222+
// Verify data provider was not called with page for previous viewport (page 0)
223+
grid.dataProvider.args.forEach(args => {
224+
expect(args[0].page).not.to.eql(0);
225+
});
226+
});
227+
210228
});
211229

212230
describe('Added item', () => {

0 commit comments

Comments
 (0)