8000 Added multiple-search extension · coderabsolute/bootstrap-table@275a609 · GitHub
[go: up one dir, main page]

Skip to content

Commit 275a609

Browse files
committed
Added multiple-search extension
Fix wenzhixin#1265
1 parent a512b86 commit 275a609

File tree

3 files changed

+126
-1
lines changed

3 files changed

+126
-1
lines changed

src/extensions/key-events/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Use Plugin: [bootstrap-table-key-events](https://github.com/wenzhixin/bootstrap-
< 10000 /div>
55
## Usage
66

77
```html
8-
<script src="extensions/cookie/bootstrap-table-key-events.js"></script>
8+
<script src="extensions/key-events/bootstrap-table-key-events.js"></script>
99
```
1010

1111
## Options
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Table Multiple Search
2+
3+
Use Plugin: [bootstrap-table-multiple-search](https://github.com/wenzhixin/bootstrap-table/tree/master/src/extensions/multiple-search)
4+
5+
## Usage
6+
7+
```html
8+
<script src="extensions/multiple-search/bootstrap-table-multiple-search.js"></script>
9+
```
10+
11+
## Options
12+
13+
### multipleSearch
14+
15+
* type: Boolean
16+
* 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.
17+
* default: `false`
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @author: Dennis Hernández
3+
* @webSite: http://djhvscf.github.io/Blog
4+
* @version: v1.0.0
5+
*/
6+
7+
!function ($) {
8+
9+
'use strict';
10+
11+
var getFieldIndex = function (columns, field) {
12+
var index = -1;
13+
14+
$.each(columns, function (i, column) {
15+
if (column.field === field) {
16+
index = i;
17+
return false;
18+
}
19+
return true;
20+
});
21+
return index;
22+
};
23+
24+
var calculateObjectValue = function (self, name, args, defaultValue) {
25+
var func = name;
26+
27+
if (typeof name === 'string') {
28+
// support obj.func1.func2
29+
var names = name.split('.');
30+
31+
if (names.length > 1) {
32+
func = window;
33+
$.each(names, function (i, f) {
34+
func = func[f];
35+
});
36+
} else {
37+
func = window[name];
38+
}
39+
}
40+
if (typeof func === 'object') {
41+
return func;
42+
}
43+
if (typeof func === 'function') {
44+
return func.apply(self, args);
45+
}
46+
if (!func && typeof name === 'string' && sprintf.apply(this, [name].concat(args))) {
47+
return sprintf.apply(this, [name].concat(args));
48+
}
49+
return defaultValue;
50+
};
51+
52+
$.extend($.fn.bootstrapTable.defaults, {
53+
multipleSearch: false
54+
});
55+
56+
var BootstrapTable = $.fn.bootstrapTable.Constructor,
57+
_initSearch = BootstrapTable.prototype.initSearch;
58+
59+
BootstrapTable.prototype.initSearch = function () {
60+
if (this.options.multipleSearch) {
61+
var strArray = this.searchText.split(" "),
62+
that = this,
63+
f = $.isEmptyObject(this.filterColumns) ? null : this.filterColumns,
64+
dataFiltered = [];
65+
66+
if (strArray.length === 1) {
67+
_initSearch.apply(this, Array.prototype.slice.apply(arguments));
68+
} else {
69+
for (var i = 0; i < strArray.length; i++) {
70+
var str = strArray[i].trim();
71+
dataFiltered = str ? $.grep(dataFiltered.length === 0 ? this.options.data : dataFiltered, function (item, i) {
72+
for (var key in item) {
73+
key = $.isNumeric(key) ? parseInt(key, 10) : key;
74+
var value = item[key],
75+
column = that.columns[getFieldIndex(that.columns, key)],
76+
j = $.inArray(key, that.header.fields);
77+
78+
// Fix #142: search use formated data
79+
if (column && column.searchFormatter) {
80+
value = calculateObjectValue(column,
81+
that.header.formatters[j], [value, item, i], value);
82+
}
83+
84+
var index = $.inArray(key, that.header.fields);
85+
if (index !== -1 && that.header.searchables[index] && (typeof value === 'string' || typeof value === 'number')) {
86+
if (that.options.strictSearch) {
87+
if ((value + '').toLowerCase() === str) {
88+
return true;
89+
}
90+
} else {
91+
if ((value + '').toLowerCase().indexOf(str) !== -1) {
92+
return true;
93+
}
94+
}
95+
}
96+
}
97+
return false;
98+
}) : this.data;
99+
}
100+
101+
this.data = dataFiltered;
102+
}
103+
} else {
104+
_initSearch.apply(this, Array.prototype.slice.apply(arguments));
105+
}
106+
};
107+
108+
}(jQuery);

0 commit comments

Comments
 (0)
0