2

I got the following drop-down list - SELECT element - construction in my ZPT template:

<select id="record_selector">
    <option tal:repeat="record view/records" tal:attributes="value record/id">
        <span tal:replace="record/name"></span>
    </option>
</select>

How make it possible to have selected OPTION which value is equal to one from the corresponding view property (i.e. for example, OPTION tag value == view/currentRecordId then make it selected).

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
rook
  • 5,880
  • 4
  • 39
  • 51

2 Answers2

2

Using the sdupton's clue, I got the following solution:

<select id="record_selector">
    <tal:block tal:repeat="record view/records">
        <option tal:condition="python: record['id'] != view.recordId" 
                tal:attributes="value record/id"
                tal:content="record/name">
        </option>
        <option tal:condition="python: record['id'] == view.recordId"
                tal:attributes="value record/id"
                tal:content="record/name"
                selected>
        </option>
    </tal:block>
</select>

TAL conditionals are awesome :)

Community
  • 1
  • 1
rook
  • 5,880
  • 4
  • 39
  • 51
2

I found another solution here: https://old.zope.org/Members/peterbe/DTML2ZPT/index.html#example14

This still works with Zope 5.3 on Python 3.

<select id="record_selector">
    <option
        tal:repeat="record view/records"
        tal:attributes="
            value record/id;
            selected python: record['id'] == view.currentRecordId">
        <span tal:replace="record/name"></span>
    </option>
</select>
Georg Pfolz
  • 1,416
  • 2
  • 12
  • 12