We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20532
    • 14 Posts
    Is it possible to append an initial "blank" value to an @SELECT Binding (or similar)? My problem is that there is currently no way for users in the manager to select nothing once there is a selection from the generated list.

    Here’s the code pasted into the "Input Option Values":

    @EVAL return substr($modx->runSnippet('Ditto', array("parents"=>"984","depth"=>"1","display"=>"all","showInMenuOnly"=>"0","sortBy"=>"pagetitle","sortDir"=>"ASC", "tpl"=>"@CODE ||[+pagetitle+]==[+id+]")), 2);


    Here’s the generated list in the TV:

    <select id="tvcontainsPatterns[]" size="8" onchange="documentDirty=true;" multiple="multiple" name="tvcontainsPatterns[]">
    <option value="1069">Ads — Monetization</option>
    <option value="1084">Article Feature Section — Multi-Link</option>
    <option value="1085">Article Feature Section — Single-Link</option>
    <option value="878">Banner — Godzillatron</option>
    <option value="1066">Banner — Moodboard</option>
    <option value="1048">Banner — Pokemontron</option>
    <option value="1079">Bio Layout — Additional Links</option>
    ...
    </select>
    


    And, this is what I’m looking for (note the second line):

    <select id="tvcontainsPatterns[]" size="8" onchange="documentDirty=true;" multiple="multiple" name="tvcontainsPatterns[]">
    <option value=""></option>
    <option value="1069">Ads — Monetization</option>
    <option value="1084">Article Feature Section — Multi-Link</option>
    <option value="1085">Article Feature Section — Single-Link</option>
    <option value="878">Banner — Godzillatron</option>
    <option value="1066">Banner — Moodboard</option>
    <option value="1048">Banner — Pokemontron</option>
    <option value="1079">Bio Layout — Additional Links</option>
    ...
    </select>
    


    I’m sure I could could easily hack it w/ a javascript "clear" button that would deselect all selected values, but I’d like a more elegant solution if possible.

    Thanks!
      • 20532
      • 14 Posts
      This is a jQuery solution I wrote that works really well. I placed this in the header include for the manager index page - of course, you will have to edit the selectors according to your values.

      $("select#tvcontainsPatterns\\[\\]").after("<br /><a href='#' id='clear_tvcontainsPatterns[]'>Clear selections</a>");
      $("a#clear_tvcontainsPatterns\\[\\]").click(function() {
      	$("select#tvcontainsPatterns\\[\\]").children("option:selected").removeAttr("selected");
      	return false;
      });
      


      It searches for your specified select html and appends a link below it that states "Clear selections". When this link is clicked, it removes the selected attribute from the specifed select’s options.
      • I realize this is an older post, but is this still the best method for doing this?
          • 26931
          • 2,314 Posts
          for an initial blank first option you can use this (e.g.):
          @SELECT '' AS pagetitle FROM modx_site_content UNION SELECT pagetitle FROM modx_site_content WHERE parent=4
          

          (found it somewhere on the forum)
          • Thanks for the reply and example (I couldn’t find that anywhere on the forums!). However, I’m getting a really strange error when I use this code (modified for my site):

            @SELECT '' AS pagetitle FROM vid_site_content UNION SELECT pagetitle FROM vid_site_content WHERE parent='5' ORDER BY pagetitle ASC;


            The error looks like this:

            Execution of a query to the database failed - Illegal mix of collations (utf8_general_ci,COERCIBLE) and (latin1_swedish_ci,IMPLICIT) for operation 'UNION' »
                  SQL: SELECT '' AS pagetitle FROM vid_site_content UNION SELECT pagetitle FROM vid_site_content WHERE parent='5' ORDER BY pagetitle ASC;


            Any ideas on this? What I don’t understand is, I’m selecting from the same table twice. How could there be two different collations?
            • Another thought: would it be easier to just use an @EVAL binding for this instead of a @SELECT? I’ve tried but didn’t any better luck. I’m still a little unsure of how the @bindings in a TV’s Input Option Values field translates it into options in a TV drop-down.
                • 26931
                • 2,314 Posts
                Any ideas on this? What I don’t understand is, I’m selecting from the same table twice. How could there be two different collations?
                hmm, found this: http://modxcms.com/forums/index.php/topic,39581.msg239358.html#msg239358
                • Thanks. That might be what I end up doing. I guess I just don’t understand encodings well enough. I’m not sure why it matters if I’m using UNION on the same table. How can the same table have two collations?
                  • Figured it out! I just set the collations in the first SELECT statement and it eliminated the "mixed collations" error:

                    @SELECT _latin1 '' COLLATE latin1_swedish_ci AS pagetitle, _latin1 '' COLLATE latin1_swedish_ci AS id FROM vid_site_content UNION SELECT pagetitle, id FROM vid_site_content WHERE parent='5' AND published='1' ORDER BY pagetitle ASC;


                    Again, I’m not a pro at this, so let me know if there’s a reason not to do it this way.
                      • 36722
                      • 101 Posts
                      Here is what I'm using for a listbox selector to typeahead search resources.

                      @SELECT '' AS `name` ,'' AS `id` FROM `[[+PREFIX]]site_content`  UNION SELECT `pagetitle` AS `name`,`id` FROM `[[+PREFIX]]site_content` WHERE `published` = 1 AND `deleted` = 0
                        Shawn Himmelberger
                        Himmelberger Design
                        https://himmdesign.com/services/website-development/modx"" target="_blank" rel="nofollow"> MODx Web Design | https://himmdesign.com/services/website-development/modx"" target="_blank" rel="nofollow"> MODx Web Development