We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23849
    • 223 Posts
    Hi All,

    I'm trying to setup a simple TV listbox of the multi-select variety. The options are to output as <option> html tags. I've tried setting the "Input Option Values" like so:

    sourdough==<option value="sourdough">sourdough</option>||baguette==<option value="baguette">baguette</option>||mad river grain==<option value="mad river grain">mad river grain</option>


    And then set the output type as "Delimiter" with a value of "||".

    The list is correctly showing in the admin, but when I make a selection it spits out some weird stuff (screenshot attached):

    sourdough" name="tv6[]"/>


    I've tried reading over the documentation for this style TV, but I'm not finding an answer. I thought perhaps I needed to set it up as an HTML output instead of Delimeter, but was unsuccessful with that as well.

    Any help on this would be amazing!
      Nick Hoag
      Creative Partner
      The FutureForward

      http://thefutureforward.com
      • 3109 ☆ A M B ☆
      • 894 Posts
      Did you try putting the HTML values in chunks and make the listbox items == your chunk values?

      I've had bad experiences trying to do what your doing with listbox TV using chunks as the values has worked for me in the past.

      Hope that helps.
        Benjamin Marte
        Interactive Media Developer
        Follow Me on Twitter | Visit my site | Learn MODX
        • 3109 ☆ A M B ☆
        • 894 Posts
        Did you try putting the HTML values in chunks and make the listbox items == your chunk values?

        I've had bad experiences trying to do what your doing with listbox TV using chunks as the values has worked for me in the past.

        Hope that helps.
          Benjamin Marte
          Interactive Media Developer
          Follow Me on Twitter | Visit my site | Learn MODX
          • 23849
          • 223 Posts
          Hi @benmarte,

          Thanks for the reply. So your'e saying put each <option> tag into it's own chunk? That would be around 50 chunks for me, as I have quite a few of these. Hopefully there's a different way to do it contained within the TV itself, as that seems like a lot of unnecessary stuff floating around in the manager/db.
            Nick Hoag
            Creative Partner
            The FutureForward

            http://thefutureforward.com
            • 3749
            • 24,544 Posts
            Try just using this for the Input Option Values and setting the output type to default:

            sourdough||baguette||mad river grain
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 23849
              • 223 Posts
              Hi @BobRay,

              I tried your suggestion and that just outputs the following:

              <select id="bread" name="bread">sourdoughbaguettemad river grain</select>


              And here's the chunk that this TV is being called in (the chunk is being used as a TPL in a getResources call):

              <select id="bread" name="bread">[[+item_bread]]</select>
                Nick Hoag
                Creative Partner
                The FutureForward

                http://thefutureforward.com
                • 23849
                • 223 Posts
                So I figured out one option...I can set the "Output Type" to "HTML Tag", and then set "Tag Name" to "option".

                Then if I set the values as you have Bob, it works.

                Now I need to figure out how to set a value for each option tag...
                  Nick Hoag
                  Creative Partner
                  The FutureForward

                  http://thefutureforward.com
                  • 3749
                  • 24,544 Posts
                  Duh, I was thinking of the display in the Manager.

                  There's probably an easier way, but a snippet would probably do it (using my suggested setting above).

                  [[!DisplayOptions? &tvName=`SomeTv` &fieldName=`bread` &size = `6`]]


                  /*DisplayOptions snippet */
                  
                  $tv = $modx->getObject('modTemplateVar', array('name' => $scriptProperties['tvName']));
                  
                  if ($tv) {  // make sure the TV exists
                      $output = '<select name = "' . $scriptProperties['fieldName'] . '" MULTIPLE SIZE="' . $scriptProperties['size'] . '">'  . "\n";
                      $options = explode('||', $tv->get('default_text'));
                      $selected = explode ('||', $tv->getValue($modx->resource->get('id)));
                      foreach($options as $option) {
                          $output .= '    <option value = "' . $option;
                          if (in_array($option, $selected)) {
                               $output .= ' SELECTED="SELECTED" ';
                          }
                          $output .= ' >' . $option . "\n"; 
                  
                      }
                      $output .= '</select
                      return $output;
                  }
                  


                  BTW, it's possible that FormIt would do all of this for you.
                  [ed. note: BobRay last edited this post 15 years, 1 month ago.]
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 23849
                    • 223 Posts
                    Bob,

                    Thank-you so much for this. It's a huge help.

                    I'm having a couple of issues with the code. Here's the snippet, slightly revised due to some syntax errors in the original:

                    <?php
                    	/*
                    		DisplayRobustOptionsTV snippet - https://forums.modx.com/thread?thread=70082#dis-post-392829
                    		Purpose - When creating a Template Variable that outputs "<option>" HTML tags (using the "Output Type" of "HTML" and the "Tag" set to "option"), you specify the input options as 		option1||option2||option3. This works, but the problem is that this only outputs a very simple <option> tag, with no value attribute. 
                    		
                    		This snippet enables you to add additional attributes like "value" to the <option> tag.
                    		
                    		Snippet Call - [[!DisplayRobustOptionsTV? &tvName=`SomeTv` &fieldName=`bread` &size = `6`]]
                    		
                    	*/
                    	 
                    	$tv = $modx->getObject('modTemplateVar', array('name' => $scriptProperties['tvName']));
                    	 
                    	if ($tv) {  // make sure the TV exists
                    		$output = '<select name="' . $scriptProperties['fieldName'] . '" size="' . $scriptProperties['size'] . '">'  . "\n";
                    		$options = explode('||', $tv->get('default_text'));
                    		$selected = explode ('||', $tv->getValue($modx->resource->get('id')));
                    		foreach($options as $option) {
                    			$output .= '<option value= "' . $option . '"';
                    			if (in_array($option, $selected)) {
                    				 $output .= ' SELECTED="SELECTED" ';
                    			}
                    			$output .= ' >' . $option . "\n"; 
                    	 
                    		}
                    		$output .= '</select>';
                    		return $output;
                    	}
                    ?>


                    This is outputting the following:

                    <select name="bread" size="6">
                        <option value="" selected="SELECTED">
                    </option></select>


                    For some reason the value/text are not being rendered. The TV I'm trying to render is set as a listbox multi-select with the following input option values:

                    "sourdough||baguette||mad river grain||bagel||whole wheat||light rye"

                    Here's the page it's appearing on - http://future.thefutureforward.com/~moon/order-online.html

                    Also, not to throw a ton at you at once, but I'm actually trying to eventually arrive at something like this for output:

                    <select id="center" name="center">
                        <option value="grilled chicken breast">grilled chicken breast</option>
                        <option value="grilled salmon{p+2}">grilled salmon - add 2.00</option>
                        <option value="roast turkey{p+1}">roast turkey - add 1.00</option>
                        <option value="roast beef{p+2}">roast beef - add 2.00</option>
                        ...
                    </select>


                    This is a Foxycart integration that mimics others that I've done in the past. The need for a solid way of producing configurable product options through the resource editor itself has always been a big challenge. The key elements for a Foxycart product option are simply the name of the option and then (generally) the price and weight difference if one exists. I'm hoping one day to be able to have a single TV that can handle editing these values by the user within the resource editor (rather than having to go into the template variable itself to edit). Also, having the ability to click a "+" to create another <option> within the resource editor would be amazing.

                    I've arrived at something that's getting closer to this by following along with Shaun's "Custom TV Output Type" tutorial - http://rtfm.modx.com/display/revolution20/Adding+a+Custom+TV+Output+Type, But it's still a pretty far way off of what I want to achieve.

                    Not sure why I told you all of that, but just figured I would share!

                    Thanks again Bob.
                      Nick Hoag
                      Creative Partner
                      The FutureForward

                      http://thefutureforward.com