Copied to clipboard

Flag this post as spam?

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


  • Peter Cort Larsen 421 posts 1038 karma points
    Jul 06, 2012 @ 15:08
    Peter Cort Larsen
    0

    IngroupsOf

    Hi,

     

    I am new to razor. I hope someone can help me.

    From a property of type 'Multi-Node Tree Picker' i select variuos nodes.

    I want to list with 3 items on each row.

    I tried using mod, with no luck. Then i saw InGroupsOf, which seems perfect.
    But i cant get it to work. I have this:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.MacroEngines;
    @{


    if (Model.HasValue("selectedNews"))
    {

    foreach (var group in Model.selectedNews.InGroupsOf(3))
    {
    @:<div class="parent">
    foreach (var item in group)
    {
    //get 3 nodes, then create new parent div
    <div><a href="#">@item.Name</a></div>
    }
    @:<div>

    }
    }
    }
  • Douglas Ludlow 210 posts 366 karma points
    Jul 06, 2012 @ 15:57
    Douglas Ludlow
    2

    That's because the Multi-Node Tree Picker stores only the Id's of the selected nodes. Depending on how you're storing the nodes, XML of CSV, you could do one of the following:

    CSV:

    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    @if (Model.HasValue("selectedNodes")
    {
    string[] ids = Model.selectedNews.Split(',');
    dynamic nodes = new DynamicNodeList();

    foreach (string id in ids)
    {
    var node = Library.NodeById(id);
    if (node.Id != 0)
    {
    // If the node exists
    nodes.Add(node);
    }
    }

    foreach (var group in nodes.InGroupsOf(3))
    {
    <div class="parent">
    @foreach (var node in group)
    {
    <div><a href="@node.Url">@node.Name</a></div>
    }
    </div>
    }
    }

    XML:

    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    @if (Model.HasValue("selectedNodes")
    {
    dynamic nodes = new DynamicNodeList();
    foreach (var item in Model.selectedNews)
    {
    var node = Library.NodeById(item.InnerText);
    if (node.Id != 0)
    {
    // If the node exists
    nodes.Add(node);
    }
    }

    foreach (var group in nodes.InGroupsOf(3))
    {
    <div class="parent">
    @foreach (var node in group)
    {
    <div><a href="@node.Url">@node.Name</a></div>
    }
    </div>
    }
    }

     

  • Peter Cort Larsen 421 posts 1038 karma points
    Jul 06, 2012 @ 16:08
    Peter Cort Larsen
    0

    Thanks, it works. Have a great weekend and thank you again.

Please Sign in or register to post replies

Write your reply to:

Draft