Brett Davis
Brett Davis

Reputation: 162

Issue Filtering Knockout.js observable array

I have a knockout.js app that has an observable array. I've added a filter to the observable array. The filtering is working properly. The issue is I'm calling another function as I'm filtering data. Here is my function:

self.filterResults = function(){
         var value = self.searchInput().toLowerCase();
         if(value != ''){
             clearMarkers();
             self.burgerList(self.burgerList().filter(function(data){
                 addmarker(data.lat(), data.long(), data.id(), data.name(), data.comments());
                 return data.name().toLowerCase().startsWith(value); 
             }));
         }else{
           showMarkers();
           self.burgerList(self.burgerListClone());
         }
     };

There seems to be a lag as I'm calling the addmarker while I'm doing the filtering.

I've pasted all the code into a jsfiddle to show an example of what I'm doing: https://jsfiddle.net/maL3zqgq/5/

Upvotes: 0

Views: 41

Answers (1)

Michael Best
Michael Best

Reputation: 16688

Because you're adding the markers before filtering, you'll see the results of the previous filter. You need to add the markers after filtering:

         self.burgerList(self.burgerList().filter(function(data){
             return data.name().toLowerCase().startsWith(value); 
         }));
         clearMarkers();
         self.burgerList().forEach(function(data){
             addmarker(data.lat(), data.long(), data.id(), data.name(), data.comments());
         });

https://jsfiddle.net/mbest/maL3zqgq/6/

Upvotes: 1

Related Questions