Announcement

Collapse
No announcement yet.

Cool functions: Auto "select" form element with correct "option" selected

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cool functions: Auto "select" form element with correct "option" selected

    Most miva script coders have shortcut functions, and here's one of my more useful ones.

    You know when you are coding forms for editing data and they contain select/option drop downs? And sometimes they have lots of options and you need to make sure the right one is selected when populating the form with data?

    For me this always meant a lot of MvIF statements in the options part of the select element. Not anymore.

    Code:
    <MvFUNCTION NAME = "select" PARAMETERS = "class,default,name,condition,options" STANDARDOUTPUTLEVEL="compresswhitespace,text,html" >
    
      <MvASSIGN NAME = "l.options" VALUE = "{ miva_array_deserialize( l.options ) }" />
    
      <select class="{ l.class } name="{ l.name }">
        <option value=""><MvEVAL EXPR = "{ l.default }"></option>
          <MvFOREACH ITERATOR = "l.option" INDEX = "l.pos" ARRAY = "l.options">
            <MvIF EXPR = "{ gettoken(l.option,'-',1) EQ l.condition }">
              <option value="{ gettoken(l.option,'-',1) }" selected><MvEVAL EXPR = "{ gettoken(l.option,'-',2) }"></option>
            <MvELSE>
              <option value="{ gettoken(l.option,'-',1) }"><MvEVAL EXPR = "{ gettoken(l.option,'-',2) }"></option>
            </MvIF>
          </MvFOREACH>
        </select>
    </MvFUNCTION>
    The class parameter sets the "select" form element class.

    The default parameter sets the no-value option (like, "Select one...")

    The name parameter is the name of the variable for the "select" form element.

    The condition parameter is the what has to match in order to make an option selected.

    The options parameter is a list of option values and labels, set up like:

    Code:
    <MvASSIGN NAME = "l.options" VALUE = "{ 'value1-Option Label One,value2-Option Label Two,value3-Option Label Three,value4-Option Label Four' }" />
    Automatically output the select/options tag without any condition matching (for a blank form):

    Code:
    <MvASSIGN NAME = "l.ok" VALUE = "{ select(l.null,'Select one...','selected_value',l.null,l.options) }" />
    If you had a form for editing and populating it with data, then you want to match the condition in the select element. In this example, it might be 'value1' that needs to be matched:

    Code:
    <MvASSIGN NAME = "l.condition" VALUE = "{ 'value1' }" />
    <MvASSIGN NAME = "l.ok" VALUE = "{ select(l.null,'Select one...','selected_value',l.condition,l.options) }" />
    When the select/option form field is rendered it would automatically have the 'value1' option selected.

    You could add "class" and "default option" parameters to set the select element class and the "null" option ("select one...") to really flesh it out.
    Last edited by Scot - ScotsScripts.com; 11-21-19, 09:43 AM.
    M.A.D.* since 1997

    http://www.scotsscripts.com

    *miva application developers
Working...
X

This website uses cookies to identify visitors, track visitors to our website, store login session information and to remember your user preferences. By continuing to use this site you agree to our use of cookies. Learn More.

This website uses cookies. By continuing to use this site you agree to our use of cookies. Learn More.

Accept