Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Implement filtering on object-based ng-repeat #6490

Closed
quantizor opened this issue Feb 28, 2014 · 13 comments
Closed

Implement filtering on object-based ng-repeat #6490

quantizor opened this issue Feb 28, 2014 · 13 comments

Comments

@quantizor
Copy link

Currently if you are using the ng-repeat directive to walk over the properties of a non-array object, filters will not work as they test immediately if the collection is an array and reject anything not matching.

This behavior can be seen here:

if (!isArray(array)) return array;
(thanks @caitp !)

This is an undocumented and - per #6215 - potentially unwanted behavior, since the syntax is the same for iterating over arrays and objects. Plus, it's just darn useful!

Opening up the lines of discussion here to see if it's worth implementing filter functionality for objects and not just straight array/array-like structures.

@smithl
Copy link
Contributor

smithl commented Jun 4, 2014

+1

@Narretz Narretz added this to the Backlog milestone Jul 2, 2014
@btford btford removed the gh: issue label Aug 20, 2014
@paishin
Copy link

paishin commented Oct 26, 2014

+1

@prady00
Copy link

prady00 commented Oct 26, 2014

+1 nice to have!
On 26-Oct-2014 5:34 am, "paishin" notifications@github.com wrote:

+1

Reply to this email directly or view it on GitHub
#6490 (comment).

@quantizor
Copy link
Author

@gkalpak Been a while since I looked at this code :) what would you like changed in regards to the tests?

@gkalpak
Copy link
Member

gkalpak commented Oct 28, 2014

@3lux: Not really sure :) It's more of a fuzzy feeling. I sure don't like the "obscure" helpers (maybe just because I don't get their usefulness). I had left some inline comments the other day.

@emaV
Copy link

emaV commented Nov 25, 2014

Would be a cast to array on the filterFilter an option?

function filterFilter() {
  return function(array, expression, comparator) {
    // cast to array
    if (!isArray(array)) {
      var newArray = [];
      forEach(array, function(obj) { 
        this.push(obj); 
      }, newArray);
      array = newArray;
    }

@thiago-negri
Copy link

+1

1 similar comment
@tsykora
Copy link

tsykora commented Dec 25, 2014

+1

@lgalfaso
Copy link
Contributor

lgalfaso commented Jan 3, 2015

this looks like a perfect solution for a third-party module. BTW, just by adding https://github.com/petebacondarwin/angular-toArrayFilter you can do and more

@Philzen
Copy link

Philzen commented Jan 22, 2015

👍

@Narretz
Copy link
Contributor

Narretz commented Jan 21, 2016

WE've decided that the filterFilter should only work on arrays. It'll throw in 1.4 and beyond if it is passed an object, see cea8e75

@Narretz Narretz closed this as completed Jan 21, 2016
@Philzen
Copy link

Philzen commented Jan 27, 2016

@Narretz - throwing an error will help newbies a lot, as it indicates unintended use straightaway instead of wondering at some later point in time why it doesn't work as expected.

WE've decided that the filterFilter should only work on arrays.

Could you maybe shed some light on the arguments behind this decision, so us "consumers" can embrace it better and understand why it makes sense not to have an object-based ng-repeat filter?

@gkalpak
Copy link
Member

gkalpak commented Jan 28, 2016

It's been discussed in several places/issues, but I believe #2694 (comment) sums it up nicely.

Basically, it's not something that can be done reliably and without loss of information and the usecase shouldn't be common (plus it can be very easily implemented as a 3rd-party module and not contribute to the size of the core).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.