Saulis / iron-data-table

iron-data-table is a Web Component for displaying data as a table or grid. Built on top of iron-list using Polymer.
Apache License 2.0
147 stars 66 forks source link

Problem filtering (related to fix for issue #168) #171

Closed lluisgener closed 7 years ago

lluisgener commented 7 years ago

Filtering seems to have gone worse with the fix to #168

If a field has a value, and the filter is empty (string value is ""), it returns false for every row, rendering the whole grid empty.

This code seems to work well for every case I have tested:

return Array.prototype.filter.call(items, function(item, index) {
  for (var i = 0; i < filter.length; i++) {
    var value = Polymer.Base.get(filter[i].path, item);

    //Fix filtering when value is null (ISSUE #168)
    if(value == null || value == undefined)
        value = "";

    if ([undefined, null].indexOf(filter[i].filter) > -1) {
      continue;
    } else if (value.toString().toLowerCase().indexOf(filter[i].filter.toString().toLowerCase()) === -1) {
      return false;
    }
  }
  return true;
});
Saulis commented 7 years ago

Hmm.. If I understand you correctly, that should not be happening.

If the item property has a value, let's say "foobar" and the filter is empty (""), "foobar".indexOf('') returns 0 which will evaluate to true in return the item.

I have even a test case for it here: https://github.com/Saulis/iron-data-table/blob/master/test/filtering.html#L84

I'm guessing you mean a situation where the item does NOT have a value and the filter is "" ? That's a use case I haven't considered earlier, can you try if the branch ignore-empty-filter works better for you?

lluisgener commented 7 years ago

You're right, the problem is when the value is undefined or null, and the filter is "".

The branch seems to work fine for me. Will it make it to trunk?

Thanks.

Saulis commented 7 years ago

Sure, I'll make a new release asap