Set marker visible with knockout JS ko.utils.arrayFilter

前端 未结 1 1171
孤街浪徒
孤街浪徒 2020-12-17 06:33

Hello guys I am trying to create an app that sets the appropriate markers visible when a knockout search is being done.

Basically the app is. When someone does a sea

相关标签:
1条回答
  • 2020-12-17 07:29

    All you need is to set the visibility of the marker to match whether it is found:

    if (!filter) {
      // this is new
      ko.utils.arrayForEach(self.listLoc(), function (item) {
        item.marker.setVisible(true);
      });
      return self.listLoc();
    } else {
      return ko.utils.arrayFilter(self.listLoc(), function(item) {
        var result = (item.title.toLowerCase().search(filter) >= 0)
        item.marker.setVisible(result); // this is a new line
        return result;
      });
    }
    

    Working fiddle.

    Note: unless you're supporting particularly old browsers, you can use the Array filter method rather than Knockout's arrayFilter util, and .foreach instead of arrayForEach.

    0 讨论(0)
提交回复
热议问题