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