Skip to content

Commit 5cbdc32

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

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
@@ -206,6 +206,24 @@
206206
};
207207
});
208208

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

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

0 commit comments

Comments
 (0)