I have a model for my view.
That model is array of objects:
var arr = { "12345qwery": { prop1: "value", prop2: "value" } } // contains 500 items
And today I am filtering it in the following way:
arr = $filter('filter')(arr, filterTerm); // contains 4 items
And after this line I get nice filtered data but if I run this filter again I don't have 500 items in it but 4.
So to avoid this I store original array in temporary object and when user change filter I first update arr with backup data (it's original 500 items) and do the filtering.
Now I get in trouble as I have multiple filters and I must restore original data before each filter... anyway it is a mess :)
Is there any better (angular) way to make this in javascript filtering?
UPDATE
To explan better what is issue I created plunker:
https://plnkr.co/edit/99b02UtUfPeM3wl4IiX6?p=preview
As you can see I load markers with objects and want to filter it via text field.
But I can't as I always get some errors.
Am I doing anything wrong here?
And to avoid this and implement filter somehow that is why I decided to do it in code and keep original array after each filter, but this is very complex solution and I wan't to make it in a more natural angular way.
BOUNTY UPDATE
I am filtering object in js
code because I can't find a way to filter markers on this directive in a standard angular way.
That is why I filter in code and before filter always make a copy of it.
I need help to filter marker objects on this directive in a standard angular way.
Plunker implement this directive but I don't know how to filter it.
You could do something like I've done in a plunker forked from yours. Create a factory where you keep your marker objects, return them to the controller as called, and then filter them according to the
filterTerm
(which wasn't within the scope of your controller in your original plunker, btw).And then in your controller, you can initialize the map by putting all the markers on
$scope
(withangular.extend($scope, { markers: myMarkers.markers });
), and then watch the value of your$scope.filterTerm
to filter the$scope.markers
object accordingly.Now it filters on the fly and adds the markers back as you reduce the filter term. Note that I'm using lodash's
_.filter()
method to filter in the factory, but you've probably already got lodash as a dependency anyway.One way to filter in angularjs view
Or you can also try in controller something like this
I don't know that there is an easy built-in 'angular' way to approach this. Here is how I would handle filtering a list by multiple filters. I would keep an array of filters, and then anytime any of those filters changed, regenerate the list of results based on ALL of the filters.
Take a look at this snippet and see if it does what you are looking for.
Use
$watch('searchTerm')
to filter on change and transform themarkers
Object to array before apply$filter
.Finally use the filteredMarkers on directive:
See updated plunker: https://plnkr.co/edit/P5bNzHmZ2CRjImbMztyr?p=preview
The problem is that you are trying to filter an object instead an array.
Try to build your own custom filer:
see:https://plnkr.co/edit/Gi4gWHne57owB44MTsAB?p=preview