2012-06-15 66 views
0

更新了例:奇过滤行​​为标记

http://jsfiddle.net/7Cwbn/62/

您可以点击标记

保持Ctrl键选择多个选项

UPDATE:

我摆弄脚本的详细一些,并与array_diff()函数作为if S的内部测试声明取代jQuery.inArray()。一点点改变if逻辑。我试图用新的代码只是的房子,它似乎工作,当我启用相同的过滤代码的公寓 - 过滤得到了一些混乱。

例如:如果我从中选择一切功能 - 在地图上什么都没有显示。但是一些标记应该已经显示出来了,因为我至少有两个具有所有三个可用功能。

$(markers.houses).each(function(index, elem) { 
     if ((array_diff(selectedFeatures, elem.features).length === 0) && (array_diff(selectedNearby, elem.nearby).length === 0)) { 
      if (!markers.houseMarkers[index].visible) { 
       markers.houseMarkers[index].setVisible(true); 
      } 
     } 
    }); 
$(markers.condos).each(function(index, elem) { 
     if ((array_diff(selectedFeatures, elem.features).length === 0) && (array_diff(selectedNearby, elem.nearby).length === 0)) { 
      if (!markers.condoMarkers[index].visible) { 
       markers.condoMarkers[index].setVisible(true); 
      } 
     } 
    }); 

图1.1 - 的片房屋和公寓

+1

我会建议广泛使用控制台。登录尝试并找出过滤执行的每个阶段发生了什么 – duncan

+0

@duncan:真的吗?我自己不会这么想。 – Bob

+0

等等记录的结果告诉你什么? – duncan

回答

1

您在这里有一个错字(selectedFeaturess),这可能是与它过滤代码:

if (jQuery.inArray(selectedFeaturess[i], elem.features) !== -1 || jQuery.inArray(selectedFeatures[i], elem.features) > -1) { 

此外,这不是t Ø帮助我怀疑:

<option value="Wendy's">Harveys</option> 

这样的代码:

if (jQuery.inArray(selectedFeatures[i], elem.features) !== -1 || jQuery.inArray(selectedFeatures[i], elem.features) > -1) { 

可以只是简单:

if (jQuery.inArray(selectedFeatures[i], elem.features) !== -1) { 

因为如果它> -1,那么它的== -1,!所以第二个条款是多余的。例如如果你有它= 0,那么IF子句的第一部分触发,不需要执行IF语句的第二部分。

最后,这是对$(document).ready()函数的重写。主要问题是如何循环数组中的元素。不要把它们当作jquery选择器,而是对它们做一个.each(),你只需要做一个简单的For循环。但是你可以使用jquery选择器来检查它们的长度。这适用于我(我也在选项中将Wendy's改名为Harveys)。

$(document).ready(function() { 
    //$('#filter').on('click', function(e) { 
    $('#filter').click(function(e) { 
     // prevent refreshing of the page 
     e.preventDefault(); 

     // close all infoWindows 
     infowindow.close(); 

     // hide all markers 
     $(markers.houses).each(function(index, elem) { 
      markers.houseMarkers[index].setVisible(false); 
     }); 
     $(markers.condos).each(function(index, elem) { 
      markers.condoMarkers[index].setVisible(false); 
     }); 

     // get the selected features from select box 
     var selectedFeatures = $("#features").val(); 
     var selectedNearby = $("#nearby").val(); 

     // for each house element... 
     $(markers.houses).each(function(index, elem) { 
      //first filter by selected features 
      if ($(selectedFeatures).length) { 
       for (var i = 0; i < selectedFeatures.length; i++) {  
        if (jQuery.inArray(selectedFeatures[i], elem.features) !== -1) {  
         if (!markers.houseMarkers[index].visible) { 
          markers.houseMarkers[index].setVisible(true); 
         } 
        } 
       } 
      } 

      //then filter by nearby selections 
      if ($(selectedNearby).length) { 
       for (var i = 0; i < selectedNearby.length; i++) { 
        if (jQuery.inArray(selectedNearby[i], elem.nearby) !== -1) { 
         // if marker is not visible, but meets the filtering criteria - show it 
         // otherwise leave it as it is 
         if (!markers.houseMarkers[index].visible) { 
          markers.houseMarkers[index].setVisible(true); 
         } 
        } 
       } 
      } 
     }); 

     // for each condo element... 
     $(markers.condos).each(function(index, elem) { 

      // first filter by selected features 
      if ($(selectedFeatures).length) { 
       for (var i = 0; i < selectedFeatures.length; i++) {  
        if (jQuery.inArray(selectedFeatures[i], elem.features) !== -1) { 
         // if marker is not visible, but meets the filtering criteria - show it 
         // otherwise leave it as it is 
         if (!markers.condoMarkers[index].visible) { 
          markers.condoMarkers[index].setVisible(true); 
         } 
        } 
       } 
      } 

      //then filter by nearby selections 
      if ($(selectedNearby).length) { 
       for (var i = 0; i < selectedNearby.length; i++) { 
        if (jQuery.inArray(selectedNearby[i], elem.nearby) !== -1) { 
         // if marker is not visible, but meets the filtering criteria - show it 
         // otherwise leave it as it is 
         if (!markers.condoMarkers[index].visible) { 
          markers.condoMarkers[index].setVisible(true); 
         } 
        } 
       } 
      } 
     }); 
    }); 
}); 
+0

这解决了公寓标记未显示的问题,但其余问题仍然存在。 – Bob

+0

查看我的更新回答 – duncan