2012-06-15 117 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