Copied to clipboard

Flag this post as spam?

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


  • Nicky Christensen 76 posts 166 karma points
    Jan 30, 2012 @ 11:26
    Nicky Christensen
    0

    Create a new div after looping X

    Hey guys..

    Is it possible with razor to create a new div after X items is outputted? 

    I would like an ouput like:

    <div class="col">
        4 items here
    </div>
    <!-- New col -->
    <div class="col">
        The next four items
    </div>

    The script i have so far is just looping all items:

    <div class="col">
      @foreach (var item in @Model.NodeById(1275).Children.Where("Visible"))
      {
        <section class="person" itemscope itemtype="http://www.schema.org/Person">
          <img src="http://placehold.it/100x100" alt="Ansat" itemprop="image" />

          <h2>
            @item.personName
          </h2>
          
          <itemprop="jobTitle">@item.personTitle</p>
          <p><href="mailto:@item.email" itemprop="email">@item.email</a></p>
          <p>
            @item.personText
          </p>
        </section>
      }
    </div>

  • Rodion Novoselov 694 posts 859 karma points
    Jan 30, 2012 @ 12:04
    Rodion Novoselov
    0

    Here's a sample how it can be done (not pretending to be the only way of course):

    @{
      var N = 4;
      for(var children = Model.NodeById(1275).Children.Where("Visible");
          children.Any();
          children = children.Skip(N))
      {
        <ul>
         @foreach(var child in children.Take(N))
         {
            <li>@child.Name</li>
         }
        </ul>
      }
    }

     

  • Nicky Christensen 76 posts 166 karma points
    Jan 30, 2012 @ 12:16
    Nicky Christensen
    0

    Great, thx that worket... 

    I was out in somekind of a solution with Modulus, but couldn't get it to work.. However, this works great

  • Rodion Novoselov 694 posts 859 karma points
    Jan 30, 2012 @ 12:25
    Rodion Novoselov
    0

    Hold on. I've just accidentally realised that there seems to be a ready out-of-the-box solution :-)

    var groups = Model.NodeById(1275).Children.InGroupsOf(4);

    (haven't yet tried on my own but it looks worth of it).

  • Nicky Christensen 76 posts 166 karma points
    Jan 30, 2012 @ 12:31
    Nicky Christensen
    0

    Hmm, ok - I'll try to look into that solution - Would be nice with a neat and easy solution like that! :) 

  • Rodion Novoselov 694 posts 859 karma points
    Jan 30, 2012 @ 12:31
    Rodion Novoselov
    0

    I've tried it - it works perfectly.

  • 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