Browse Source

Merge pull request #4808 from wenzhixin/bugfix/fix-bug-4578

Fix #4578
文翼 5 years ago
parent
commit
b13bedf322

+ 34 - 32
src/extensions/cookie/bootstrap-table-cookie.js

@@ -230,40 +230,42 @@ $.extend($.fn.bootstrapTable.utils, {
 
 $.BootstrapTable = class extends $.BootstrapTable {
   init () {
-    // FilterBy logic
-    const filterByCookie = JSON.parse(UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.filterBy))
-    this.filterColumns = filterByCookie ? filterByCookie : {}
-
-    // FilterControl logic
-    this.options.filterControls = []
-    this.options.filterControlValuesLoaded = false
-
-    this.options.cookiesEnabled = typeof this.options.cookiesEnabled === 'string' ?
-      this.options.cookiesEnabled.replace('[', '').replace(']', '')
-        .replace(/'/g, '').replace(/ /g, '').toLowerCase().split(',') :
-      this.options.cookiesEnabled
-
-    if (this.options.filterControl) {
-      const that = this
-      this.$el.on('column-search.bs.table', (e, field, text) => {
-        let isNewField = true
-
-        for (let i = 0; i < that.options.filterControls.length; i++) {
-          if (that.options.filterControls[i].field === field) {
-            that.options.filterControls[i].text = text
-            isNewField = false
-            break
+    if (this.options.cookie) {
+      // FilterBy logic
+      const filterByCookie = JSON.parse(UtilsCookie.getCookie(this, this.options.cookieIdTable, UtilsCookie.cookieIds.filterBy))
+      this.filterColumns = filterByCookie ? filterByCookie : {}
+
+      // FilterControl logic
+      this.options.filterControls = []
+      this.options.filterControlValuesLoaded = false
+
+      this.options.cookiesEnabled = typeof this.options.cookiesEnabled === 'string' ?
+        this.options.cookiesEnabled.replace('[', '').replace(']', '')
+          .replace(/'/g, '').replace(/ /g, '').toLowerCase().split(',') :
+        this.options.cookiesEnabled
+
+      if (this.options.filterControl) {
+        const that = this
+        this.$el.on('column-search.bs.table', (e, field, text) => {
+          let isNewField = true
+
+          for (let i = 0; i < that.options.filterControls.length; i++) {
+            if (that.options.filterControls[i].field === field) {
+              that.options.filterControls[i].text = text
+              isNewField = false
+              break
+            }
+          }
+          if (isNewField) {
+            that.options.filterControls.push({
+              field,
+              text
+            })
           }
-        }
-        if (isNewField) {
-          that.options.filterControls.push({
-            field,
-            text
-          })
-        }
 
-        UtilsCookie.setCookie(that, UtilsCookie.cookieIds.filterControl, JSON.stringify(that.options.filterControls))
-      }).on('created-controls.bs.table', UtilsCookie.initCookieFilters(that))
+          UtilsCookie.setCookie(that, UtilsCookie.cookieIds.filterControl, JSON.stringify(that.options.filterControls))
+        }).on('created-controls.bs.table', UtilsCookie.initCookieFilters(that))
+      }
     }
     super.init()
   }

+ 2 - 2
src/extensions/filter-control/bootstrap-table-filter-control.js

@@ -155,7 +155,7 @@ const UtilsFilterControl = {
 
         if (result.length > 0) {
           $(this).val(result[0].value)
-          if (result[0].hasFocus) {
+          if (result[0].hasFocus && result[0].value !== '') {
             // set callback if the field had the focus.
             fieldToFocusCallback = ((fieldToFocus, carretPosition) => {
               // Closure here to capture the field and cursor position
@@ -554,7 +554,7 @@ $.extend($.fn.bootstrapTable.defaults, {
   filterTemplate: {
     input (that, field, placeholder, value) {
       return Utils.sprintf(
-        '<input type="text" class="form-control bootstrap-table-filter-control-%s" style="width: 100%;" placeholder="%s" value="%s">',
+        '<input type="text" class="form-control bootstrap-table-filter-control-%s search-input" style="width: 100%;" placeholder="%s" value="%s">',
         field,
         'undefined' === typeof placeholder ? '' : placeholder,
         'undefined' === typeof value ? '' : value