[ZPT] select option question

Wade Leftwich wade@lightlink.com
Fri, 1 Jun 2001 09:37:34 -0400


>is this the best way to do an <select><option selected> tag set? it works
but I
>thought there might
>be a shorter route that I missed.

I do it in a Python script, like this:

## Script (Python) "ischecked"
##parameters=recset,fieldname

dict = {}
for rec in recset:
    dict[fieldname] = 1

def ischecked(item, dict=dict):
    if dict.has_key(item):
        return 'checked'
    else:
        return ''

return ischecked

########


Then your template can say something like this:

<span tal:define="global ischecked
python:container.ischecked(myquerysesults, 'Id')" tal:replace="nothing"/>
<span tal:repeat="row otherqueryresults">
 <input type="checkbox" name="id"
                 tal:define="thisid row/Id"
                 tal:attributes="value thisid;
                                 checked python:ischecked(thisid)" />
</span>

-----------------------------
If you find yourself doing this a lot, you could make a top-level script and
build in some generality, to deal with 'checked' values in querystrings,
lists, tokens, etc.
------------------------------

Wade Leftwich
Ithaca, NY