浏览代码

improved scrollTo function to allow units (new unit is rows) (#4240)

* improved scrollTo function to allow units (new unit is rows)
Dustin U 6 年之前
父节点
当前提交
66c616804b
共有 2 个文件被更改,包括 26 次插入9 次删除
  1. 7 3
      site/docs/api/methods.md
  2. 19 6
      src/bootstrap-table.js

+ 7 - 3
site/docs/api/methods.md

@@ -381,12 +381,16 @@ The calling method syntax: `$('#table').bootstrapTable('method', parameter)`.
 
 ## scrollTo
 
-- **Parameter:** `value`
+- **Parameter:** `value|object`
 
 - **Detail:**
 
-  Scroll to the number `value` position, the unit is `'px'`, set `'bottom'` means scroll to the bottom.
-
+  - value
+    - Scroll to the number `value` position, the unit is `'px'`, set `'bottom'` means scroll to the bottom.
+  - object
+    -  Scroll to the unit (`px` or `rows (index starts by 0)`)   
+    Default: `{unit: 'px', value: 0}`
+    
 ## getScrollPosition
 
 - **Parameter:** `undefined`

+ 19 - 6
src/bootstrap-table.js

@@ -3150,16 +3150,29 @@
       this.updatePagination()
     }
 
-    scrollTo (_value) {
-      if (typeof _value === 'undefined') {
+    scrollTo (params) {
+      if (typeof params === 'undefined') {
         return this.$tableBody.scrollTop()
       }
 
-      let value = _value
-      if (typeof _value === 'string' && _value === 'bottom') {
-        value = this.$tableBody[0].scrollHeight
+      let options = {unit: 'px', value: 0}
+      if (typeof params === 'object') {
+        options = Object.assign(options, params)
+      } else if (typeof params === 'string' && params === 'bottom') {
+        options.value = this.$tableBody[0].scrollHeight
+      } else if (typeof params === 'string') {
+        options.value = params
       }
-      this.$tableBody.scrollTop(value)
+
+      let scrollTo = options.value
+      if (options.unit === 'rows') {
+        scrollTo = 0
+        this.$body.find(`> tr:lt(${options.value})`).each((i, el) => {
+          scrollTo += $(el).outerHeight(true)
+        })
+      }
+
+      this.$tableBody.scrollTop(scrollTo)
     }
 
     getScrollPosition () {