Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Search Provider #176

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions src/classes/searchProvider.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
window.kg.SearchProvider = function (grid) {
kg.SearchProvider = function (grid) {
var self = this,
searchConditions = [],
lastSearchStr;
Expand All @@ -8,53 +8,72 @@
self.throttle = grid.config.filterOptions.filterThrottle;
self.fieldMap = {};
self.evalFilter = function () {
if (searchConditions.length === 0) {
grid.filteredData(grid.sortedData.peek().filter(function(item) {
if (searchConditions.length === 0)
grid.filteredData(grid.sortedData.peek().filter(function (item) {
return !item._destroy;
}));
} else {
grid.filteredData(grid.sortedData.peek().filter(function(item) {
else {
grid.filteredData(grid.sortedData.peek().filter(function (item) {
if (item._destroy) {
return false;
}

for (var i = 0, len = searchConditions.length; i < len; i++) {
var condition = searchConditions[i];
//Search entire row
if (!condition.column) {
for (var prop in item) {
if (item.hasOwnProperty(prop)) {
var c = self.fieldMap[prop];
if (!c) continue;
var pVal = ko.utils.unwrapObservable(item[prop]);
if (pVal && condition.regex.test(pVal.toString())) {
if (pVal && (typeof c.cellFilter === 'function' ? condition.regex.test(ko.utils.unwrapObservable(c.cellFilter(pVal)).toString()) : condition.regex.test(typeof pVal === 'object' ? evalObject(pVal, c.field).toString() : pVal.toString())))
return true;
}
}
}
return false;
}
//Search by column.
var field = ko.utils.unwrapObservable(item[condition.column]) || ko.utils.unwrapObservable(item[self.fieldMap[condition.columnDisplay]]);
if (!field || !condition.regex.test(field.toString())) {
var col = self.fieldMap[condition.column] || self.fieldMap[condition.columnDisplay];
if (!col) return false;
var value = ko.utils.unwrapObservable(item[condition.column]) || ko.utils.unwrapObservable(item[col.field]);
if (!value || !(typeof col.cellFilter === 'function' ? condition.regex.test(ko.utils.unwrapObservable(col.cellFilter(value)).toString()) : condition.regex.test(typeof value === 'object' ? evalObject(value, col.field).toString() : value.toString())))
return false;
}
}
return true;
}));
}
grid.rowFactory.filteredDataChanged();
};
var getRegExp = function(str, modifiers) {

//Traversing through the object to find the value that we want. If fail, then return the original object.
var evalObject = function (obj, columnName) {
if (typeof obj != 'object' || typeof columnName != 'string')
return obj;
var args = columnName.split('.');
var cObj = obj;
if (args.length > 1) {
for (var i = 1, len = args.length; i < len; i++) {
cObj = cObj[args[i]];
if (!cObj)
return obj;
}
return cObj;
}
return obj;
};
var getRegExp = function (str, modifiers) {
try {
return new RegExp(str, modifiers);
} catch(err) {
}
catch (err) {
//Escape all RegExp metacharacters.
return new RegExp(str.replace(/(\^|\$|\(|\)|\<|\>|\[|\]|\{|\}|\\|\||\.|\*|\+|\?)/g, '\\$1'));
}
};
}
var buildSearchConditions = function (a) {
//reset.
searchConditions = [];
var qStr;
var qStr = '';
if (!(qStr = $.trim(a))) {
return;
}
Expand All @@ -68,15 +87,15 @@
searchConditions.push({
column: columnName,
columnDisplay: columnName.replace(/\s+/g, '').toLowerCase(),
regex: getRegExp(columnValue, 'i')
regex: getRegExp(columnValue, 'i'),
});
}
} else {
var val = $.trim(args[0]);
if (val) {
searchConditions.push({
column: '',
regex: getRegExp(val, 'i')
regex: getRegExp(val, 'i'),
});
}
}
Expand All @@ -98,7 +117,9 @@
if (!self.extFilter) {
grid.columns.subscribe(function (a) {
$.each(a, function (i, col) {
self.fieldMap[col.displayName().toLowerCase().replace(/\s+/g, '')] = col.field;
//Just in the case that value being set is an object within an object e.g ParentObject.childObject.
self.fieldMap[col.field.split('.')[0]] = col;
self.fieldMap[col.displayName().toLowerCase().replace(/\s+/g, '')] = col;
});
});
}
Expand Down