Copied to clipboard

Flag this post as spam?

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


  • Profiterole 232 posts 264 karma points
    Dec 05, 2010 @ 02:05
    Profiterole
    0

    table with for-each but two column

    Hi, I try to do a table that would looks like that :

    <table>
    <tr>
    <td>node 1</td><td>node 2</td>
    </tr>
    <tr>
    <td>node 3</td><td>node 4</td>
    </tr>
    </table>

    but for every childs node under a parent. I don't know how many nodes there's under the parent. I can do that :

    <table>
    <xsl:for-each select="umbraco.library:GetXmlNodeById($source)/node">
      <tr>
    <td>
    <a href="{umbraco.library:NiceUrl(@id)}">Text</a></td>
      </tr>
    </xsl:for-each>
    </table>

    but it will place every node in a row <tr>.

    Can you help me?

    Thank you

  • Tommy Poulsen 514 posts 708 karma points
    Dec 05, 2010 @ 07:20
    Tommy Poulsen
    1

    Hi Profiterole

    You CAN do what you want with tables, e.g. by only selecting evey second in your for-each using something like this:

    <xsl:for-each select="umbraco.library:GetXmlNodeById($source)/node [position() mod 2 = 0]">

    and then choose the next sibling also to show in your 2nd td.

    However I often prefer to skip tables and use DIV's instead - in this case you can easily apply columns without knowing anything about the number.

    Make sure 2 of your DIV's fit in width on your page, make them float left ... and then it's just adding your content in the DIV's.

    >Tommy

     

     

     

     

  • Kim Andersen 1447 posts 2196 karma points MVP
    Dec 05, 2010 @ 11:07
    Kim Andersen
    0

    Hi

    You should be able to achieve this with this small piece of code:

    <xsl:variable name="mod" select="number(2)"></xsl:variable>
    <table>
            <xsl:for-each select="umbraco.library:GetXmlNodeById($source)/node">
                <xsl:if test="position() mod $mod = '1'">
                    <xsl:text disable-output-escaping="yes"><![CDATA[<tr>]]></xsl:text>
                </xsl:if>
                <td>
                    <a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a>
                </td>
                <xsl:if test="position() mod $mod = '0' or position()=last()">
                    <xsl:text disable-output-escaping="yes"><![CDATA[</tr>]]></xsl:text>
                </xsl:if>
            </xsl:for-each>
    </table>

    This would create a tabel and by every second node there would be created a new row in the table. The value in the $mod variable is the number of nodes you want to run through before inserting a new table row, so it's pretty easy to change this in the future if you suddenly want to have three or four nodes on every row.

    /Kim A

  • Profiterole 232 posts 264 karma points
    Dec 06, 2010 @ 02:34
    Profiterole
    0

    Hi, I tried Tommy's way, but I'm not a fan of DIVs. So I used Kim tips.

  • Kim Andersen 1447 posts 2196 karma points MVP
    Dec 06, 2010 @ 11:52
    Kim Andersen
    0

    Great to hear that you can use the code!

    But actually depending on how the content should be shown on the website, I'd go for DIV's as well. But as I say, it depends. If the data you are showing are actually ment to be tabular, then I would go for table as well. But if you are building up the layout or something like that, I would go for the DIV's for sure. It's just more semantic correct :)

    /Kim A

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Dec 06, 2010 @ 12:02
    Chriztian Steinmeier
    1

    Hi all,

    I put a simple example of the clean XSLT approach Tommy describes, up here some time ago:

    http://our.umbraco.org/forum/developers/xslt/11692-Simple-table-row-example-with-XSLT-templates

    Kim's solution works great - it's just not (and he knows that) the cleanest cup of "XSLTea" you can get :-)

    /Chriztian

  • Kim Andersen 1447 posts 2196 karma points MVP
    Dec 06, 2010 @ 14:46
    Kim Andersen
    0

    @Steinmeier: Yeah, I do know that Mr. XSLT :D Hadn't seen the other post, but it's actually a pretty simple and great example. It's bookmarked for later usage.

    @Profiterole: Even though the code I provided works, I'd go for Chriztian's example in the other post the next time I need to do something similar.

    /Kim A

Please Sign in or register to post replies

Write your reply to:

Draft