HurkNburkS
HurkNburkS

Reputation: 5510

How to use an XSLT for-each counter

I am trying to keep a count of where I am in my loop so that I can toggle some of my table rows.

This is the counter I am using in my for-each row.

<xsl:variable name="i" select="position()"/>

The problem I am having is that I don't know how to assign i as my rows id, so that each table in my page can open and close its own rows instead of one button turning on and off every row in every table.

This is what my for-each loop looks like

<xsl:for-each select="Talents_Passive">

    <xsl:variable name="i" select="position()"/>

    <div style="font-family:Calibri, Arial; font-size:5pt">
        <xsl:if test="Talent != ''">
            <table border="0" width="550">

                <tr>
                    <td bgcolor="#A0A0A0" width="80%">
                        <a href="#" onClick="SwitchMenu(this, {$i})">Toggle Form?</a> <b id="toggle"><xsl:value-of select="Talent"/></b></td>
                    <td bgcolor="#A0A0A0" width="20%" align="center">
                        <xsl:value-of select="Cost"/><xsl:text>  -  </xsl:text><xsl:value-of select="Type"/></td>
                </tr>

                <xsl:if test="Prerequisite != ''">
                    <tr>
                        <td colspan="2" bgcolor="#C0C0C0"><b>Prerequisite: </b><xsl:value-of select="Prerequisite"/></td>
                    </tr>
                </xsl:if>

                <tr>
                    <td colspan="2" bgcolor="#C0C0C0"> 
                        <xsl:if test="Action != ''">
                            <b>Action: </b><xsl:value-of select="Action"/>
                        </xsl:if>
                        <xsl:if test="Range != ''">
                            <xsl:text>    </xsl:text> <b>Range: </b><xsl:value-of select="Range"/> 
                        </xsl:if>
                        <xsl:if test="Cost != ''">
                            <xsl:text>    </xsl:text> <b>Cost: </b><xsl:value-of select="Cost"/>
                        </xsl:if>
                    </td>
                </tr>   

                <xsl:if test="Action != ''">
                    <tr>
                        <!-- I would like to open and hide this row and give control to each table that is created in this for-each loop -->
                        <td colspan="2" bgcolor="#E0E0E0" id="{$i}" style="display:none;"><b>Action: </b><xsl:value-of select="Action"/></td>
                    </tr>
                </xsl:if>


            </table>
        </xsl:if>
    </div>

</xsl:for-each>

So I would like to know how to get the current value of i and assign that to my rows <td id="i"> or something along those lines.

Upvotes: 0

Views: 2433

Answers (1)

hr_117
hr_117

Reputation: 9627

Looks like you should move the id and the style="display:none;" from td to tr.

<tr id="sh_{$i}" style="display:none;">

<!-- I would like to open and hide this row and give controll to each table that is creted in this for each loop -->
                    <td colspan="2" bgcolor="#E0E0E0" ><b>Action: </b><xsl:value-of select="Action"/></td>
   </tr>

Your javascript function has than to look for id's like "sh_1".

Upvotes: 1

Related Questions