Angular Js Smart Table custom filter to search muti word filter using space

498 Views Asked by At

I am struggling with custom filter directive for past five hours. I am beginner of angularjs. I have a table with all needs except of filter. i want global multiple string filter using space in a single search text box. Here is my code :

app.filter('multiWordFilter', ['$filter', function ($filter) {

    // function that's invoked each time Angular runs $digest()
    return function (input, predicate) {
        var searchValue = predicate['$'];
        //console.log(searchValue);
        var customPredicate = function (value, index, array) {
            console.log(value);
            // alert(searchValue);

            // if filter has no value, return true for each element of the input array
            if (typeof searchValue === 'undefined') {
                return true;
            }
            var propList = Object.getOwnPropertyNames(value) || [];
            var splitValue = (searchValue || '').split(' ');
            var resultArray = [];
            for (var i = 0; i < propList.length; i++) {
                var prop = propList[i];
                if (typeof value[prop] !== 'string')
                    continue;

                for (var j = 0; j < splitValue.length; j++) {
                    var searchText = splitValue[j];
                    if (!searchText)
                        continue;

                    var index = (String)(value[prop]).toLowerCase().indexOf(searchText.toLowerCase());
                    if (index > -1) {
                        trueList.push(true);
                        break;
                    }
                }
            }
            return trueList.length
        }
        return $filter('filter')(input, customPredicate, false);
    }
}])

above code working only for single string search and also i try to get multiple string split with space for ex |Name|ContactNo| |:Asraf|8765270810| |:Ram|8765270810|

search word be like 8754270810 Asraf

only return first search result not a second.

I am expecting

search word be like 8754270810 will return two records search word be like 8754270810 Asraf will return one record

Thanks in advance guys.

1

There are 1 best solutions below

0
On BEST ANSWER

I found an solution for above my problem.

app.filter('multiWordFilter', ['$filter', function ($filter) {
    // function that's invoked each time Angular runs $digest()
    return function (input, predicate) {
        var searchValue = predicate['$'];
        var customPredicate = function (value, index, array) {

            // if filter has no value, return true for each element of the input array
            if (typeof searchValue === 'undefined') {
                return true;
            }
            var propList = Object.getOwnPropertyNames(value) || [];
            var splitValue = ((String)(searchValue).trim() || '').split(' ');

            if (!splitValue.length)
                return true;

            var searchCount = 0;
            for (var j = 0; j < splitValue.length; j++) {
                var searchText = splitValue[j];
                if (!searchText)
                    continue;
                for (var i = 0; i < propList.length; i++) {
                    var prop = propList[i];
                    if (typeof value[prop] !== 'string')
                        continue;

                    var index = (String)(value[prop]).toLowerCase().indexOf(searchText.toLowerCase());
                    if (index > -1) {
                        searchCount++;
                        break;
                    }
                }
            }
            return (searchCount == splitValue.length)
        }
        return $filter('filter')(input, customPredicate, false);
    }
}])

Here when split Search Text value count and searched count is equal then only the matched records available there.

In my case assume a grid has 10 rows, Search Text is : 'Dog eat'. Now what will actually happen there is, first filter with Dog now we have 7 rows and then now filter with eat in previous filter where already we get 7 rows, finally we get 4 rows. this is the output what actually i want.

Thank u guys who spend your precious time for my question.