浏览代码

Fix/6128 (#6301)

* removed duplicated code

* Added new options to allow filtering multiple values at once

* Fixed typo in documentation

* Trim filter value

* removed extra semicolon
Dustin Utecht 3 年之前
父节点
当前提交
914b1cc8f4

+ 25 - 0
site/docs/extensions/filter-control.md

@@ -80,6 +80,31 @@ toc: true
 
 - **Default:** `undefined`
 
+### filterControlMultipleSearch
+
+- **Attribute:** `data-filter-control-multiple-search`
+
+- **type:** `bool`
+
+- **Detail:**
+
+  Set to `true` to allow searching multiple values at once.   
+  The values will be splitted by a delimiter, see option `filterControlMultipleSearchDelimiter`.
+
+- **Default:** `false`
+
+### filterControlMultipleSearchDelimiter
+
+- **Attribute:** `data-filter-control-multiple-search-delimiter`
+
+- **type:** `String`
+
+- **Detail:**
+
+  Defines the delimiter which will be used to split the search values in the option `filterControlMultipleSearchDelimiter`.
+
+- **Default:** `,`
+
 ### searchOnEnterKey
 
 - **Attribute:** `data-search-on-enter-key`

+ 1 - 1
src/bootstrap-table.js

@@ -1606,7 +1606,7 @@ class BootstrapTable {
           const match = indexRegex.exec(Utils.normalizeAccent(value))
 
           if (match) {
-            searchText = value.substring(match.index, match.index + searchText.length);
+            searchText = value.substring(match.index, match.index + searchText.length)
           }
         }
 

+ 54 - 46
src/extensions/filter-control/bootstrap-table-filter-control.js

@@ -10,6 +10,8 @@ const Utils = $.fn.bootstrapTable.utils
 $.extend($.fn.bootstrapTable.defaults, {
   filterControl: false,
   filterControlVisible: true,
+  filterControlMultipleSearch: false,
+  filterControlMultipleSearchDelimiter: ',',
   // eslint-disable-next-line no-unused-vars
   onColumnSearch (field, text) {
     return false
@@ -225,57 +227,63 @@ $.BootstrapTable = class extends $.BootstrapTable {
             filterValue = Utils.normalizeAccent(filterValue)
           }
 
-          if (filterValue === '') {
-            tmpItemIsExpected = true
-          } else {
-            // Fix #142: search use formatted data
-            if (thisColumn) {
-              if (thisColumn.searchFormatter || thisColumn._forceFormatter) {
-                value = $.fn.bootstrapTable.utils.calculateObjectValue(
-                  that.header,
-                  that.header.formatters[$.inArray(key, that.header.fields)],
-                  [value, item, i],
-                  value
-                )
-              }
+          let filterValues = [filterValue]
+
+          if (
+            this.options.filterControlMultipleSearch
+          ) {
+            filterValues = filterValue.split(this.options.filterControlMultipleSearchDelimiter)
+          }
+
+          filterValues.forEach(filterValue => {
+            if (tmpItemIsExpected === true) {
+              return
             }
 
-            if ($.inArray(key, that.header.fields) !== -1) {
-              if (value === undefined || value === null) {
-                tmpItemIsExpected = false
-              } else if (typeof value === 'object' && thisColumn.filterCustomSearch) {
-                itemIsExpected.push(that.isValueExpected(rawFilterValue, value, thisColumn, key))
-                return
-              } else if (typeof value === 'object' && Array.isArray(value)) {
-                value.forEach(objectValue => {
-                  if (tmpItemIsExpected) {
-                    return
-                  }
-
-                  if (this.options.searchAccentNeutralise) {
-                    objectValue = Utils.normalizeAccent(objectValue)
-                  }
-                  tmpItemIsExpected = that.isValueExpected(filterValue, objectValue, thisColumn, key)
-                })
-              } else if (typeof value === 'object' && !Array.isArray(value)) {
-                Object.values(value).forEach(objectValue => {
-                  if (tmpItemIsExpected) {
-                    return
-                  }
-
-                  if (this.options.searchAccentNeutralise) {
-                    objectValue = Utils.normalizeAccent(objectValue)
-                  }
-                  tmpItemIsExpected = that.isValueExpected(filterValue, objectValue, thisColumn, key)
-                })
-              } else if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
-                if (this.options.searchAccentNeutralise) {
-                  value = Utils.normalizeAccent(value)
+            filterValue = filterValue.trim()
+
+            if (filterValue === '') {
+              tmpItemIsExpected = true
+            } else {
+              // Fix #142: search use formatted data
+              if (thisColumn) {
+                if (thisColumn.searchFormatter || thisColumn._forceFormatter) {
+                  value = $.fn.bootstrapTable.utils.calculateObjectValue(
+                    that.header,
+                    that.header.formatters[$.inArray(key, that.header.fields)],
+                    [value, item, i],
+                    value
+                  )
+                }
+              }
+
+              if ($.inArray(key, that.header.fields) !== -1) {
+                if (value === undefined || value === null) {
+                  tmpItemIsExpected = false
+                } else if (typeof value === 'object' && thisColumn.filterCustomSearch) {
+                  itemIsExpected.push(that.isValueExpected(rawFilterValue, value, thisColumn, key))
+                } else if (typeof value === 'object' && Array.isArray(value)) {
+                  value.forEach(objectValue => {
+                    if (tmpItemIsExpected) {
+                      return
+                    }
+
+                    tmpItemIsExpected = that.isValueExpected(filterValue, objectValue, thisColumn, key)
+                  })
+                } else if (typeof value === 'object' && !Array.isArray(value)) {
+                  Object.values(value).forEach(objectValue => {
+                    if (tmpItemIsExpected) {
+                      return
+                    }
+
+                    tmpItemIsExpected = that.isValueExpected(filterValue, objectValue, thisColumn, key)
+                  })
+                } else if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
+                  tmpItemIsExpected = that.isValueExpected(filterValue, value, thisColumn, key)
                 }
-                tmpItemIsExpected = that.isValueExpected(filterValue, value, thisColumn, key)
               }
             }
-          }
+          })
 
           itemIsExpected.push(tmpItemIsExpected)
         })