I would like to be able to perform the following, using examples based on my code:
I couldn't get it to work so far, also tried to combine the regex with the following expression:
"[\w\s]+\"
Here is my plunker sample:
http://plnkr.co/edit/SqFOqqiRG7oFXHY89o6e?p=preview
And specifically the code part I am looking into:
var app = angular.module('filter', [])
app.controller('MainController', function($scope) {
$scope.deals = [{
lob: 'Marine, Motor, Carpenter',
cedent: 'ABC Paris Company',
treaty: 'QS - Test'
}, {
lob: 'Liability',
cedent: 'Carpenter Company',
treaty: 'W/XL Test'
}];
});
// filterBy implementation
app.filter('filterBy', function() {
return function(array, query) {
var parts = query && query.trim().split(/\s+/g) ,
keys = Object.keys(array[0]);
if (!parts || !parts.length) return array;
return array.filter(function(obj) {
return parts.every(function(part) {
return keys.some(function(key) {
return String(obj[key]).toLowerCase().indexOf(part.toLowerCase()) > -1;
});
});
});
};
});
Thank you in advance for some hints how to solve it.
You need to adjust the code a bit to collect the search keywords from the input:
app.filter('filterBy', function() {
return function(array, query) {
if (!query) return array; // Added for better error checking
var parts = query && get_query(query.trim()) , // Use the get_query function here
keys = Object.keys(array[0]);
if (!parts || !parts.length) return array;
return array.filter(function(obj) {
return parts.every(function(part) {
return keys.some(function(key) {
return String(obj[key]).toLowerCase().indexOf(part.toLowerCase()) > -1;
});
});
});
};
});
function get_query(qry) { // Function to collect search keywords
var re = /"([^"]+)"|\S+/g; // The regex gets all "..." or
var m; // non-whitespace substrings
var res = [];
while ((m = re.exec(qry)) !== null) {
res.push(m[1] ? m[1] : m[0]); // Get either the captured text inside ""
} // or a non-whitespace chunk
Here is an updated plunkr
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.