0

I have a global filter that filters data based on a text input.

<search-box ng-model="vm.filter.keyword"></search-box>

And, we use the filter like this

<tr ng-repeat="group in vm.groups | searchGlobal:vm.filter.keyword

My filter looks like:

.filter('searchGlobal',
    function searchGlobal () {
        return function searchGlobal (searchValArr,filterText) {
            var filteredData = [];
            for(var i = 0; i <searchValArr.length; i++) {
                if (searchValArr[i].name.toLowerCase().indexOf(filterText.toLowerCase()) !== -1 || 
                searchValArr[i].id.indexOf(filterText) !== -1) {
                    filteredData.push(searchValArr[i]);
                }                        
            }
            return filteredData;
        };
    });

Now, this is the problem.

The fields to look for might just not be name and id.

In some cases they might be name, phone_numbers,

in some case name,age,refNumbers.

How do I handle the scenario logic inside the filter accordingly?

2
  • Do you pass second param to view searchGlobal:vm.filter.keyword? I saw it just one param. Commented Apr 6, 2018 at 9:37
  • @TanDuong First param is always the dataset of ngRepeat and the second one is vm.filter.keyword Commented Apr 6, 2018 at 9:39

2 Answers 2

1

You can add one more param for checking the property you want

<tr ng-repeat="group in vm.groups | searchGlobal:vm.filter.keyword:vm.filter.listKeys

.filter('searchGlobal',
function searchGlobal () {
    return function searchGlobal (searchValArr,filterText, listKeys) {
        var filteredData = [];
        var listIdAdd = [];

        if (angular.isUnDefined(listKeys) || !angular.isArray(listKeys)) {
            listKeys = ['name', 'id'];
        }
        for(var i = 0; i <searchValArr.length; i++) {
            for (var j = 0; j < listKeys.length; j++) {
                if (searchValArr[i][listKeys[j]].toLowerCase().indexOf(filterText.toLowerCase()) !== -1 &&
                    listIdAdd.indexOf(searchValArr[i].id === -1)) {
                    filteredData.push(searchValArr[i]);
                    listIdAdd.push(searchValArr[i].id);
                }
            }
        }
        return filteredData;
    };
});

Hope this help

Sign up to request clarification or add additional context in comments.

Comments

0

You can create a filter that will check if any of the object keys contains the keyword.

I have created a simple example here.

The filter function looks like the following, note that it uses some modern JS features like Object.values and String.includes which can be exchanged if older browser support is required.

angular.module("myApp")
  .filter("searchGlobal", function(){
    return function(data, keyword){
      // Return a filtered array of items in the initial array
      return data.filter(function(obj){
        // Check if any (some) of the object keys contains the keyword
        return Object.values(obj).some(function(value){
          return value.includes(keyword);
        });
      });
    };
  });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.