4

ng table does not filter string empty in strict comparison

 2 years ago
source link: https://www.codesd.com/item/ng-table-does-not-filter-string-empty-in-strict-comparison.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

ng table does not filter string empty in strict comparison

advertisements

I am using Ng-Table to show a list of items with 3 columns: Name, Rack, Group.

I wanted to apply 'strict comparison' to those columns, so what I did was:

  self.tableParams = new ngTableParams({
    page: 1,
    // This count: 2 can be ignored as we change it in every request.
    count: 50,
  }, {
    counts: [],
    total: 100,
    getData: function ($defer, params) {

      var filteredData = params.filter() ? $filter('filter')(self.data, params.filter(), true) : self.data;
      var orderedData = params.sorting() ? $filter('orderBy')(filteredData, params.orderBy()) : self.data;

      $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
    }
  });

You can assume that self.data contains all my data and var self = this; in my controller.

The problem with the afore mentioned code, is that when you erase the filter string in one of the columns, the $filter('filter')(self.data, params.filter(), true) returns no result.

I found that, when you erase on column (lets say Name column), params.filter() will contain an object like this: {'name': ""} which is passing to the $filter('filter') and the filter is trying to match only empty strings!!

I tried to override this by adding the following code:

   getData: function ($defer, params) {
      // When the user deletes the search text, this object keeps a variable with an empty string. The
      // strict comparator will accept only empty strings. To overcome this problem we delete the property
      for (var propt in params.filter()) {
        if (params.filter()[propt] === '') {
          delete params.filter()[propt];
        }
      }

      var filteredData = params.filter() ? $filter('filter')(self.data, params.filter()) : self.data;
      var orderedData = params.sorting() ? $filter('orderBy')(filteredData, params.orderBy()) : self.data;

      $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
    }

My problem is that is not an elegant solution, and this code is looping for all search columns to see if one of them is an empty string. Can I do something faster? That can find exactly which column is empty and delete the property? Something more elegant and less hacky?


I have a little correction for your solution, because, in case of using your code, filter after input parameter, erase, input do not work. For getting filtered data user have to input parameter again. That's why I copied params.filter() to another variable and worked whit it.

getData: function ($defer, params) {

  var customFilter = angular.copy(params.filter());

  for (var propt in customFilter) {
   if (customFilter[propt] === '') {
         delete customFilter[propt];
      }
   }
orderedData = customFilter ? $filter('filter')(orderedData, customFilter, true) : orderedData;




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK