1

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>

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
GertV
  • 831
  • 2
  • 10
  • 23

2 Answers2

1

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Is the syntax ok? It's not `table-${repeat/myindex/index}`? – keul May 10 '13 at 16:03
  • @keul: Ah, yes. This is Chameleon used with *Plone*; default Chameleon uses Python expressions, but the Plone integration switches that to path expressions. – Martijn Pieters May 10 '13 at 16:04
  • So I guess I was on the right track after all, thanks for the explenation, this was more than I could have hoped for. Yet I think I'm using the default chameleon zpt because when I use the code u provided I get the following error: TypeError: unsupported operand type(s) for /: 'RepeatDict' and 'callableint' ==> Expression: "string:table-${repeat/myindex/index}". – GertV May 10 '13 at 16:17
  • @GertV: Rolled back to the previous version, does this one work for you? :-) – Martijn Pieters May 10 '13 at 16:19
  • @GertV: Do you see *any* `id` attribute being generated at all? Do you get an error message instead? If so, what is the error? – Martijn Pieters May 10 '13 at 17:05
  • tal:attributes="id string:table-${python:repeat['myindex'].index}" and tal:attributes="id string:table-${repeat['myindex'].index}" give me : builtins.KeyError KeyError: 'myindex' ==> - Expression: "string:table-${python:repeat['myindex'].index}" I can paste the full traceback here if u want – GertV May 10 '13 at 17:08
  • @GertV: ah, you are not using a `tal:repeat` at all! You are using `tal:define`, nothing is being repeated. – Martijn Pieters May 10 '13 at 17:11
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29747/discussion-between-gertv-and-martijn-pieters) – GertV May 10 '13 at 17:12
  • @GertV: found the solution? Yes, `id string:table-$myindex;` also works, because that is just a variable with the same value. – Martijn Pieters May 11 '13 at 15:35
  • yup found the solution, not proud of what I did because the eroor wasn't realy related to this just some stupid bug. But thanks alot for your help. – GertV May 11 '13 at 15:57
0

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">
Phil LaNasa
  • 2,907
  • 1
  • 25
  • 16