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)
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
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:- 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
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:
Its wierd giving you Razor help...I feel kind of dirty
Hi!
Try this for the array initiator
var classes = new [] {"none", "single", "double", "triple"};
then classes[n] will work
Didn't even notice the duff array declarition. Good spot Jonas!
Ah - thanks Jonas!
That was my problem - my (Java|Coffee)Script-esque pseudo-code didn't work... :-)
/Chriztian
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:
is working on a reply...