How to select option in HTML select using TAL in ZPT?

1.3k views Asked by At

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).

2

There are 2 answers

0
rook On

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 :)

0
Georg Pfolz On

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>