Browse Source

Adding validation to cookie extension

djhvscf 5 years ago
parent
commit
195924346a
1 changed files with 34 additions and 32 deletions
  1. 34 32
      src/extensions/cookie/bootstrap-table-cookie.js

+ 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()
   }