Skip to content

Commit 94a0216

Browse files
djhvscfwenzhixin
authored andcommitted
Improvements in filter-control extension (wenzhixin#2460)
* Release the new extension * Update the resetSearch docs * Adding a separated css file and disabled the unused options from select control * Added new option disableUnusedSelectOptions * Instead of disable, hide the option * Use hide/show instead of display:none/block * Fix wenzhixin#2461 Example: pagination true: http://jsfiddle.net/djhvscf/hcbg3k8g/13/ pagination false: http://jsfiddle.net/djhvscf/hcbg3k8g/14/ * Fix the filter-control version number
1 parent fde4220 commit 94a0216

5 files changed

Lines changed: 56 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ ChangeLog
1616
- fix(export extension): fix #2220, selected rows does not work when data-pagination-side is server
1717
- feat(cookie extension): fix #2386, add `getCookies` method
1818
- feat(multiple-selection-row extension): add multiple-selection-row extension
19+
- feat(filter-control extension): fix #1540, disable unnecessary/unused values from select options
20+
- feat(filter-control extension): fix #2448, create a css file which contains the style for this extension
21+
22+
#### Breaking changes in 1.11.1
23+
- **Filter-Control extension**: deleted the inline-style and now this extension is using a separated css file.
24+
1925

2026
### 1.11.0
2127

src/bootstrap-table.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,6 +2510,7 @@
25102510
});
25112511

25122512
this.initSearch();
2513+
this.initPagination();
25132514
this.initSort();
25142515
this.initBody(true);
25152516
};

src/extensions/filter-control/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Dependence if you use the datepicker option: [bootstrap-datepicker](https://gith
66
## Usage
77

88
```html
9+
<link rel="stylesheet" type="text/css" href="extensions/filter-control/bootstrap-table-filter-control.css">
910
<script src="extensions/filter-control/bootstrap-table-filter-control.js"></script>
1011
```
1112

@@ -20,7 +21,7 @@ Dependence if you use the datepicker option: [bootstrap-datepicker](https://gith
2021
### filterShowClear
2122

2223
* type: Boolean
23-
* description: Set true to add a button to clear all the controls added by this plugin
24+
* description: Set true to add a button to clear all the controls added by this plugin.
2425
* default: `false`
2526

2627
### alignmentSelectControlOptions
@@ -29,6 +30,12 @@ Dependence if you use the datepicker option: [bootstrap-datepicker](https://gith
2930
* description: Set the alignemnt of the select control options. Use Use `left`, `right` or `auto`.
3031
* default: `undefined`
3132

33+
### hideUnusedSelectOptions
34+
35+
* type: Boolean
36+
* description: Set to true in order to hide the options that are not in the table. This option does not work on server-side pagination.
37+
* default: `false`
38+
3239
## Column options
3340

3441
### filterControl
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @author: Dennis Hernández
3+
* @webSite: http://djhvscf.github.io/Blog
4+
* @version: v2.1.1
5+
*/
6+
7+
.no-filter-control {
8+
height: 34px;
9+
}
10+
11+
.filter-control {
12+
margin: 0 2px 2px 2px;
13+
}

src/extensions/filter-control/bootstrap-table-filter-control.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@
1111
var sprintf = $.fn.bootstrapTable.utils.sprintf,
1212
objectKeys = $.fn.bootstrapTable.utils.objectKeys;
1313

14+
var getOptionsFromSelectControl = function (selectControl) {
15+
return selectControl.get(selectControl.length - 1).options;
16+
};
17+
18+
var hideUnusedSelectOptions = function (selectControl, uniqueValues) {
19+
var options = getOptionsFromSelectControl(selectControl);
20+
21+
for (var i = 0; i < options.length; i++) {
22+
if (options[i].value !== "") {
23+
if (!uniqueValues.hasOwnProperty(options[i].value)) {
24+
selectControl.find(sprintf("option[value='%s']", options[i].value)).hide();
25+
} else {
26+
selectControl.find(sprintf("option[value='%s']", options[i].value)).show();
27+
}
28+
}
29+
}
30+
};
31+
1432
var addOptionToSelectControl = function (selectControl, value, text) {
1533
value = $.trim(value);
1634
selectControl = $(selectControl.get(selectControl.length - 1));
@@ -39,7 +57,7 @@
3957
};
4058

4159
var existOptionInSelectControl = function (selectControl, value) {
42-
var options = selectControl.get(selectControl.length - 1).options;
60+
var options = getOptionsFromSelectControl(selectControl);
4361
for (var i = 0; i < options.length; i++) {
4462
if (options[i].value === value.toString()) {
4563
//The value is not valid to add
@@ -161,7 +179,7 @@
161179
};
162180

163181
var initFilterSelectControls = function (that) {
164-
var data = that.options.data,
182+
var data = that.data,
165183
itemsPerPage = that.pageTo < that.options.data.length ? that.options.data.length : that.pageTo,
166184

167185
isColumnSearchableViaSelect = function (column) {
@@ -198,16 +216,21 @@
198216

199217
uniqueValues[formattedValue] = fieldValue;
200218
}
219+
201220
for (var key in uniqueValues) {
202221
addOptionToSelectControl(selectControl, uniqueValues[key], key);
203222
}
204223

205224
sortSelectControl(selectControl);
225+
226+
if (that.options.hideUnusedSelectOptions) {
227+
hideUnusedSelectOptions(selectControl, uniqueValues);
228+
}
206229
}
207230
});
208231
};
209232

210-
var escapeID = function( id ) {
233+
var escapeID = function(id) {
211234
return String(id).replace( /(:|\.|\[|\]|,)/g, "\\$1" );
212235
};
213236

@@ -226,9 +249,9 @@
226249
}
227250

228251
if (!column.filterControl) {
229-
html.push('<div style="height: 34px;"></div>');
252+
html.push('<div class="no-filter-control"></div>');
230253
} else {
231-
html.push('<div style="margin: 0 2px 2px 2px;" class="filterControl">');
254+
html.push('<div class="filter-control">');
232255

233256
var nameControl = column.filterControl.toLowerCase();
234257
if (column.searchable && that.options.filterTemplate[nameControl]) {

0 commit comments

Comments
 (0)