Question

I'm using the tal:repeat statement to generate tables inside another table. Sadly I do not know how to give each table an unique id when generated. How can I do this?

I'm trying to use:

tal:attributes="id myindex" 

and

tal:attributes="id string:${myindex}"

But I can't get it to work.

Example:

<table id="tableIngrepen" class="table">
<thead class="header">
<tr>
    <th>Header1</th>
    <th tal:repeat="procedur_headers Procedur_Headers" tal:content="procedur_headers" > </th>
</tr>
</thead>
<tr tal:repeat="diagnoses Diagnoses"> 
    <div tal:define="myindex python:repeat['diagnoses'].index">
        <td ><input type='text' id="dz_code" readonly></input></td> <!-- onfocus="rijencolom($(this).parent().children().index($(this)),$(this).parent().parent().children().index($(this).parent()))" -->
        <td colspan="5">
            <table tal:attributes="id myindex"  class="table table-hover" style="border-style:none">
                <thead class="header">
                    <tr>
                        <th tal:repeat="procedur_headers Procedur_Headers" tal:content="procedur_headers" style="display:none"> </th> <!-- style="display:none"-->
                    </tr>
                </thead>
                <tr tal:repeat="list_procedur List_Procedur[myindex]">
                    <td><input type='text' ></input></td>
                    </tr>
                <tr>
                    <td><input type='text' ></input></td>
                    <td ><input type='text'></input></td>
                    <td><input type='text' ></input></td>
                    <td><input type='text' ></input></td>
                </tr>
            </table>
        </td>
    </div>
</tr>

Was it helpful?

Solution

You can use the TAL repeat variable that each repeat loop creates:

<table tal:attributes="id string:table-${python:repeat['diagnoses'].index}" 
       class="table table-hover" style="border-style:none">

or using a path expression:

<table tal:attributes="id string:table-${path:repeat/diagnoses/index}" 
       class="table table-hover" style="border-style:none">

Depending on how Chameleon was configured you can omit either the path: or the python: prefix; whichever one is the default expression type. Pagetemplates default to path: expressions, Chameleon to python: but normally the Plone integration switches that to path: to keep compatibility.

The repeat mapping contains a special object for each loop variable; your loop uses the name myindex so there is a repeat['diagnoses'] object that contains things like the loop index, the parity of the iteration (odd or even) and even roman numeral versions of the loop counter.

OTHER TIPS

If Chameleon ZPT doesn't like the string syntax, you could go with the python expression syntax:

<table tal:attributes="id python:'table-' + repeat['diagnoses'].number" 
       class="table table-hover" style="border-style:none">

Or if you want it to be zero-based:

<table tal:attributes="id python:'table-' + repeat['diagnoses'].index" 
       class="table table-hover" style="border-style:none">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top