本文介绍了使用淘汰赛 JS ko.utils.arrayFilter 设置标记可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在尝试创建一个应用程序,在完成淘汰搜索时设置适当的标记可见.

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

基本上是应用程序.当有人搜索其下方的列表时,过滤该列表并仅使与过滤器列表关联的标记在地图上可见.我创建了一个 ko.utils.arrayFilter 并且我试图只设置 item.marker.setVisible(true)

Basically the app is.When someone does a search the list that is bellow it, filters the list and makes only the markers that are associated with the filter list visible on the map.I have created a ko.utils.arrayFilter and I am trying to set only the item.marker.setVisible(true)

我的 Github 链接是 https://github.com/Aimpotis/map3

My Github link is https://github.com/Aimpotis/map3

再次感谢您,感谢社区帮助我学到了很多东西

Thank you again and much respect to the community it is helping me learn a lot

推荐答案

你只需要设置标记的可见性来匹配是否找到:

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;
  });
}

工作小提琴.

注意:除非您支持特别旧的浏览器,否则您可以使用 数组过滤器方法 而不是 Knockout 的 arrayFilter util,以及 .foreach 而不是 arrayForEach.

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.

这篇关于使用淘汰赛 JS ko.utils.arrayFilter 设置标记可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:43