C # and Ajax filtering (4 filters, multiple options each)

To explain that I have 4 types of filters: Location, Type, Grade, Facilities.

Currently everything works for me, but each filter works as an OR, so it just adds the results. I want it to be AND (& &) between filter types and every type of the same filter becomes OR. So an example is "Scotland || England && & && 4 stars || 5 stars & & Play & Play Area Area Pool.

I tried to create a list that removed additions that were not equal to the results, but it won't work unless you start from the location.

Here is my current code that works like all OR filters.

// 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();
    }

      

Frontend JS / AJAX (no url change:

$(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);
            }
        });

    }

      

This is a fully working OR filter that updates as you check the box, so feel free to take any ideas from it if you like (if you stumbled upon this while looking at how to do it), but I need it and in between each of the filters, and I'm stuck in a swamp as they would say. I'm also a PHP developer, so if I did anything unnecessary please give some CC's.

Thanks in Advance.

+3


source to share


1 answer


So, with the help of a colleague (.Net Developer) we managed to get it. Any constructive criticism is appreciated, but this is how we work. The only difference in function that changed from my main question is that the last filter for Infrastructures, all objects will be ANDs: example - Scotland || Ireland && Hotels && 4 stars || 2 Star && Pool && Gym && Bar. So for objects, take a close look at how I switch what it does versus the first 3.

Anyone looking at this for guidance, only my backend hasn't changed any foreground changes. There are a few extra things on the front end like a url filter and a few empty variable declarations like page, showAll, etc. and create your input checkboxes with appropriate values ​​and id names as per your 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 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();
        }

      

0


source







All Articles