From fbcb8c70159a22b0828d9047edeac277190e3b33 Mon Sep 17 00:00:00 2001 From: Trinh Ho Date: Thu, 13 Dec 2012 18:33:32 -0800 Subject: [PATCH 1/4] Add search to support cellFilter output --- src/classes/searchProvider.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/classes/searchProvider.js b/src/classes/searchProvider.js index 9b6d0555..55255818 100644 --- a/src/classes/searchProvider.js +++ b/src/classes/searchProvider.js @@ -22,16 +22,20 @@ 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 && (condition.regex.test(pVal.toString()) || (typeof c.cellFilter === 'function' && condition.regex.test(c.cellFilter(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.columnDisplay]; + if (!col) return false; + var value = ko.utils.unwrapObservable(item[condition.column]) || ko.utils.unwrapObservable(item[col.value]); + if (!value || !condition.regex.test(value.toString()) && !(typeof col.cellFilter === 'function' && condition.regex.test(col.cellFilter(value.toString())))) return false; } return true; @@ -95,7 +99,8 @@ if (!self.extFilter) { grid.columns.subscribe(function (a) { $.each(a, function (i, col) { - self.fieldMap[col.displayName().toLowerCase().replace(/\s+/g, '')] = col.field; + self.fieldMap[col.field] = col; + self.fieldMap[col.displayName().toLowerCase().replace(/\s+/g, '')] = col; }); }); } From 154b89593551c8331629e51792351b952dfd5a83 Mon Sep 17 00:00:00 2001 From: Trinh Ho Date: Wed, 19 Dec 2012 13:44:24 -0800 Subject: [PATCH 2/4] Fixing few bugs in Search Provider. Remove underlining data filter and only filter visible columns and visible data (through cellformatter). Add support for edge cases such as when the column value is an object type. --- src/classes/searchProvider.js | 67 ++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/src/classes/searchProvider.js b/src/classes/searchProvider.js index c3a48378..36f0bab2 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,12 +8,10 @@ self.throttle = grid.config.filterOptions.filterThrottle; self.fieldMap = {}; self.evalFilter = function () { - if (searchConditions.length === 0) { - grid.filteredData(grid.sortedData.peek().filter(function(item) { - return !item._destroy; - })); - } else { - grid.filteredData(grid.sortedData.peek().filter(function(item) { + if (searchConditions.length === 0) + grid.filteredData(grid.sortedData.peek()); + else { + grid.filteredData(grid.sortedData.peek().filter(function (item) { if (item._destroy) { return false; } @@ -27,47 +25,55 @@ var c = self.fieldMap[prop]; if (!c) continue; var pVal = ko.utils.unwrapObservable(item[prop]); -<<<<<<< HEAD - if (pVal && (condition.regex.test(pVal.toString()) || (typeof c.cellFilter === 'function' && condition.regex.test(c.cellFilter(pVal.toString))))) -======= - if (pVal && condition.regex.test(pVal.toString())) { ->>>>>>> 5fcb4c54bc80bf9082001c69f406d5fd1c3132f6 + if (pVal && (typeof c.cellFilter === 'function' ? condition.regex.test(ko.utils.unwrapObservable(c.cellFilter(typeof pVal === 'number' ? pVal : pVal.toString()))) : condition.regex.test(typeof pVal === 'object' ? evalObject(pVal, c.field).toString() : pVal.toString()))) return true; - } } } return false; } //Search by column. -<<<<<<< HEAD - var col = self.fieldMap[condition.columnDisplay]; + 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.value]); - if (!value || !condition.regex.test(value.toString()) && !(typeof col.cellFilter === 'function' && condition.regex.test(col.cellFilter(value.toString())))) -======= - var field = ko.utils.unwrapObservable(item[condition.column]) || ko.utils.unwrapObservable(item[self.fieldMap[condition.columnDisplay]]); - if (!field || !condition.regex.test(field.toString())) { ->>>>>>> 5fcb4c54bc80bf9082001c69f406d5fd1c3132f6 + 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(typeof value === 'number' ? value : 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; } @@ -81,7 +87,8 @@ searchConditions.push({ column: columnName, columnDisplay: columnName.replace(/\s+/g, '').toLowerCase(), - regex: getRegExp(columnValue, 'i') + regex: getRegExp(columnValue, 'i'), + originalStr: columnValue }); } } else { @@ -89,7 +96,8 @@ if (val) { searchConditions.push({ column: '', - regex: getRegExp(val, 'i') + regex: getRegExp(val, 'i'), + originalStr: val }); } } @@ -111,7 +119,8 @@ if (!self.extFilter) { grid.columns.subscribe(function (a) { $.each(a, function (i, col) { - self.fieldMap[col.field] = col; + //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; }); }); From 1c9bb10c8f30a5e926541f737be4bf0fc6102093 Mon Sep 17 00:00:00 2001 From: Trinh Ho Date: Wed, 19 Dec 2012 13:48:31 -0800 Subject: [PATCH 3/4] Removed Uneeded code. --- src/classes/searchProvider.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/classes/searchProvider.js b/src/classes/searchProvider.js index 36f0bab2..150c4c54 100644 --- a/src/classes/searchProvider.js +++ b/src/classes/searchProvider.js @@ -88,7 +88,6 @@ column: columnName, columnDisplay: columnName.replace(/\s+/g, '').toLowerCase(), regex: getRegExp(columnValue, 'i'), - originalStr: columnValue }); } } else { @@ -97,7 +96,6 @@ searchConditions.push({ column: '', regex: getRegExp(val, 'i'), - originalStr: val }); } } From 3e30a1c4882047c55ad0623d88aea011f0dec38a Mon Sep 17 00:00:00 2001 From: Trinh Ho Date: Wed, 19 Dec 2012 14:34:38 -0800 Subject: [PATCH 4/4] Add back in the check for destoryed items. Pass value directly into cellFilter and let user handle it, then unwrap if necessary. --- src/classes/searchProvider.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/classes/searchProvider.js b/src/classes/searchProvider.js index 150c4c54..3552b187 100644 --- a/src/classes/searchProvider.js +++ b/src/classes/searchProvider.js @@ -9,13 +9,14 @@ self.fieldMap = {}; self.evalFilter = function () { if (searchConditions.length === 0) - grid.filteredData(grid.sortedData.peek()); + grid.filteredData(grid.sortedData.peek().filter(function (item) { + return !item._destroy; + })); 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 @@ -25,7 +26,7 @@ var c = self.fieldMap[prop]; if (!c) continue; var pVal = ko.utils.unwrapObservable(item[prop]); - if (pVal && (typeof c.cellFilter === 'function' ? condition.regex.test(ko.utils.unwrapObservable(c.cellFilter(typeof pVal === 'number' ? pVal : pVal.toString()))) : condition.regex.test(typeof pVal === 'object' ? evalObject(pVal, c.field).toString() : 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; } } @@ -35,7 +36,7 @@ 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(typeof value === 'number' ? value : value.toString()))) : condition.regex.test(typeof value === 'object' ? evalObject(value, col.field).toString() : value.toString()))) + 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; @@ -59,7 +60,6 @@ return cObj; } return obj; - }; var getRegExp = function (str, modifiers) { try {