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

filter and orderBy should also work with objects in ngRepeat #8458

Closed
nodecode opened this issue Aug 2, 2014 · 9 comments
Closed

filter and orderBy should also work with objects in ngRepeat #8458

nodecode opened this issue Aug 2, 2014 · 9 comments

Comments

@nodecode
Copy link

nodecode commented Aug 2, 2014

filter and orderBy should work with objects within ngRepeat. My use case for this, I have already written recently in #1286, but the issue only refers to oderBy and has previously been rejected seemingly because no concrete use case has been found for this.

Here is my use case where I absolutely need an object instead of an array:

I'm currently developing a real-time browser game with AngularJS, in which I have a room list. To update individual entries in this list, the server sends a notification with the new version of this single entry when it changes. Thus, a single entry can be by ID, provided by the server, updated, I need to use an object instead of an array.

I would like to sort these entries with AngularJS, which, however, currently not working. I would be happy if this feature will be implemented soon. Otherwise, I had to write such a function myself, which I however come back in the boilerplate code hell.

A support of objects in filter and orderBy of AngularJS for me would be really helpful as I would otherwise have to try myself to implement such a function.

@petebacondarwin
Copy link
Contributor

This is easy to fix using a toArray filter.

Here is a solution that uses @finalclass's toArray filter (remembering that this will not work on IE8): http://jsfiddle.net/z8ME5/

Here is a version that uses toArray but actually modifies the original object (and will work on IE8):
http://jsfiddle.net/JHL6X/

@petebacondarwin
Copy link
Contributor

And here is a version with a filter: http://jsfiddle.net/75LLk/

@petebacondarwin
Copy link
Contributor

I have created a module that provides the toArray filter for helping with these situations

http://ngmodules.org/modules/angular-toArrayFilter

@btford btford removed the gh: issue label Aug 20, 2014
@mohsen1
Copy link
Contributor

mohsen1 commented Aug 28, 2014

I am having a problem with lack of built in orderBy support in Angular. The toArray is not working for me because it makes angular re-render the entire array which looks bad in my case because I want to update my document per kepress.

It would be nice if I could sort my object values without converting it to an array.

@petebacondarwin
Copy link
Contributor

@mohsen1 - you can always achieve what you want by doing the ordering in a controller: watching the object, ordering it and attaching the ordered list to the scope, which you can then bind to. But regarding toArray filter, it should be maintaining object references in the array so ng-repeat should not be re-rendering on every digest. What is you use case?

@kcrwfrd
Copy link

kcrwfrd commented Oct 3, 2014

People are advocating for a toArray filter, and while I think that keeps good separation of concerns, I believe it would be costly performance-wise. Filters in the view may be invoked many times.

@caitp
Copy link
Contributor

caitp commented Oct 3, 2014

there have been recent fixes to that problem @kvcrawford --- they will now (by default) be treated as pure and only re-run if their inputs change

@wesleycho
Copy link
Contributor

Given that this has been rejected for filterFilter in #2694, this probably should be closed as won't support since orderBy suffers the same issues.

@petebacondarwin
Copy link
Contributor

Yes, I agree - and we have a performance workaround.

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

No branches or pull requests

7 participants