ソースを参照

aded the number comparsions (<, >, <=, >=) as in the default search,
also added a new option set a custom search function

NAME 5 年 前
コミット
5f048e2c1c

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

@@ -209,6 +209,23 @@ Dependence if you use the datepicker option: [bootstrap-datepicker](https://gith
 
 - **Default:** `'asc'`
 
+### filterCustomSearch
+
+- **Attribute:** `data-filter-custom-search`
+
+- **type:** `function`
+
+- **Detail:**
+
+   The custom search function is executed instead of built-in search function, takes four parameters:
+
+     * `text`: the search text.
+     * `value`: the the value of the column to compare.
+     * `field`: the column field name.
+     * `data`: the table data.
+
+- **Default:** `undefined`
+
 ### Icons
 
 * clear: 'glyphicon-trash icon-clear'

+ 47 - 5
src/extensions/filter-control/bootstrap-table-filter-control.js

@@ -707,9 +707,10 @@ $.BootstrapTable = class extends $.BootstrapTable {
           const thisColumn = that.columns[that.fieldsColumnsIndex[key]]
           const fval = (fp[key] || '').toLowerCase()
           let value = Utils.getItemField(item, key, false)
+          let tmpItemIsExpected
 
           if (fval === '') {
-            itemIsExpected.push(true)
+            tmpItemIsExpected = true
           } else {
             // Fix #142: search use formatted data
             if (thisColumn && thisColumn.searchFormatter) {
@@ -723,18 +724,59 @@ $.BootstrapTable = class extends $.BootstrapTable {
 
             if ($.inArray(key, that.header.fields) !== -1) {
               if (value === undefined || value === null) {
-                itemIsExpected.push(false)
+                tmpItemIsExpected = false
               } else if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
                 if (thisColumn.filterStrictSearch) {
-                  itemIsExpected.push(value.toString().toLowerCase() === fval.toString().toLowerCase())
+                  tmpItemIsExpected = value.toString().toLowerCase() === fval.toString().toLowerCase()
                 } else if (thisColumn.filterStartsWithSearch) {
-                  itemIsExpected.push((`${value}`).toLowerCase().indexOf(fval) === 0)
+                  tmpItemIsExpected = (`${value}`).toLowerCase().indexOf(fval) === 0
                 } else {
-                  itemIsExpected.push((`${value}`).toLowerCase().includes(fval))
+                  tmpItemIsExpected = (`${value}`).toLowerCase().includes(fval)
+                }
+
+                const largerSmallerEqualsRegex = /(?:(<=|=>|=<|>=|>|<)(?:\s+)?(\d+)?|(\d+)?(\s+)?(<=|=>|=<|>=|>|<))/gm
+                const matches = largerSmallerEqualsRegex.exec(fval)
+
+                if (matches) {
+                  const operator = matches[1] || `${matches[5]}l`
+                  const comparisonValue = matches[2] || matches[3]
+                  const int = parseInt(value, 10)
+                  const comparisonInt = parseInt(comparisonValue, 10)
+
+                  switch (operator) {
+                    case '>':
+                    case '<l':
+                      tmpItemIsExpected = int > comparisonInt
+                      break
+                    case '<':
+                    case '>l':
+                      tmpItemIsExpected = int < comparisonInt
+                      break
+                    case '<=':
+                    case '=<':
+                    case '>=l':
+                    case '=>l':
+                      tmpItemIsExpected = int <= comparisonInt
+                      break
+                    case '>=':
+                    case '=>':
+                    case '<=l':
+                    case '=<l':
+                      tmpItemIsExpected = int >= comparisonInt
+                      break
+                    default:
+                      break
+                  }
+                }
+
+                if (thisColumn.filterCustomSearch) {
+                  tmpItemIsExpected = Utils.calculateObjectValue(that, thisColumn.filterCustomSearch, [fval, value, key, that.options.data], true)
                 }
               }
             }
           }
+
+          itemIsExpected.push(tmpItemIsExpected)
         })
 
         return !itemIsExpected.includes(false)