Skip to content
This repository was archived by the owner on Jan 15, 2022. It is now read-only.

Commit 3a2fad1

Browse files
committed
Allow to filter by column: value
This Branch improves the way filterBy workes, by allowing it to be set with and hash like {'Column': 'search query'} programatically. Another improvement, that still needs to be touched is the ability to do the same from the search box in a form of 'Column: search query' that will be split by the ':' and use each side as column to filter, and value.
1 parent 0acc184 commit 3a2fad1

File tree

8 files changed

+464
-2389
lines changed

8 files changed

+464
-2389
lines changed

Gruntfile.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,3 @@ module.exports = function(grunt) {
131131
grunt.registerTask('build', ['babel:common', 'buildBrowser']);
132132
grunt.registerTask('default', ['build', 'watch:build']);
133133
};
134-

build/reactable.js

Lines changed: 93 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,28 @@ window.ReactDOM["default"] = window.ReactDOM;
239239
}, {
240240
key: 'render',
241241
value: function render() {
242+
var value = '';
243+
if (typeof this.props.value != 'string') {
244+
var should_slice = false;
245+
for (var key in this.props.value) {
246+
if (key && this.props.value[key]) {
247+
should_slice = true;
248+
value += key + ': ' + this.props.value[key] + ', ';
249+
} else {
250+
value += key + ': ';
251+
}
252+
}
253+
if (should_slice) {
254+
value = value.slice(0, -2);
255+
}
256+
} else {
257+
value = this.props.value;
258+
}
259+
value = value.trim();
242260
return _react['default'].createElement('input', { type: 'text',
243261
className: 'reactable-filter-input',
244262
placeholder: this.props.placeholder,
245-
value: this.props.value,
263+
value: value,
246264
onKeyUp: this.onChange.bind(this),
247265
onChange: this.onChange.bind(this) });
248266
}
@@ -1204,24 +1222,85 @@ window.ReactDOM["default"] = window.ReactDOM;
12041222
}, {
12051223
key: 'applyFilter',
12061224
value: function applyFilter(filter, children) {
1207-
// Helper function to apply filter text to a list of table rows
1208-
filter = filter.toLowerCase();
1209-
var matchedChildren = [];
1225+
if (typeof filter === 'string') {
1226+
// Helper function to apply filter text to a list of table rows
1227+
filter = filter.toLowerCase();
1228+
var matchedChildren = [];
12101229

1211-
for (var i = 0; i < children.length; i++) {
1212-
var data = children[i].props.data;
1230+
for (var i = 0; i < children.length; i++) {
1231+
var data = children[i].props.data;
12131232

1214-
for (var j = 0; j < this.props.filterable.length; j++) {
1215-
var filterColumn = this.props.filterable[j];
1233+
for (var j = 0; j < this.props.filterable.length; j++) {
1234+
var filterColumn = this.props.filterable[j];
12161235

1217-
if (typeof data[filterColumn] !== 'undefined' && (0, _libExtract_data_from.extractDataFrom)(data, filterColumn).toString().toLowerCase().indexOf(filter) > -1) {
1218-
matchedChildren.push(children[i]);
1219-
break;
1236+
if (typeof data[filterColumn] !== 'undefined' && (0, _libExtract_data_from.extractDataFrom)(data, filterColumn).toString().toLowerCase().indexOf(filter) > -1) {
1237+
matchedChildren.push(children[i]);
1238+
break;
1239+
}
12201240
}
12211241
}
1242+
1243+
return matchedChildren;
1244+
} else {
1245+
var _ret = (function () {
1246+
1247+
var filterCount = Object.keys(filter).length;
1248+
var matchedChildren = [];
1249+
1250+
for (var filterColumn in filter) {
1251+
var val = filter[filterColumn].toLowerCase();
1252+
for (var i = 0; i < children.length; i++) {
1253+
var data = children[i].props.data;
1254+
if (typeof data[filterColumn] !== 'undefined' && (0, _libExtract_data_from.extractDataFrom)(data, filterColumn).toString().toLowerCase().indexOf(val) > -1) {
1255+
matchedChildren.push(children[i]);
1256+
}
1257+
}
1258+
}
1259+
1260+
if (filterCount > 1) {
1261+
var result = [];
1262+
return {
1263+
v: matchedChildren.map(function (children) {
1264+
var occurrences = matchedChildren.filter(function (value) {
1265+
return value.key === children.key;
1266+
}).length;
1267+
if (occurrences == filterCount) {
1268+
return children;
1269+
}
1270+
})
1271+
};
1272+
} else {
1273+
return {
1274+
v: matchedChildren
1275+
};
1276+
}
1277+
})();
1278+
1279+
if (typeof _ret === 'object') return _ret.v;
12221280
}
1281+
}
1282+
}, {
1283+
key: 'onFilter',
1284+
value: function onFilter(filters) {
1285+
if (typeof filters === 'string' && filters.indexOf(':') != -1) {
1286+
var filterObj = {};
1287+
filters = filters.trim();
1288+
var col = '';
1289+
var val = '';
1290+
1291+
var filter = filters.split(':');
1292+
if (filter[0]) {
1293+
col = filter[0].trim();
1294+
}
1295+
if (filter[1]) {
1296+
val = filter[1].trim();
1297+
}
1298+
filterObj[col] = val;
12231299

1224-
return matchedChildren;
1300+
filters = filterObj;
1301+
}
1302+
1303+
this.setState({ filter: filters });
12251304
}
12261305
}, {
12271306
key: 'sortByCurrentSort',
@@ -1284,7 +1363,7 @@ window.ReactDOM["default"] = window.ReactDOM;
12841363
this.setState({ currentSort: currentSort });
12851364
this.sortByCurrentSort();
12861365

1287-
if (this.props.onSort) {
1366+
if (typeof this.props.onSort === 'function') {
12881367
this.props.onSort(currentSort);
12891368
}
12901369
}
@@ -1410,9 +1489,7 @@ window.ReactDOM["default"] = window.ReactDOM;
14101489
props,
14111490
columns && columns.length > 0 ? _react['default'].createElement(_thead.Thead, { columns: columns,
14121491
filtering: filtering,
1413-
onFilter: function (filter) {
1414-
_this.setState({ filter: filter });
1415-
},
1492+
onFilter: this.onFilter.bind(this),
14161493
filterPlaceholder: this.props.filterPlaceholder,
14171494
currentFilter: this.state.filter,
14181495
sort: this.state.currentSort,

0 commit comments

Comments
 (0)