Browse Source

Delete jquery from copyvalues and setvalues

djhvscf 7 years ago
parent
commit
28d7e0b918

+ 2 - 0
src/bootstrap-table.js

@@ -9,9 +9,11 @@ import Utils from './utils/index.js'
 import VirtualScroll from './virtual-scroll/index.js'
 import {isNumeric, isEmptyObject} from './types.js'
 import Sort from './sort.js'
+import Polyfill from './polyfill.js'
 
 class BootstrapTable {
   constructor (el, options) {
+    Polyfill()
     this.options = options
     this.$el = $(el)
     this.$el_ = this.$el.clone()

+ 8 - 0
src/dom.js

@@ -72,4 +72,12 @@ export const is = (ele, tag) => {
   }
 
   ele.nodeName.toLowerCase() === tag
+}
+
+export const find = (ele, selector) => {
+  if (isJQueryObject(ele)) {
+    ele = ele[0]
+  }
+
+  return ele.querySelectorAll(selector)
 }

+ 11 - 18
src/extensions/filter-control/bootstrap-table-filter-control.js

@@ -4,7 +4,7 @@
  * @version: v3.0.0
  */
 
-import {createElem, createOpt, is} from '../../dom.js'
+import {createElem, createOpt, is, find} from '../../dom.js'
 import Sort from '../../sort.js'
 import {isEmptyObject} from '../../types.js'
 import Utils from '../../utils/index.js'
@@ -93,23 +93,18 @@ const UtilsFilterControl = {
     }
     return -1
   },
-  setCursorPosition (el) {
-    $(el).val(el.value)
-  },
   copyValues (that) {
     const header = UtilsFilterControl.getCurrentHeader(that)
     const searchControls = UtilsFilterControl.getCurrentSearchControls(that)
 
     that.options.valuesFilterControl = []
 
-    header.find(searchControls).each(function () {
+    find(header, searchControls).forEach((elem, i) => {
       that.options.valuesFilterControl.push({
-        field: $(this)
-          .closest('[data-field]')
-          .data('field'),
-        value: $(this).val(),
-        position: UtilsFilterControl.getCursorPosition($(this).get(0)),
-        hasFocus: is(this, ':focus')
+        field: elem.closest('[data-field]').getAttribute('data-field'),
+        value: elem.value,
+        position: UtilsFilterControl.getCursorPosition(elem),
+        hasFocus: is(elem, ':focus')
       })
     })
   },
@@ -122,24 +117,22 @@ const UtilsFilterControl = {
     if (that.options.valuesFilterControl.length > 0) {
       //  Callback to apply after settings fields values
       let fieldToFocusCallback = null
-      header.find(searchControls).each(function (index, ele) {
-        field = $(this)
-          .closest('[data-field]')
-          .data('field')
+      find(header, searchControls).forEach((elem, i) => {
+        field = elem.closest('[data-field]').getAttribute('data-field')
         result = that.options.valuesFilterControl.filter(valueObj => valueObj.field === field)
 
         if (result.length > 0) {
-          $(this).val(result[0].value)
+          elem.value = result[0].value
           if (result[0].hasFocus) {
             // set callback if the field had the focus.
             fieldToFocusCallback = ((fieldToFocus, carretPosition) => {
               // Closure here to capture the field and cursor position
               const closedCallback = () => {
                 fieldToFocus.focus()
-                UtilsFilterControl.setCursorPosition(fieldToFocus, carretPosition)
+                fieldToFocus.value = fieldToFocus.value
               }
               return closedCallback
-            })($(this).get(0), result[0].position)
+            })(elem, result[0].position)
           }
         }
       })

+ 21 - 0
src/polyfill.js

@@ -0,0 +1,21 @@
+function closest () {
+  if (!Element.prototype.matches) {
+    Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector
+  }
+
+  if (!Element.prototype.closest) {
+    Element.prototype.closest = function (s) {
+      var el = this
+
+      do {
+        if (el.matches(s)) return el
+        el = el.parentElement || el.parentNode
+      } while (el !== null && el.nodeType === 1)
+      return null
+    }
+  }
+}
+
+export default function Polyfill () {
+  closest()
+}