Copied to clipboard

Flag this post as spam?

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


  • Paul Stewart 50 posts 71 karma points
    May 09, 2012 @ 10:17
    Paul Stewart
    0

    Sorting with the embedded content tool

    Is there any way I can sort / randomise the order of using multiple embedded content tools?

    Credit to Gilad for helping me on the previous post. 

     

    @using umbraco.MacroEngines;
    @{
     foreach(var testamonial in Model.Children)
     {
            var itemsList = testamonial.customerTestimonials;  
                <ul>
                  @foreach(var item in itemsList)
                  {     
                    var customerItem = item.customer.InnerText;
                    var testimonItem = item.testimonial.InnerText;
                   
                    <li>
                       <p>@testimonItem</p>
                       <p class="quoter">@customerItem</p>
                    </li>
                  }
                </ul>
     }
    }

    I want to randomise the order really, cause its adding in sequence, I have list one, then list two

    Effective This is what it looks like the now in the CMS

    List 1 Items
    - Item 1.1
    - Item 1.2
    - Item 1.3

    List 2 Items
    - Item 2.1
    - Item 2.2

    List 3 Items
    - Item 3.1
    - Item 3.2

    and this razor script would render the list like this. 

    - Item 1.1
    - Item 1.2
    - Item 1.3
    - Item 2.1
    - Item 2.2
    - Item 3.1
    - Item 3.2

    But Ideally I would like it to do this. (pure random)

    - Item 2.1
    - Item 1.1
    - Item 3.2
    - Item 3.1
    - Item 1.3
    - Item 2.2
    - Item 1.2

    Any ideas? 

     

  • gilad 185 posts 425 karma points
    May 09, 2012 @ 14:38
    gilad
    0

    Try this :

     

    @using umbraco.MacroEngines;
    @{
    List<dynamic> forRandom = new List<dynamic>();
    foreach(var testamonial inModel.Children)
    {
    var itemsList = testamonial.customerTestimonials;
    foreach(var item in itemsList)
    {
    forRandom.Add(item);
    }
    }
    Random rnd = new Random();
    forRandom = forRandom.OrderBy(p => rnd.Next()).ToList();

     
    <ul>
       
    @foreach(var item in forRandom)
       
    {    
           
    var customerItem = item.customer.InnerText;
           
    var testimonItem = item.testimonial.InnerText;
           
    <li>
               
    <p>@testimonItem</p>
                <p class="quoter">@customerItem</
    p>
           
    </li>
        }
    </
    ul>
    }

     

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies