2015-05-15 34 views
3

要解釋好我有4種類型的過濾器:位置,類型,等級,設施。C#和Ajax過濾(4個過濾器,多發於每個選項)

我現在有這一切工作,但每一個過濾器的工作原理爲左右,它只是對結果增加。我希望它是過濾器類型之間的AND(& &),並且每個類型的相同過濾器變爲OR。因此,一個例子是「蘇格蘭||英格蘭& &酒店& & 4星|| 5星& &兒童遊樂區||池。

我試圖創建刪除那些不等於結果添加的列表,但如果你沒有與位置開始它會失敗

這裏是我當前的代碼,它作爲剛剛全部或過濾器

// Get by Filters // 
    public String GetAccomFilterPages(Int32 ModelID, Int32 page, String location, String type, String grade, String facility) 
    { 
     // Builder Start // 
     AccommodationList Model = Umbraco.TypedContent(ModelID) as AccommodationList; 
     StringBuilder HTMLAccom = new StringBuilder(); 

     //Build list and use | to split // 
     List<Accommodation> Results = new List<Accommodation>(); 
     Boolean noFilter = true; 

     // Location 
     if (!String.IsNullOrEmpty(location)) 
     { 
      String[] Locations = location.Split('|'); 
      foreach (String locSearch in Locations) 
      { 
       if (!String.IsNullOrEmpty(locSearch)) 
       { 
        foreach (var locItem in Model.Descendants<Accommodation>().Where(x => x.Location.Where(xx => xx.Name.Replace("&", "and") == locSearch.Replace("&", "and")).Count() > 0)) 
        { 
         if (!Results.Contains(locItem)) 
         { 
          Results.Add(locItem); 
         } 
        } 
       } 
      } 
      noFilter = false; 
     } 

     // Accom Types 
     if (!String.IsNullOrEmpty(type)) 
     { 
      String[] Types = type.Split('|'); 
      foreach (String typSearch in Types) 
      { 
       if (!String.IsNullOrEmpty(typSearch)) 
       { 
        foreach (var typItem in Model.Descendants<Accommodation>().Where(x => x.AccommodationType.Where(xx => xx.Name.Replace("&", "and") == typSearch.Replace("&", "and")).Count() > 0)) 
        { 
         if (!Results.Contains(typItem)) 
         { 
          Results.Add(typItem); 
         } 
        } 
       } 
      } 
      noFilter = false; 
     } 

     // Grades 
     if (!String.IsNullOrEmpty(grade)) 
     { 
      String[] Grades = grade.Split('|'); 
      foreach (String gradeSearch in Grades) 
      { 
       if (String.IsNullOrEmpty(gradeSearch)) 
       { 
        foreach (var gradeItem in Model.Descendants<Accommodation>().Where(x => x.GradeRating.Where(xx => xx.Name.Replace("&", "and") == gradeSearch.Replace("&", "and")).Count() > 0)) 
        { 
         if (!Results.Contains(gradeItem)) 
         { 
          Results.Add(gradeItem); 
         } 
        } 
       } 
      } 
      noFilter = false; 
     } 

     // Facilities 
     if (!String.IsNullOrEmpty(facility)) 
     { 
      String[] Facilities = facility.Split('|'); 
      foreach (String facSearch in Facilities) 
      { 
       if (String.IsNullOrEmpty(facSearch)) 
       { 
        foreach (var facItem in Model.Descendants<Accommodation>().Where(x => x.FacilitiesAvaliable.Where(xx => xx.Name.Replace("&", "and") == facSearch.Replace("&", "and")).Count() > 0)) 
        { 
         if (!Results.Contains(facItem)) 
         { 
          Results.Add(facItem); 
         } 
        } 
       } 
      } 
      noFilter = false; 
     } 

     if (noFilter == true) 
     { 
      Results.AddRange(Model.Descendants<Accommodation>()); 
     } 

     // End Filtering // 
     int skip = 0; 
     int take = 5; 
     int totalNodes = Results.Count(); 
     int totalPages = (int)Math.Ceiling((double)totalNodes/take); 

     // Results Count // 
     int lastIdOnPage = take; 

     if (page != 1) 
     { 
      skip = take * (page - 1); 
      lastIdOnPage = (totalNodes - skip); 
      if (lastIdOnPage > take) 
      { 
       lastIdOnPage = skip + take; 
      } 
      else 
      { 
       lastIdOnPage = skip + lastIdOnPage; 
      } 

     } 
     else if (take > totalNodes) 
     { 
      lastIdOnPage = totalNodes; 
     } 
     int firstIdOnPage = skip + 1; 

     // Start Print of Page // 
     // Loop through Accoms // 

     foreach (Accommodation ACC in Results.Skip(skip).Take(take)) 
     { 
      .... 
    } 

     // Filter Reset // 
     if (noFilter == false) 
     { 
      HTMLAccom.AppendLine("<li><a onclick=\"GetAccomPages(1, false)\">Remove Filters</a></li>"); 
     } 

     // Pagination Results // 
     if (firstIdOnPage == lastIdOnPage) 
     { 
      HTMLAccom.AppendLine("<p>" + firstIdOnPage + " of " + totalNodes + " results</p>"); 
     } 
     else if (lastIdOnPage == 0) 
     { 
      HTMLAccom.AppendLine("<p> No results</p>"); 
     } 
     else 
     { 
      HTMLAccom.AppendLine("<p>" + firstIdOnPage + " - " + lastIdOnPage + " of " + totalNodes + " results</p>"); 
     } 

     // Page Links // 
     if (totalPages > 1) 
     { 
      int countPages = 0; 
      HTMLAccom.AppendLine("<ul class=\"pagination\">"); 
      while (totalPages > countPages) 
      { 
       countPages++; 
       HTMLAccom.AppendLine("<li " + (countPages == page ? "class=\"active\">" : "") + "<a onclick=\"GetAccomFilterPages(" + (countPages) + ")\">" + countPages + "</a></li>"); 
      } 
      HTMLAccom.AppendLine("</ul>"); 
     } 

     return HTMLAccom.ToString(); 
    } 

