On Tue, 26 Dec 2006 12:52:11 +0530, Vamsee Kanakala vamlists@gmail.com wrote:
I have something like this, and the <option> tag needs to be selected when id matches the value of the parameter called location-id.
<p><label for="location-id">Location: </label> <select id="location-id" name="location-id"> <option value="">Choose a location</option> <!-- tmpl_loop rows --> <!-- tmpl_loop cols --> <option value="<!-- tmpl_var id -->"> <!-- tmpl_var name --></option> <!-- /tmpl_loop --> <!-- /tmpl_loop --> </select></p>
So I followed Igor's advice (thank you!) and changed the <option> tag above to this:
<option <!-- tmpl_var selected --> value="<!-- tmpl_var id -->"> <!-- tmpl_var name --></option>
Supported by this code:
(rows (loop for locationl in locations collect (list :cols (loop for location in locationl if (and (required-exist-p location-id) (equal (parse-integer location-id :junk-allowed t) (id location))) collect (list :id (id location) :name (name location) :selected "selected") else collect (list :id (id location) :name (name location)))))
It's working now. Do let me know if there are better ways to achieve this.
That's fine. I'd probably do it a little bit different (see below, untested), but that's just a matter of taste:
<option <!-- tmpl_if selected -->selected <!-- /tmpl_if -->value="<!-- tmpl_var id -->"><!-- tmpl_var name --></option>
(loop for location in locationl collect (list :id (id location) :name (name location) :selected (and (required-exist-p location-id) (equal (parse-integer location-id :junk-allowed t) (id location)))))