usability1Add my vote for this tag create new tag
, view all tags

How to get a dropdown list with a preselected value

I came across this problem in BetterMore, but it occurs in more places with dropdown lists. Sometimes you want to have one of the items in the list preselected.

In Form.pm there is a routine that finds the selected item in the list and puts a 'selected' after it. Perhaps such a routine could be written for more general use.

My current code in oops.tmpl is:

        <select name="topicparent">
        %TOPICLIST{"<option value=$qname>$name</option>" separator=" "}%

... to generate a list of all topics. Of course I can add a <option value> line before the TOPICLIST where I set some value to be the selected one, but then this value is twice in the list, and I also would like to be it in the proper position, somewhere in the middle of the list perhaps.

I guess to let it find the selected item I would have to provide this, something like:

        <select name="topicparent">
        %TOPICLIST{"<option value=$qname>$name</option>" separator=" " selected="%META{'parent' dontrecurse='on'}%"}%


-- ArthurClemens - 07 Sep 2003

It would be nice if you didn't have to make a special TOPICLIST parameter that would only be used when producing pull-down lists.

Maybe things like %TOPICLIST% and %SEARCH% would benefit from some sort of "code section" that would be evaluated every time through. Picture something like this:

        <select name="topicparent">
        %TOPICLIST{"<option value=$qname $selected>$name</option>" separator=" " eval_code="$selected = $qname eq %METAstuff ? 'selected' : '';"}%

In that example, the variable $selected would be generated at every line and would be empty most cases and would be "selected" during the one default case.

If this would be the only time something like this would be used, maybe something less general, like this:

        <select name="topicparent">
        %TOPICLIST{"<option value=$qname $selected>$name</option>" separator=" " selected="$qname eq %METAstuff ? 'selected' : ''"}%

Here, if a parameter was given that was not understood, a limited amount of code would be interpreted that would create that variable for that particular row.

If this was applied to %SEARCH% as well, it would fix the problem mentioned in ShortUserNameInFormattedSearch. Now, I believe that the problem listed in ShortUserNameInFormattedSearch would be fixed as it already is, but imagine if something more extravagant was needed -- perhaps the first three letters of the WikiName would be requested or something like this. Then a "first_three_letters" could be set equal to a regular expression which grabbed only those first three characters and no else. This is somewhat like the $pattern that already is defined for %SEARCH%.

So anyway, the gist of what I'm saying is that the solution to this problem should be more general and empowering rather than specific to one particular case.

-- TedPavlic - 07 Sep 2003

A poor mans solution is to take the default as the selected one, e.g. the first entry.

        <select name="topicparent">
        <option>%META{"parent" dontrecurse="on"}%</option>
        %TOPICLIST{"<option value=$qname>$name</option>" separator=" "}%

This has just one drawback, the default topic is listed twice; which is not too bad after all since it is the current topic.

Note that the meta data currently does not get rendered in the topic text, a potential fix is HandleMetaTagsInHandleCommonTags.

-- PeterThoeny - 08 Sep 2003

Edit | Attach | Watch | Print version | History: r5 < r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r5 - 2003-09-10 - ArthurClemens
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by Perl Hosted by OICcam.com Ideas, requests, problems regarding TWiki? Send feedback. Ask community in the support forum.
Copyright © 1999-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.