Browse Source

Added multiple-search extension

Fix #1265
Dennis Hernández 10 years ago
parent
commit
275a6095aa

+ 1 - 1
src/extensions/key-events/README.md

@@ -5,7 +5,7 @@ Use Plugin: [bootstrap-table-key-events](https://github.com/wenzhixin/bootstrap-
 ## Usage
 
 ```html
-<script src="extensions/cookie/bootstrap-table-key-events.js"></script>
+<script src="extensions/key-events/bootstrap-table-key-events.js"></script>
 ```
 
 ## Options

+ 17 - 0
src/extensions/multiple-search/README.md

@@ -0,0 +1,17 @@
+# Table Multiple Search
+
+Use Plugin: [bootstrap-table-multiple-search](https://github.com/wenzhixin/bootstrap-table/tree/master/src/extensions/multiple-search)
+
+## Usage
+
+```html
+<script src="extensions/multiple-search/bootstrap-table-multiple-search.js"></script>
+```
+
+## Options
+
+### multipleSearch
+
+* type: Boolean
+* description: Set to true if you want to search by multiple columns. For example: if the user puts: "526 table" we are going to `split` that string and then we are going to search in all columns in the boostrap table.
+* default: `false`

+ 108 - 0
src/extensions/multiple-search/bootstrap-table-multiple-search.js

@@ -0,0 +1,108 @@
+/**
+ * @author: Dennis Hernández
+ * @webSite: http://djhvscf.github.io/Blog
+ * @version: v1.0.0
+ */
+
+!function ($) {
+
+    'use strict';
+
+    var getFieldIndex = function (columns, field) {
+        var index = -1;
+
+        $.each(columns, function (i, column) {
+            if (column.field === field) {
+                index = i;
+                return false;
+            }
+            return true;
+        });
+        return index;
+    };
+
+    var calculateObjectValue = function (self, name, args, defaultValue) {
+        var func = name;
+
+        if (typeof name === 'string') {
+            // support obj.func1.func2
+            var names = name.split('.');
+
+            if (names.length > 1) {
+                func = window;
+                $.each(names, function (i, f) {
+                    func = func[f];
+                });
+            } else {
+                func = window[name];
+            }
+        }
+        if (typeof func === 'object') {
+            return func;
+        }
+        if (typeof func === 'function') {
+            return func.apply(self, args);
+        }
+        if (!func && typeof name === 'string' && sprintf.apply(this, [name].concat(args))) {
+            return sprintf.apply(this, [name].concat(args));
+        }
+        return defaultValue;
+    };
+
+    $.extend($.fn.bootstrapTable.defaults, {
+        multipleSearch: false
+    });
+
+    var BootstrapTable = $.fn.bootstrapTable.Constructor,
+        _initSearch = BootstrapTable.prototype.initSearch;
+
+    BootstrapTable.prototype.initSearch = function () {
+        if (this.options.multipleSearch) {
+            var strArray = this.searchText.split(" "),
+                that = this,
+                f = $.isEmptyObject(this.filterColumns) ? null : this.filterColumns,
+                dataFiltered = [];
+
+            if (strArray.length === 1) {
+                _initSearch.apply(this, Array.prototype.slice.apply(arguments));
+            } else {
+                for (var i = 0; i < strArray.length; i++) {
+                    var str = strArray[i].trim();
+                    dataFiltered = str ? $.grep(dataFiltered.length === 0 ? this.options.data : dataFiltered, function (item, i) {
+                        for (var key in item) {
+                            key = $.isNumeric(key) ? parseInt(key, 10) : key;
+                            var value = item[key],
+                                column = that.columns[getFieldIndex(that.columns, key)],
+                                j = $.inArray(key, that.header.fields);
+
+                            // Fix #142: search use formated data
+                            if (column && column.searchFormatter) {
+                                value = calculateObjectValue(column,
+                                    that.header.formatters[j], [value, item, i], value);
+                            }
+
+                            var index = $.inArray(key, that.header.fields);
+                            if (index !== -1 && that.header.searchables[index] && (typeof value === 'string' || typeof value === 'number')) {
+                                if (that.options.strictSearch) {
+                                    if ((value + '').toLowerCase() === str) {
+                                        return true;
+                                    }
+                                } else {
+                                    if ((value + '').toLowerCase().indexOf(str) !== -1) {
+                                        return true;
+                                    }
+                                }
+                            }
+                        }
+                        return false;
+                    }) : this.data;
+                }
+
+                this.data = dataFiltered;
+            }
+        } else {
+            _initSearch.apply(this, Array.prototype.slice.apply(arguments));
+        }
+    };
+
+}(jQuery);