huf*_*uff 5 javascript jquery knockout.js
我正在尝试创建一个搜索栏,如果它们与搜索查询不匹配,则会从列表中过滤掉项目.我想要添加的附加功能是,如果它与搜索查询不匹配,它也会从地图中删除引脚.
这就是我现在所拥有的,它适用于删除页面顶部的名称,但ID也可以删除引脚.我想知道如何解决这个问题.
self.pins = ko.observableArray([
new self.mapPin("Waterloo Station", 51.503035, -0.112326, "test3""),
new self.mapPin("King's Cross Station", 51.530984, -0.122583, "test2"),
new self.mapPin("Putney Bridge", 51.468050, -0.209081, "test1")
]);
self.query = ko.observable('');
self.filterPins = ko.computed(function () {
var search = self.query().toLowerCase();
return ko.utils.arrayFilter(self.pins(), function (pin) {
return pin.name().toLowerCase().indexOf(search) >= 0;
});
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<input data-bind="value: query, valueUpdate: 'keyup'" autocomplete="off" type="text" class="form-control" placeholder="Filter Locations">
<ul data-bind="foreach: filterPins" class="nav navbar-nav" class=" nav navbar-nav">
<li class="active">
<a data-bind="text: name"></a>
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激!
我会将您的标记封装到他们自己的数据模型中,该模型负责在幕后与Google地图的交互:
// we have to give it access to the map object, so that
// it can register and de-register itself
var Pin = function Pin(map, name, lat, lon, text) {
var marker;
this.name = ko.observable(name);
this.lat = ko.observable(lat);
this.lon = ko.observable(lon);
this.text = ko.observable(text);
marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lon),
animation: google.maps.Animation.DROP
});
this.isVisible = ko.observable(false);
this.isVisible.subscribe(function(currentState) {
if (currentState) {
marker.setMap(map);
} else {
marker.setMap(null);
}
});
this.isVisible(true);
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以创建引脚pin = new Pin(map, 1, 2, 'text'),当您切换其可见性状态时pin.isVisible(false),它们会自动注册或取消注册地图.
因此,您的过滤功能变为
self.filterPins = ko.computed(function () {
var search = self.query().toLowerCase();
return ko.utils.arrayFilter(self.pins(), function (pin) {
var doesMatch = pin.name().toLowerCase().indexOf(search) >= 0;
pin.isVisible(doesMatch);
return doesMatch;
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1693 次 |
| 最近记录: |