Copied to clipboard

Flag this post as spam?

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


  • Siw Ørnhaug 130 posts 109 karma points
    Sep 10, 2010 @ 21:04
    Siw Ørnhaug
    0

    Loop through data fields in node and format output as zebra striped table

    I want to output all fields on a node that have content in a table with alternate row background color. I don't see how I can use a for-each loop on data fields within a single node. Originally I used a session variable for this but suddenly that didn't work anymore - I have trouble getting a session variable value stored. So how can I simulate a counter value to use a mod 2 on so I can control the background color, anyone?

  • Sascha Wolter 615 posts 1101 karma points
    Sep 10, 2010 @ 23:46
    Sascha Wolter
    0

    Hi Siw,

    If you are using xsl you can just use the position() in the for-each loop like this:

    Another way might be to use CSS to style up your table: http://www.w3.org/Style/Examples/007/evenodd.

    Hope that helps,

    Sascha

     

  • Siw Ørnhaug 130 posts 109 karma points
    Sep 13, 2010 @ 09:42
    Siw Ørnhaug
    0

    Hi Sascha,

    You seem to have a suggestion for xslt that did not show up in your response.

    But I tried the CSS tip and it works wondrously - in all browsers except IE 7 and 8.

    I cannot believe they don't support that CSS markup at all, do you know if there is some trick to it in IE?
    If this actually doesn't work in IE I need another solution.

    P.S. I always keep my css markup in a separate stylesheet. 

  • Sascha Wolter 615 posts 1101 karma points
    Sep 13, 2010 @ 11:17
    Sascha Wolter
    0

    Hi Siw,

    ups, just found out here (http://msdn.microsoft.com/en-us/library/cc351024(VS.85).aspx) that nth-child won't work with IE, bugger, didn't know that...

    You could try it out with jQuery: http://blog.darkhax.com/2010/01/17/making-nth-child-work-everywhere.

    Very odd my xslt code didn't come through, anyway here it is:

    <xsl:for-each select="$currentPage/*">
      <li>
        <xsl:if test="position() mod 2 = 1">
          <!-- apply some class here -->
        </xsl:if>
        <!-- display the item -->
      </li>
    </xsl:if>

    Hope that helps,

    Sascha

  • Siw Ørnhaug 130 posts 109 karma points
    Sep 13, 2010 @ 11:24
    Siw Ørnhaug
    0

    Hi Sascha,

    I really appreciate fast responses :-)

    Your xslt is exactly what I tried to use, but you can't do a for-each on data fields within a node, can you?

    In my case a document type called Destination has like 15 string fields. These are to be outputted in a simple table, one field per row, if they contain text.

    I have seen another solution wich uses an index parameter to a secondary xsl:template in order to achieve counter behaviour. But I have to treat my fields in if-tags, so I would need a separate secondary template for each field in order to make it work.

    I'll have a look at your Jquery example.

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Sep 13, 2010 @ 11:44
    Morten Bock
    0

    Why would you not be able to do a for-each on data nodes (I'm assuming that you are using umbraco 4.0.x and not 4.5)?

    You should be able to get just the fields with data in them like this:

    <xsl:for-each select="$currentPage/data [. != '']">

    Then the position() mod 2 would work on the fileds with data.

  • Siw Ørnhaug 130 posts 109 karma points
    Sep 13, 2010 @ 12:43
    Siw Ørnhaug
    0

    Dear Morten,

    My xsl knowledge is sadly faulty. Your suggestion is perfect. Thanks a bunch and have a nice day! 

    EDIT:

    Mortens suggestion solved my problem.

     

Please Sign in or register to post replies

Write your reply to:

Draft