javascript - angular custom filtering in ngTables - Stack Overflow

I'm trying to do some custom filtering in ngTables (simmilar to this example), but with text input

I'm trying to do some custom filtering in ngTables (simmilar to this example), but with text input field. I have set of columns with standard text input filters and for some of them I want to use my own filtering function, not default angular $filter('filter')(array, params.filter()), but something like $filter('myOwnFilter')(array, params.filter())

Filtering happens in my controller:

var orderedData = params.filter() ? $filter('filter')(array, params.filter()) : array;

What I have:

<td class="text-left" data-title="'Name'" filter="{ 'Column': 'myOwnFilter' }" data-sortable="'Column'">
  {{ array.Column }}
</td>

and the template:

<script type="text/ng-template" id="ng-table/filters/myOwnFilter.html">
  <input type="text" name="myOwnFilter" data-ng-model="params.filter()[name]" data-ng-if="filter == 'myOwnFilter'" class="input-filter form-control"/>
</script>

I'm trying to do some custom filtering in ngTables (simmilar to this example), but with text input field. I have set of columns with standard text input filters and for some of them I want to use my own filtering function, not default angular $filter('filter')(array, params.filter()), but something like $filter('myOwnFilter')(array, params.filter())

Filtering happens in my controller:

var orderedData = params.filter() ? $filter('filter')(array, params.filter()) : array;

What I have:

<td class="text-left" data-title="'Name'" filter="{ 'Column': 'myOwnFilter' }" data-sortable="'Column'">
  {{ array.Column }}
</td>

and the template:

<script type="text/ng-template" id="ng-table/filters/myOwnFilter.html">
  <input type="text" name="myOwnFilter" data-ng-model="params.filter()[name]" data-ng-if="filter == 'myOwnFilter'" class="input-filter form-control"/>
</script>
Share Improve this question asked Jun 17, 2014 at 12:43 michas84michas84 1773 silver badges11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

As answered in this question https://stackoverflow./a/27296677/61577 you must use a custom "parator" function as a third argument in your controller. A simple example

var myCustomComparator = function(value, searchTerm) {
    return (value || '').indexOf(searchTerm) > -1;
}

then in your getData callback

var orderedData = params.filter() ? $filter('filter')(array, params.filter(), myCustomComparator) : array;

Just note that you must inspect the params.filter() in case you need different parators for different columns/fields.

The angular documentation for filter parator

Let's say you want to add a filter that shows only the rows that have the property avg greater-than your filter:

View

<td data-title="'Average'" sortable="'avg'" filter="{ 'min': 'number' }">

Controller

vm.filtered = params.filter() ? filterData(params, vm.filtered) : vm.filtered;

And the custom filter

function filterData(params, data) {
              var min;
              if (params.filter() && params.filter().min) {
                  //remove default params.filter().min filter
                  min = params.filter().min;
                  delete params.filter().min;
                  //filter data by min
                  data = _.filter(data, function (d) { return min <= d.avg; });
              }
              //use the normal filter
              data = params.filter() ? $filter('filter')(data, params.filter()) : data;
              if (min != undefined)//add it back
                  params.filter().min = min;
              return data;
          }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745439537a4627765.html

相关推荐

  • javascript - angular custom filtering in ngTables - Stack Overflow

    I'm trying to do some custom filtering in ngTables (simmilar to this example), but with text input

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信