Browse Source

Added virtual scroll options and set false by default

zhixin 6 years ago
parent
commit
fee9f31c13

+ 30 - 0
site/docs/api/table-options.md

@@ -145,6 +145,36 @@ The table options are defined in `jQuery.fn.bootstrapTable.defaults`.
 
 
 - **Example:** [Table Locale](https://examples.bootstrap-table.com/#options/table-locale.html)
 - **Example:** [Table Locale](https://examples.bootstrap-table.com/#options/table-locale.html)
 
 
+## virtualScroll
+
+- **Attribute:** `data-virtual-scroll`
+
+- **Type:** `Boolean`
+
+- **Detail:**
+
+  Set `true` to enable virtual scroll to displays a virtual, "infinite" list.
+
+- **Default:** `false`
+
+- **Example:** [Large Data](https://examples.bootstrap-table.com/#options/large-data.html)
+
+## virtualScrollItemHeight
+
+- **Attribute:** `data-virtual-scroll-item-height`
+
+- **Type:** `Number`
+
+- **Detail:**
+
+  If this option is not define, we will use the height of the first item by default.
+
+  It is **important** to provide this if virtual item height will be significantly larger than the default height. This dimension is used to help determine how many cells should be created when initialized, and to help calculate the height of the scrollable area. This height value can only use `px` units.
+
+- **Default:** `undefined`
+
+- **Example:** [Virtual Scroll Item Height](https://examples.bootstrap-table.com/#options/virtual-scroll-item-height.html)
+
 ## sortable
 ## sortable
 
 
 - **Attribute:** `data-sortable`
 - **Attribute:** `data-sortable`

+ 3 - 2
src/bootstrap-table.js

@@ -1382,7 +1382,7 @@ class BootstrapTable {
       const tr = this.initRow(item, i, data, trFragments)
       const tr = this.initRow(item, i, data, trFragments)
       hasTr = hasTr || !!tr
       hasTr = hasTr || !!tr
       if (tr && typeof tr === 'string') {
       if (tr && typeof tr === 'string') {
-        if (this.virtualScrollDisabled) {
+        if (!this.options.virtualScroll) {
           trFragments.append(tr)
           trFragments.append(tr)
         } else {
         } else {
           rows.push(tr)
           rows.push(tr)
@@ -1396,7 +1396,7 @@ class BootstrapTable {
         this.$header.find('th').length,
         this.$header.find('th').length,
         this.options.formatNoMatches())}</tr>`)
         this.options.formatNoMatches())}</tr>`)
     } else {
     } else {
-      if (this.virtualScrollDisabled) {
+      if (!this.options.virtualScroll) {
         this.$body.html(trFragments)
         this.$body.html(trFragments)
       } else {
       } else {
         if (this.virtualScroll) {
         if (this.virtualScroll) {
@@ -1406,6 +1406,7 @@ class BootstrapTable {
           rows,
           rows,
           scrollEl: this.$tableBody[0],
           scrollEl: this.$tableBody[0],
           contentEl: this.$body[0],
           contentEl: this.$body[0],
+          itemHeight: this.options.virtualScrollItemHeight,
           callback: () => {
           callback: () => {
             this.fitHeader()
             this.fitHeader()
           }
           }

+ 2 - 0
src/constants/index.js

@@ -118,6 +118,8 @@ const DEFAULTS = {
   },
   },
   undefinedText: '-',
   undefinedText: '-',
   locale: undefined,
   locale: undefined,
+  virtualScroll: false,
+  virtualScrollItemHeight: undefined,
   sortable: true,
   sortable: true,
   sortClass: undefined,
   sortClass: undefined,
   silentSort: true,
   silentSort: true,

+ 4 - 2
src/extensions/export/bootstrap-table-export.js

@@ -225,13 +225,15 @@ $.BootstrapTable = class extends $.BootstrapTable {
     if (o.exportDataType === 'all' && o.pagination) {
     if (o.exportDataType === 'all' && o.pagination) {
       const eventName = o.sidePagination === 'server'
       const eventName = o.sidePagination === 'server'
         ? 'post-body.bs.table' : 'page-change.bs.table'
         ? 'post-body.bs.table' : 'page-change.bs.table'
+      const virtualScroll = this.options.virtualScroll
+
       this.$el.one(eventName, () => {
       this.$el.one(eventName, () => {
         doExport(() => {
         doExport(() => {
-          this.virtualScrollDisabled = false
+          this.options.virtualScroll = virtualScroll
           this.togglePagination()
           this.togglePagination()
         })
         })
       })
       })
-      this.virtualScrollDisabled = true
+      this.options.virtualScroll = false
       this.togglePagination()
       this.togglePagination()
       this.trigger('export-saved', this.getData())
       this.trigger('export-saved', this.getData())
     } else if (o.exportDataType === 'selected') {
     } else if (o.exportDataType === 'selected') {

+ 1 - 1
src/extensions/treegrid/bootstrap-table-treegrid.js

@@ -32,7 +32,7 @@ $.BootstrapTable = class extends $.BootstrapTable {
   }
   }
 
 
   initBody (...args) {
   initBody (...args) {
-    this.virtualScrollDisabled = this.treeEnable
+    this.options.virtualScroll = !this.treeEnable
     super.initBody(...args)
     super.initBody(...args)
   }
   }
 
 

+ 6 - 3
src/virtual-scroll/index.js

@@ -8,6 +8,7 @@ class VirtualScroll {
     this.scrollEl = options.scrollEl
     this.scrollEl = options.scrollEl
     this.contentEl = options.contentEl
     this.contentEl = options.contentEl
     this.callback = options.callback
     this.callback = options.callback
+    this.itemHeight = options.itemHeight
 
 
     this.cache = {}
     this.cache = {}
     this.scrollTop = this.scrollEl.scrollTop
     this.scrollTop = this.scrollEl.scrollTop
@@ -59,9 +60,11 @@ class VirtualScroll {
   }
   }
 
 
   getRowsHeight () {
   getRowsHeight () {
-    const nodes = this.contentEl.children
-    const node = nodes[Math.floor(nodes.length / 2)]
-    this.itemHeight = node.offsetHeight
+    if (typeof this.itemHeight === 'undefined') {
+      const nodes = this.contentEl.children
+      const node = nodes[Math.floor(nodes.length / 2)]
+      this.itemHeight = node.offsetHeight
+    }
     this.blockHeight = this.itemHeight * BLOCK_ROWS
     this.blockHeight = this.itemHeight * BLOCK_ROWS
     this.clusterRows = BLOCK_ROWS * CLUSTER_BLOCKS
     this.clusterRows = BLOCK_ROWS * CLUSTER_BLOCKS
     this.clusterHeight = this.blockHeight * CLUSTER_BLOCKS
     this.clusterHeight = this.blockHeight * CLUSTER_BLOCKS