Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Amir Khan 1282 posts 2739 karma points
    Feb 18, 2014 @ 22:16
    Amir Khan
    0

    Paging combined with filtering

    Does anyone have a good method for combining pagination with filtering? I have this (simplified) pagination which I've always used, but I can't seem to figure out how to filter the nodes before paginating in the same forech loop?

    @using System;
    @using System.IO;
    @using System.Xml.XPath;
    @using System.Xml;
    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
    //Request Query String
    string filingGroup = Request.QueryString["filingGroup"];

    }


    @{
    var startNodeID = 2628;


    }

    @if (startNodeID != null)
    {
    if (@filingGroup != null) {
    <h1>@filingGroup</h1>
    }
    @* Get the start node as a dynamic node *@
    var startNode = Library.NodeById(startNodeID);

    //Pagination
    var pageSize = 20;
    var pages = 1; int.TryParse(Request.QueryString["page"], out pages);
    var items = startNode.Children.Where("Visible");
    var totalPages = (int)Math.Ceiling((double)items.Count() / (double)pageSize);

    if (pages > totalPages)
    {
    pages = totalPages;
    }
    else if (pages < 1)
    {
    pages = 1;
    }

    if (startNode.Children.Where("Visible").Any())
    {
    <ul>
    foreach (var item in items.Skip((pages -1)* pageSize).Take(pageSize))
    {
    <li class="filingRow filing clearfix">
    @item.Name
    </li>
    }
    </ul>
    }

    // Pagination nav
    if(totalPages >1)
    {
    <div class="pagination">
    <ul>
    @if (pages > 1)
    {
    <li><a href="?page=@(pages-1)">Prev</a></li>
    }
    @for (int p = 1; p < totalPages + 1; p++)
    {
    var active = (p == pages) ? "active" : string.Empty;
    <li class="@active">
    <a href="?page=@p">@p</a>
    </li>
    }
    @if (pages < items.Count())
    {
    <li><a href="?page=@(pages+1)">Next</a></li>
    }
    </ul>
    </div>

    }

    }
  • Benas Brazdziunas 34 posts 155 karma points c-trib
    Feb 19, 2014 @ 00:25
    Benas Brazdziunas
    0
    var items = startNode.Children.Where("Visible" && filter);

    I think you just need filter before you calculate page count

  • Amir Khan 1282 posts 2739 karma points
    Feb 19, 2014 @ 00:29
    Amir Khan
    0

    Hi Benas, how do I set a filter on nodes that haven't been retreived yet? How would I grab the property to filter on?

  • Benas Brazdziunas 34 posts 155 karma points c-trib
    Feb 19, 2014 @ 00:43
    Benas Brazdziunas
    0
    var items = startNode.Children.Where(x => x.GetProperty("propertyAliasName").Value == "filter" && "Visible");
  • Amir Khan 1282 posts 2739 karma points
    Feb 19, 2014 @ 00:50
    Amir Khan
    0

    Hi Benas, that gives me the following:

    error CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

  • Benas Brazdziunas 34 posts 155 karma points c-trib
    Feb 19, 2014 @ 01:02
    Benas Brazdziunas
    0
    var items = startNode.Children.Where(x => x.GetPropertyValue("propertyAliasName","")=="filter"&&x.IsVisible());

    Examples can be found

    http://our.umbraco.org/documentation/Reference/Mvc/querying

  • Amir Khan 1282 posts 2739 karma points
    Feb 19, 2014 @ 01:34
    Amir Khan
    0

    Still the same error.

  • Benas Brazdziunas 34 posts 155 karma points c-trib
    Feb 19, 2014 @ 10:25
    Benas Brazdziunas
    0
    @using umbraco.MacroEngines
    @{
        //Request Query String
        string filingGroup = Request.QueryString["filingGroup"];
        var startNodeID = 11797;
        if (startNodeID != null)
        {
            if (filingGroup != null)
            {
                <h1>@filingGroup</h1>
            }
            var root = new DynamicNode(startNodeID);       
            //Pagination
            var pageSize = 20;
            var pages = 1;
            int.TryParse(Request.QueryString["page"], out pages);
            var items = root.Children.Where(c=>c.Visible && c.GetPropertyValue("propertyAlias") == "filter");
            var totalPages = (int)Math.Ceiling((double)items.Count() / (double)pageSize);       
        }
    }

    Code snippet works to me

Please Sign in or register to post replies

Write your reply to:

Draft