Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 939 posts 2574 karma points
    Jun 26, 2014 @ 16:34
    Claushingebjerg
    0

    Get position of element in a "node-set"

    How do i get the position of a node in a node-set?

    Equal to position in xslt?

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jun 26, 2014 @ 16:45
    Jan Skovgaard
    0

    Hi Claus

    What are you trying to do and do you have some code you could share?

    /Jan

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jun 26, 2014 @ 18:31
    Dennis Aaen
    0

    Hi Claus,

    To get the position of a node in a node-set, in Razor you can use the .Index() method. The index is zero based.

    <a href="@item.Url" class="[email protected]()">@item.Name</a>

    So with is you will get items, in e.g a foreach, where each item gets the class name item-0, item-1 and so on, like you know it in XSLT.

    In razor you have some simple IsHelpers to get e.g the first item and the last item, and isEven isOdd an so on.

    Here is a list of the IsHelpers:

    .IsFirst(valueIfTrue[,valueIfFalse])
    .IsNotFirst(valueIfTrue[,valueIfFalse])
    .IsLast(valueIfTrue[,valueIfFalse])
    .IsNotLast(valueIfTrue[,valueIfFalse])
    .IsPostion(int,valueIfTrue[,valueIfFalse])
    .IsNotPosition(int,valueIfTrue[,valueIfFalse])
    .IsModZero(int,valueIfTrue[,valueIfFalse])
    .IsNotModZero(int,valueIfTrue[,valueIfFalse])
    .IsEven(valueIfTrue[,valueIfFalse])
    .IsOdd(valueIfTrue[,valueIfFalse])
    .IsEqual(DynamicPublishedContent,valueIfTrue[,valueIfFalse])
    .IsNotEqual(DynamicPublishedContent,valueIfTrue[,valueIfFalse])
    .IsDescendant(DynamicPublishedContent,valueIfTrue[,valueIfFalse])
    .IsDescendantOrSelf(DynamicPublishedContent,valueIfTrue[,valueIfFalse])
    .IsAncestor(DynamicPublishedContent,valueIfTrue[,valueIfFalse])
    .IsAncestorOrSelf(DynamicPublishedContent,valueIfTrue[,valueIfFalse])

    Hope this helps,

    /Dennis

  • Claushingebjerg 939 posts 2574 karma points
    Jun 26, 2014 @ 20:55
    Claushingebjerg
    0

    Ok, so here's some code

    @{
        var navigate = Model.Content.GetPropertyValue<ArchetypeModel>("slider");
             foreach (var fieldset in navigate)
            {
                if (fieldset.Equals(navigate.First()))
                {
                    <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
                }
                else
                {
                    <li data-target="#carousel-example-generic" data-slide-to="@fieldset.Index()"></li> 
                }
            }
    }   

    So i want the "data-slide-to" to use the "Index()". But the above code, using Dennis' example doesnt work...

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jun 26, 2014 @ 21:05
    Dennis Aaen
    0

    Hi Claus,

    What if you use simple .Position() method does this do any difference.

    @{
       
    var navigate = Model.Content.GetPropertyValue<ArchetypeModel>("slider");
             
    foreach(var fieldset in navigate)
           
    {
               
    if(fieldset.Equals(navigate.First()))
               
    {
                   
    <li data-target="#carousel-example-generic" data-slide-to="0"class="active"></li>
               
    }
               
    else
               
    {
                   
    <li data-target="#carousel-example-generic" data-slide-to="@fieldset.Position()"></li>
               
    }
           
    }
    }  

    Hope so.

    /Dennis

  • Claushingebjerg 939 posts 2574 karma points
    Jun 26, 2014 @ 21:33
    Claushingebjerg
    0

    Nope, returns error

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jun 26, 2014 @ 23:23
    Dennis Aaen
    100

    Hi Claus,

    I was thinking of could you do it this way?

    @{
        var navigate = Model.Content.GetPropertyValue<ArchetypeModel>("slider");
        int i = 1;
           
        foreach(var fieldset in navigate){
              
                if(fieldset.Equals(navigate.First()))
                {
                    <li data-target="#carousel-example-generic" data-slide-to="0"class="active"></li>
                }
                else
                {
                    <li data-target="#carousel-example-generic" data-slide-to="@i"></li>
                }
                  i++;
            }
    }  

    If not then I am almost empty for ideas on how you try to get it work.

    /Dennis

  • Claushingebjerg 939 posts 2574 karma points
    Jun 27, 2014 @ 07:29
    Claushingebjerg
    0

    Succes! Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft