Copied to clipboard

Flag this post as spam?

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


  • William Corry 34 posts 171 karma points
    Jan 11, 2015 @ 21:21
    William Corry
    0

    Trying to use a count of children pages to determine the number of columns

    I am using bootstrap 3 and have an element that fits into columns but the number of cselements can vary. I have used .count and some if statements to vary the col width class based upon the number of items but wonder if there is a cleaner way of doing this:

    <div class="row">
    @foreach (var icon in icons)
    {
    <div class="col-md-@if (item.Children.Count() == 3) {<text>4</text>}@if (item.Children.Count() == 4) {<text>3</text>}@if (item.Children.Count() == 2) {<text>6</text>}">
    @{
    <div class="icon-header @if (!item.HasValue("bodyText")) {<text>padtop-75</text>}">
    <div class="icon-effect @Umbraco.GetPreValueAsString(icon.BackgroundColor)">
    <i class="effect @icon.Icon fa-5x"></i>
    </div>
    <header class="process-header">
    <h3>@icon.Header</h3>
    @Html.Raw(icon.BodyText)
    </header>
    </div>
    }
    </div>

    }
    </div>

    I know I could use Umbraco 7.2 new grid tool for this but have not got time to delve into the finer details of how it all works etc.

  • Cimplex 113 posts 576 karma points
    Jan 11, 2015 @ 22:33
    Cimplex
    0
    var count = item.Children.Count();
    var cols = 6;
    switch (count) {
    case 3:
    cols = 4;
    break;
    case 4:
    cols = 3;
    break;
    }
    <div class="col-md-@cols">
  • William Corry 34 posts 171 karma points
    Jan 12, 2015 @ 00:01
    William Corry
    0

    Thanks but unfortuantely this does not seem to work :(

    I get this error:

    A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Jan 12, 2015 @ 01:34
    Bjarne Fyrstenborg
    100

    Hi William

    Try declare you "count" variable as a int:

    int count = item.Children.Count();

    int cols = 6; switch (count) { case 3: cols = 4; break; case 4: cols = 3; break; default: cols = 6; break; }


    /Bjarne

  • William Corry 34 posts 171 karma points
    Jan 12, 2015 @ 03:43
    William Corry
    0

    Worked a treat thanks

Please Sign in or register to post replies

Write your reply to:

Draft