Copied to clipboard

Flag this post as spam?

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


  • Kadir 30 posts 80 karma points
    Oct 09, 2013 @ 22:15
    Kadir
    0

    Group Sorting Alphabetically - Razor

    Is it possible to list subnodes in alphabetical groups in razor? e.g.

    • A
      Adidas, Armani
    • B
      Bvlgari, Burton 

    etc.. I have found this topic http://our.umbraco.org/forum/developers/xslt/17314-How-to-Group-Sort-nodes-alphabetically where it was possible to do in XSLT.

    I would really appreciate any guidance for Razor example. I have both umbraco version 4 and 6 projects running.

  • Arie 224 posts 675 karma points
    Oct 09, 2013 @ 22:26
    Arie
    1

    You can use "OrderBy" to sort alphabetically in Razor. To create sub-groups you can use nested "foreach" loops, the first one to grab the main group and the inner loop to get the children.

  • Dallas 133 posts 405 karma points
    Oct 10, 2013 @ 02:26
    Dallas
    0

    Here is an example that we are using in mvc razor. It is using OrderBy and GroupBy to group by the first letter of each node.

    var groupedByLetters = Model.Content.Children
                            .OrderBy(x => x.GetPropertyValue<string>("brand"))
                            .GroupBy(s => s.GetProperty("brand").Value.ToString().Substring(0,1), s => s,  
                                     (key, g) => new { letter = key, brands = g.ToList() });
    

    and then loop over the groups

    @foreach (var letter in groupedByLetters)
    {
        <div>@letter.letter</div>
    
           @foreach (var brand in letter.brands)
             {   
                <div>@brand.Name</div>  
             }
    }
    

    Dallas

  • Kadir 30 posts 80 karma points
    Oct 10, 2013 @ 22:55
    Kadir
    0

    Hey Dallas,

    thank you very much for the code you provided. I tried to run it, i am receiving this error message: 
    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

    I don't know if I am doing something wrong or not. I will try to investigate it.

    Best,

  • Dallas 133 posts 405 karma points
    Oct 11, 2013 @ 00:55
    Dallas
    0

    That example is using the strongly typed IPublishedContent (@Model.Content) in an MVC View. Are you using the DynamicPublishedContent? or razor in a MacroScript?

    Dallas

  • Kadir 30 posts 80 karma points
    Oct 12, 2013 @ 12:51
    Kadir
    0

    Yes, I am trying to run razor in a Macroscript.

  • Troels Larsen 75 posts 280 karma points
    Oct 12, 2013 @ 16:17
    Troels Larsen
    1

    if i recall correctly the DynamicNodeList has and property called "InGroupsOf" that might take and lamda espression as a string! (yes as a string)  u can mostlily do it with "regular" lamda but in razormacros it's syntax is ..... u have to passe the expression as a string to the method.  

    something like myList.Where("NodeTypeAlias == \"something\" and Field == \"Myfield \""); ugly yes :) 

    I will try to dig up some examples from old projects. 

  • Dallas 133 posts 405 karma points
    Oct 12, 2013 @ 16:45
    Dallas
    101

    This forum post discusses the razor GroupBy and InGroupsOf functions

    to get this to work you could order the nodes ( with OrderBy ) and then use a second loop to format the grouping.

    //current letter value to compare
    string letter=String.Empty;
    
    var nodes =Model.Descendants().Where("Visible").OrderBy("lastName");
    
    @foreach (var item in nodes)
    {
       //if the letter has changed display header
      if(!letter.Equals(item.Name.Substring(0,1))
      {
          letter =item.Name.Substring(0,1);
         <div class="heading">@letter</div>
      }
    
        <div>@item.Name</div>
    
    }
    

    Dallas

  • Kadir 30 posts 80 karma points
    Oct 12, 2013 @ 19:19
    Kadir
    0

    thanks Dallas,

    I will check it out.

Please Sign in or register to post replies

Write your reply to:

Draft