Copied to clipboard

Flag this post as spam?

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


  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Nov 01, 2013 @ 13:44
    Chriztian Steinmeier
    0

    How to add a class from a list based on count of items

    I'm building a very simple navigation where I need to add a a static class and a computed class to the <ol> element - here's my pseudo code, which obviously doesn't quite work:

    // Possible class names:
    var classes = ["none", "single", "double", "triple"];
    
    // Take the one we need
    var classNames = "nav " + classes[Model.Pages.Count];
    
    <ol class="@classNames">
        <li>One</li>
        <li>Two</li>
        etc.
    </ol>
    

    - but hopefully you get that I'd like to get the class name based on the number of items in the list (Model.Pages.Count is from Contour and refers to the number of steps in the Form, but the technique should apply to many other scenarios as well)

    /Chriztian

  • Peter Duncanson 430 posts 1360 karma points c-trib
    Nov 01, 2013 @ 14:00
    Peter Duncanson
    0

    Hi Chriztian,

    Couple of ways to do this, you could just append the number to the end of a class "node1, node2, etc." or like you are doing us a look up:

    @{ // Enter a script block
    // Assume usual boiler plate stuff is at the top as looks like you are editing one of the existing Contour forms
    // Possible class names:

    var classes =["none","single","double","triple"];
    // Take the one we need
    var classNames ="nav "+ classes[Model.Pages.Count];
    }
    <ol class="@classNames">
       
    <li>One</li>
        <li>Two</
    li>
        etc
    .
    </ol>

    OR you could...

    <ol class="nav @classes[Model.Pages.Count]"> <li>One</li> <li>Two</li> etc. </ol>

    Its wierd giving you Razor help...I feel kind of dirty

  • Jonas Eriksson 930 posts 1825 karma points
    Nov 01, 2013 @ 14:04
    Jonas Eriksson
    101

    Hi!

    Try this for the array initiator

    var classes = new [] {"none", "single", "double", "triple"};

    then classes[n] will work

     

  • Peter Duncanson 430 posts 1360 karma points c-trib
    Nov 01, 2013 @ 14:09
    Peter Duncanson
    0

    Didn't even notice the duff array declarition. Good spot Jonas!

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Nov 01, 2013 @ 14:11
    Chriztian Steinmeier
    0

    Ah - thanks Jonas!

    That was my problem - my (Java|Coffee)Script-esque pseudo-code didn't work... :-)

    /Chriztian

  • Jonas Eriksson 930 posts 1825 karma points
    Nov 01, 2013 @ 14:28
    Jonas Eriksson
    1

    Glad to help :) yea, it's easy to forget the syntax diff there. Actually sometimes I find it conveniant to use the json parser to build c#-objects:

    @{
    var json_complex = Json.Decode(@"{ item: 'yaah', myarray:['one','two','three'] }"); } <p>@json_complex.item</p> <p>@json_complex.myarray[2]</p>
Please Sign in or register to post replies

Write your reply to:

Draft