Maintain scrollTop when changing item count

master
Vitaliy Filippov 2017-07-06 00:47:09 +03:00
parent 70488eeb8e
commit 3cad0959e6
1 changed files with 7 additions and 8 deletions

View File

@ -470,8 +470,6 @@ TreeGrid.prototype.syncView = function()
{ {
if (!this.table.offsetParent) if (!this.table.offsetParent)
return; return;
var scrollPos = this.tableWrapper.scrollHeight > this.tableWrapper.offsetHeight ?
this.tableWrapper.scrollTop / (this.tableWrapper.scrollHeight - this.tableWrapper.offsetHeight) : 0;
var offsetHeight = this.wrapper.offsetHeight - this.thead.offsetHeight; var offsetHeight = this.wrapper.offsetHeight - this.thead.offsetHeight;
var visibleItems = Math.ceil(offsetHeight/this.minItemHeight); var visibleItems = Math.ceil(offsetHeight/this.minItemHeight);
var endStart = this.nodeCount - visibleItems; var endStart = this.nodeCount - visibleItems;
@ -486,6 +484,12 @@ TreeGrid.prototype.syncView = function()
{ {
// Calculate virtual scroll // Calculate virtual scroll
var lastFirst = endStart+this._findVisibleNodeOffset(lastNodes, offsetHeight); var lastFirst = endStart+this._findVisibleNodeOffset(lastNodes, offsetHeight);
var avgItemHeight = this.endItemHeight / lastNodes.length;
avgItemHeight = (avgItemHeight < this.minItemHeight ? this.minItemHeight : avgItemHeight);
var scrollPos = this.nodeCount*avgItemHeight > this.tableWrapper.offsetHeight ?
this.tableWrapper.scrollTop / (this.nodeCount*avgItemHeight - this.tableWrapper.offsetHeight) : 0;
if (scrollPos > 1)
scrollPos = 1;
var firstVisible = scrollPos*lastFirst; var firstVisible = scrollPos*lastFirst;
var rangeStart = Math.floor(firstVisible); var rangeStart = Math.floor(firstVisible);
var rangeCount = visibleItems; var rangeCount = visibleItems;
@ -510,10 +514,7 @@ TreeGrid.prototype.syncView = function()
var visibleNodes = this._findNodes(this.root, rangeStart, rangeCount); var visibleNodes = this._findNodes(this.root, rangeStart, rangeCount);
this._renderNodes(visibleNodes); this._renderNodes(visibleNodes);
this._syncStart(visibleNodes); this._syncStart(visibleNodes);
if (endStart > rangeStart+rangeCount) this._addPlaceholder('mid');
{
this._addPlaceholder('mid', 0);
}
firstItemHeight = getNodeHeight(visibleNodes[0])*(firstVisible-rangeStart); firstItemHeight = getNodeHeight(visibleNodes[0])*(firstVisible-rangeStart);
} }
this._setPlaceholder('top', this.tableWrapper.scrollTop-firstItemHeight); this._setPlaceholder('top', this.tableWrapper.scrollTop-firstItemHeight);
@ -546,8 +547,6 @@ TreeGrid.prototype.syncView = function()
} }
if (endStart > 0) if (endStart > 0)
{ {
var avgItemHeight = this.endItemHeight / lastNodes.length;
avgItemHeight = (avgItemHeight < this.minItemHeight ? this.minItemHeight : avgItemHeight);
this._setPlaceholder('mid', this.nodeCount*avgItemHeight - total - (this.tableWrapper.scrollTop-firstItemHeight)); this._setPlaceholder('mid', this.nodeCount*avgItemHeight - total - (this.tableWrapper.scrollTop-firstItemHeight));
} }
} }