diff --git a/src/classes/searchProvider.js b/src/classes/searchProvider.js index 2b2c3217..3552b187 100644 --- a/src/classes/searchProvider.js +++ b/src/classes/searchProvider.js @@ -1,4 +1,4 @@ -window.kg.SearchProvider = function (grid) { +kg.SearchProvider = function (grid) { var self = this, searchConditions = [], lastSearchStr; @@ -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; } @@ -68,7 +87,7 @@ searchConditions.push({ column: columnName, columnDisplay: columnName.replace(/\s+/g, '').toLowerCase(), - regex: getRegExp(columnValue, 'i') + regex: getRegExp(columnValue, 'i'), }); } } else { @@ -76,7 +95,7 @@ if (val) { searchConditions.push({ column: '', - regex: getRegExp(val, 'i') + regex: getRegExp(val, 'i'), }); } } @@ -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; }); }); }