前端JS/AJAX(不含URL修正:。

$(document).ready(function() { 
    var page = @page + ""; 
    var ShowAll = @ShowAll + ""; 

    if (page != "") 
     GetAccomPages(page, false) 
    else 
     GetAccomPages(1, false); 
}); 

function GetAccomPages(page, ShowAll) { 
... 
} 

function GetAccomFilterPages(page, location, type, grade, facility) { 

    var filterName = [locFilterName, typeFilterName, gradeFilterName, facFilterName]; 
    for (name in filterName){ 
     if ($("#ft" + name).prop('checked')) { 
      $("#ft" + name).prop('checked', false) 
     } else { 
      $("#ft" + name).prop('checked', true) 
     } 
    } 

    var locFilterName = ""; 
    var typeFilterName = ""; 
    var gradeFilterName = ""; 
    var facFilterName = ""; 

    var rows = $('.chkLoc'); 
    $.each(rows, function() { 
     if ($(this).prop('checked')) { 
      locFilterName += $(this).val() + "|"; 
     } 
    }); 

    var rows = $('.chkType'); 
    $.each(rows, function() { 
     if ($(this).prop('checked')) { 
      typeFilterName += $(this).val() + "|"; 
     } 
    }); 

    var rows = $('.chkGrade'); 
    $.each(rows, function() { 
     if ($(this).prop('checked')) { 
      gradeFilterName += $(this).val() + "|"; 
     } 
    }); 

    var rows = $('.chkFac'); 
    $.each(rows, function() { 
     if ($(this).prop('checked')) { 
      facFilterName += $(this).val() + "|"; 
     } 
    }); 

    //alert(locFilterName + typeFilterName + gradeFilterName + facFilterName); 

    var AC = "ModelID=" + @Model.Content.Id + "&page=" + page + "&location=" + locFilterName + "&type=" + typeFilterName + "&grade=" + gradeFilterName + "&facility=" + facFilterName; 
     $.ajax({ 
      type: "GET", 
      url: "/Umbraco/Api/AccomAjax/GetAccomFilterPages", 
      data: AC, 
      success: function (data) { 
       $("#GetAccom").html(data); 
      } 
     }); 

    } 

這是一個完全工作或過濾器的更新爲你打勾的每個複選框可以隨意從它採取一些想法,如果你想(如果你遇到了這個尋找如何使一個),但我需要它與每個過濾器之間的「和」,我就像他們會說的那樣陷入泥潭中。我也是一個PHP開發人員,所以如果我做了多餘的事情,請給一些CC。

在此先感謝。

+0

如果您發佈該課程的住宿代碼,您將從中受益,因爲這將更容易回答您的問題。基本上,你應該做的不是過濾分階段的結果,只需保存不同的過濾器選項,然後使用or和相應地執行包含所有過濾器數據的linq查詢。 –

回答

0

因此,與同事(.NET開發人員)的幫助下,我們設法得到它。任何建設性的批評都是受歡迎的,但這是我們的作品。功能的一個區別在於我的初始問題是設施的最後一個過濾器,所有的設施都是ANDs:example - Scotland ||愛爾蘭& &酒店& & 4星|| 2星& &游泳池& &健身房& &酒吧。因此,對於設施仔細一看,我切換它是相對於第一3.

任何人都希望在這個指導做的,只是我的後端改變沒有前端的變化。在前端還需要一些額外的東西,比如url過濾器和聲明一些空的變量,如page,showAll等。並建立你的輸入複選框與相關的值和id名稱,以配合您的JS。

// Get by Filters // 
     public String GetAccomFilterPages(Int32 ModelID, Int32 page, String location, String type, String grade, String facility) 
     { 
      // Builder Start // 
      AccommodationList Model = Umbraco.TypedContent(ModelID) as AccommodationList; 
      StringBuilder HTMLAccom = new StringBuilder(); 

      //Build list and use | to split // 

      List<Accommodation> LocationResults = new List<Accommodation>(); 
      List<Accommodation> TypeResults = new List<Accommodation>(); 
      List<Accommodation> GradeResults = new List<Accommodation>(); 
      List<Accommodation> FacResults = new List<Accommodation>(); 
      List<Accommodation> Results = new List<Accommodation>(); 
      Boolean noFilter = true; 

      // Location 
      if (!String.IsNullOrEmpty(location)) 
      { 
       String[] Locations = location.Split('|'); 
       foreach (String locSearch in Locations) 
       { 
        if (!String.IsNullOrEmpty(locSearch)) 
        { 
         foreach (var locItem in Model.Descendants<Accommodation>().Where(x => x.Location.Where(xx => xx.Name.Replace("&", "and") == locSearch.Replace("&", "and")).Count() > 0)) 
         { 
          if (!LocationResults.Contains(locItem)) 
          { 
           LocationResults.Add(locItem); 
          } 
         } 
        } 
       } 
       noFilter = false; 
      } 
       // If no filters set to all descendants. Similar Else's work to make sure if a filter isn't used the filtering still works by defaulting to check through all descendants. 
      else 
      { 
       LocationResults.AddRange(Model.Descendants<Accommodation>()); 
      } 

      // Accom Types 
      if (!String.IsNullOrEmpty(type)) 
      { 
       String[] Types = type.Split('|'); 
       foreach (String typSearch in Types) 
       { 
        if (!String.IsNullOrEmpty(typSearch)) 
        { 
         foreach (var typItem in LocationResults.Where(x => x.AccommodationType.Where(xx => xx.Name.Replace("&", "and") == typSearch.Replace("&", "and")).Count() > 0)) 
         { 
          if (!TypeResults.Contains(typItem)) 
          { 
           TypeResults.Add(typItem); 
          } 
         } 
        } 
       } 
       noFilter = false; 
      } 
      else 
      { 
       TypeResults.AddRange(LocationResults); 
      } 


      // Grades 
      if (!String.IsNullOrEmpty(grade)) 
      { 
       String[] Grades = grade.Split('|'); 
       foreach (String gradeSearch in Grades) 
       { 
        if (!String.IsNullOrEmpty(gradeSearch)) 
        { 
         foreach (var gradeItem in TypeResults.Where(x => x.GradeRating.Where(xx => xx.Name.Replace("&", "and") == gradeSearch.Replace("&", "and")).Count() > 0)) 
         { 
          if (!GradeResults.Contains(gradeItem)) 
          { 
           GradeResults.Add(gradeItem); 
          } 
         } 
        } 
       } 
       noFilter = false; 
      } 
      else 
      { 
       GradeResults.AddRange(TypeResults); 
      } 



      // Facilities 
      //For this section we are using a 'for' since we are wanting to remove non-matching results 

      FacResults.AddRange(GradeResults); 
      if (!String.IsNullOrEmpty(facility)) 
      { 
       String[] Facilities = facility.Split('|'); 
       foreach (String facSearch in Facilities) 
       { 
        if (!String.IsNullOrEmpty(facSearch)) 
        { 
         for (int i = FacResults.Count - 1; i >= 0; i--) 
         { 
          Accommodation currentAccom = FacResults[i]; 
          if (currentAccom.FacilitiesAvaliable.Where(x => x.Name.Replace("&", "and") == facSearch.Replace("&", "and")).Count() == 0) 
          { 
           FacResults.Remove(currentAccom); 
          } 
         } 
        } 
       } 
       noFilter = false; 
      } 

      if (noFilter == true) 
      { 
       Results.AddRange(Model.Descendants<Accommodation>()); 
      } 
      else 
      { 
       Results.AddRange(FacResults);    
      } 

      // End Filtering // 
      int skip = 0; 
      int take = 5; 
      int totalNodes = Results.Count(); 
      int totalPages = (int)Math.Ceiling((double)totalNodes/take); 

      // Results Count // 
      int lastIdOnPage = take; 

      if (page != 1) 
      { 
       skip = take * (page - 1); 
       lastIdOnPage = (totalNodes - skip); 
       if (lastIdOnPage > take) 
       { 
        lastIdOnPage = skip + take; 
       } 
       else 
       { 
        lastIdOnPage = skip + lastIdOnPage; 
       } 

      } 
      else if (take > totalNodes) 
      { 
       lastIdOnPage = totalNodes; 
      } 
      int firstIdOnPage = skip + 1; 

      // Start Print of Page // 
      // Loop through Accoms // 

      foreach (Accommodation ACC in Results.Skip(skip).Take(take)) 
      { 
       ... 
      } 

      // Filter Reset // 
      if (noFilter == false) 
      { 
       HTMLAccom.AppendLine("<li><a onclick=\"GetAccomPages(1, false)\">Remove Filters</a></li>"); 
      } 

      // Pagination Results // 
      if (firstIdOnPage == lastIdOnPage) 
      { 
       HTMLAccom.AppendLine("<p>" + firstIdOnPage + " of " + totalNodes + " results</p>"); 
      } 
      else if (lastIdOnPage == 0) 
      { 
       HTMLAccom.AppendLine("<p> No results</p>"); 
      } 
      else 
      { 
       HTMLAccom.AppendLine("<p>" + firstIdOnPage + " - " + lastIdOnPage + " of " + totalNodes + " results</p>"); 
      } 

      // Page Links // 
      if (totalPages > 1) 
      { 
       int countPages = 0; 
       HTMLAccom.AppendLine("<ul class=\"pagination\">"); 
       while (totalPages > countPages) 
       { 
        countPages++; 
        HTMLAccom.AppendLine("<li " + (countPages == page ? "class=\"active\">" : "") + "<a onclick=\"GetAccomFilterPages(" + (countPages) + ")\">" + countPages + "</a></li>"); 
       } 
       HTMLAccom.AppendLine("</ul>"); 
      } 

      return HTMLAccom.ToString(); 
     }