esvit / ng-table

Simple table with sorting and filtering on AngularJS
http://esvit.github.io/ng-table
BSD 3-Clause "New" or "Revised" License
2.77k stars 851 forks source link

Filtering with the "select-multiple" without a custom filter algorithm #875

Open matthieu-hm opened 8 years ago

matthieu-hm commented 8 years ago

Hello,

Is it possible to filtering with the "select-multiple" template without a custom filter algorithm ? Otherwise can I set a custom filter for only one column ?

Here is a CodePen of what I tried http://codepen.io/sound-matt/pen/zBogoz

anuja-joshi commented 7 years ago

@sound-matt I did not find any way to do this without custom filters. Also, I don't think you can set custom filter only for one column as filterFilterName/filterFn in filterOptions overrides default filter logic. I come up with this solution, we can add logic to filter function depending on the field passed for filtering:

 function multiCountryFilter(){
    return function(data, countryCriteria){
      var key = Object.keys(countryCriteria)[0];
      switch(key){
        case 'country':
          return (data || []).filter(function(item){
            return countryCriteria.country.indexOf(item.country) !== -1;
          });
          break;
       case  'name':
           // some logic ...
        default:
           // some logic with default $filter
          break;
      }
    };

and in your HTML, along with country row you will have few more rows like: <td> filter="{name: 'text'}" filter-data="demo.names"> {{row.name}} </td>

But, it would be really great if ng-table provides something to implement such functionality by easier way.

ashes999 commented 7 years ago

Thanks, this was very useful.

The default text filter matches substrings. Here's a filter that matches records where the field has all the specified words (not necessarily in order), case-insensitively.

function matchAllWordsFilter()
{
  return function(data, search)
  {
      var fieldSearched = Object.keys(search)[0]; 
      var words = search[fieldSearched].split(' ');
      return (data || []).filter(function(item)
      {    
        for (var i = 0; i < words.length; i++)
        {
          // Data doesn't have the specified field, or the specified word
          if (typeof(item[fieldSearched]) == "undefined" || item[fieldSearched].toLowerCase().indexOf(words[i].toLowerCase()) == -1)
          {
            // Word missing, don't match
            return false;
          }        
        }
        // All words present, match.
        return true;
      });
    //}
  };
}
hansen168 commented 6 years ago

@anuja-joshi, your solution is great. But it did not work with multi column filter. How to make it return multi column filter? Thanks