[thelist] [asp] improving a generalized options function

Warden, Matt mwarden at mattwarden.com
Wed Jul 11 10:52:42 CDT 2001


Thelist,

I'm sifting through my code library and trying to remember the code I put on
my mental "need to improve when I have time" list. This is one of them. The
function below (slightly modified for readability) is a generic handler for
option-type elements in HTML. This includes <option> of the select element
and multiple-select elements, checkboxes, radio. It builds them from a
two-dimensional array (it's being used with Recordset.GetRows()) of options
and labels.

My specific concern is the series of replace()s. When I wrote this, I made a
mental note that I would change the function somehow to not use
p_sOptionTemplate and the replace()s. However, just like then, I can't think
of another way to do this without un-generalizing the function. What's nice
about the current function is that I can add a function like
buildCheckboxColumn() that calls getOptions() with a template like:

<tr><td><input type="checkbox" class="yo" value="[TEM:OPTION]"
[SELECTED]></td><td> [TEM:LABEL]</td></tr>

...or any other variation of structure by simply writing a wrapper function
with a new option template. I'd like to keep this if I can. If anyone has
any ideas, my ears are open. I'm just looking to improve upon this function
and I can't figure a way to do it.

' optionTemplate looks like:
' <option value="[TEM:OPTION]" [SELECTED]>[TEM:LABEL]</option>"
' or
' <input type="checkbox" name="elementname" value="[TEM:OPTION]" [SELECTED]>
[TEM:LABEL]
FUNCTION handleOptions(p_sSelected, p_sOptionTemplate, a_options,
p_sSelectedOptionAttrName)

 p_sSelected = lcase(trim(p_sSelected))

 iArrayBound = ubound(a_options,2)

 sOutput = ""

 for i = 0 to iArrayBound
  sOption = ""
  sLabel = ""
  sSelected = ""

  sOption = trim(a_options(0,i))
  sLabel = a_options(1,i)

  if p_sSelected=lcase(sOption) then
   sSelected = " " & p_sSelectedOptionAttrName & "=""true"""
  end if

  ' <one-line>
  sOutput = sOutput &
        replace(
           replace(
              replace(p_sOptionTemplate, "[TEM:LABEL]", sLabel)
            , "[TEM:OPTION]", sOption)
        , " [SELECTED]", sSelected)
  ' </one-line>

 next

 a_options = Empty

 handleOptions = sOutput

END FUNCTION


Any ideas welcome.


<tip>
Creating a code library in your spare time is a good idea. It speeds up your
development time and you can charge your clients for the time (or at least a
portion of it) you took to develop that code. Code once, charge many
times... very similar to stand-alone software development.
</tip>


Thanks,



--
mattwarden
mattwarden.com





More information about the thelist mailing list