Browse Source

Merge pull request #4615 from wenzhixin/fix/4524

Fixed virtual scroll to top error when using append method
Dustin Utecht 6 years ago
parent
commit
295a7af449
2 changed files with 11 additions and 5 deletions
  1. 1 0
      src/bootstrap-table.js
  2. 10 5
      src/virtual-scroll/index.js

+ 1 - 0
src/bootstrap-table.js

@@ -1435,6 +1435,7 @@ class BootstrapTable {
         }
         }
         this.virtualScroll = new VirtualScroll({
         this.virtualScroll = new VirtualScroll({
           rows,
           rows,
+          fixedScroll,
           scrollEl: this.$tableBody[0],
           scrollEl: this.$tableBody[0],
           contentEl: this.$body[0],
           contentEl: this.$body[0],
           itemHeight: this.options.virtualScrollItemHeight,
           itemHeight: this.options.virtualScrollItemHeight,

+ 10 - 5
src/virtual-scroll/index.js

@@ -13,7 +13,7 @@ class VirtualScroll {
     this.cache = {}
     this.cache = {}
     this.scrollTop = this.scrollEl.scrollTop
     this.scrollTop = this.scrollEl.scrollTop
 
 
-    this.initDOM(this.rows)
+    this.initDOM(this.rows, options.fixedScroll)
 
 
     this.scrollEl.scrollTop = this.scrollTop
     this.scrollEl.scrollTop = this.scrollTop
     this.lastCluster = 0
     this.lastCluster = 0
@@ -32,13 +32,14 @@ class VirtualScroll {
     }
     }
   }
   }
 
 
-  initDOM (rows) {
+  initDOM (rows, fixedScroll) {
     if (typeof this.clusterHeight === 'undefined') {
     if (typeof this.clusterHeight === 'undefined') {
+      this.cache.scrollTop = this.scrollEl.scrollTop
       this.cache.data = this.contentEl.innerHTML = rows[0] + rows[0] + rows[0]
       this.cache.data = this.contentEl.innerHTML = rows[0] + rows[0] + rows[0]
       this.getRowsHeight(rows)
       this.getRowsHeight(rows)
     }
     }
 
 
-    const data = this.initData(rows, this.getNum())
+    const data = this.initData(rows, this.getNum(fixedScroll))
     const thisRows = data.rows.join('')
     const thisRows = data.rows.join('')
     const dataChanged = this.checkChanges('data', thisRows)
     const dataChanged = this.checkChanges('data', thisRows)
     const topOffsetChanged = this.checkChanges('top', data.topOffset)
     const topOffsetChanged = this.checkChanges('top', data.topOffset)
@@ -54,6 +55,10 @@ class VirtualScroll {
         html.push(this.getExtra('bottom', data.bottomOffset))
         html.push(this.getExtra('bottom', data.bottomOffset))
       }
       }
       this.contentEl.innerHTML = html.join('')
       this.contentEl.innerHTML = html.join('')
+
+      if (fixedScroll) {
+        this.contentEl.scrollTop = this.cache.scrollTop
+      }
     } else if (bottomOffsetChanged) {
     } else if (bottomOffsetChanged) {
       this.contentEl.lastChild.style.height = `${data.bottomOffset}px`
       this.contentEl.lastChild.style.height = `${data.bottomOffset}px`
     }
     }
@@ -70,8 +75,8 @@ class VirtualScroll {
     this.clusterHeight = this.blockHeight * CLUSTER_BLOCKS
     this.clusterHeight = this.blockHeight * CLUSTER_BLOCKS
   }
   }
 
 
-  getNum () {
-    this.scrollTop = this.scrollEl.scrollTop
+  getNum (fixedScroll) {
+    this.scrollTop = fixedScroll ? this.cache.scrollTop : this.scrollEl.scrollTop
     return Math.floor(this.scrollTop / (this.clusterHeight - this.blockHeight)) || 0
     return Math.floor(this.scrollTop / (this.clusterHeight - this.blockHeight)) || 0
   }
   }