cancel
Showing results for 
Search instead for 
Did you mean: 

(SOLVED) Setting selected value in dropdown from model

crafter
Champ in-the-making
Champ in-the-making
I am populating a form in a dialogue from values from a database query.

One of the fields is a country field. I now want to set the value of a dropdown with the selected value.

However, my code has no result. Even alerts placed in the code is not running.

The code works in a plain html file, so it should work here as well. Or do I have to do this another way?

/* myform_post.ftl */

new Alfresco.dashlet.MyApp("${args.htmlid}").setOptions(
{
      "location"            : "${location!''}",
      "country"            : "${country!''}",
   }).setMessages(
      ${messages}
   );
  
//]]>


function setSelectedIndex(s, v) {
   
   for ( var i = 0; i < s.options.length; i++ ) {
      if ( s.options[i].text === v ) {
         s.options[i].selected = true;
/*         s.value = v;
  *         s.selectedIndex = i;
  */
         return;
      }
   }
}
setSelectedIndex(document.getElementById("${args.htmlid}-country"), "${country}");
                             :
                             :
<select name="country" id="${args.htmlid}-country" size="1"  >
     <option value="Albania">Albania</option>
     <option value="Algeria">Algeria</option>
     <option value="American Samoa">American Samoa</option>
     <option value="Andorra">Andorra</option>
     <option value="Angola">Angola</option>
                :
</select

1 REPLY 1

crafter
Champ in-the-making
Champ in-the-making
I figured that the code belongs in the Dashlet controller javascript, while I was adding it to the model javascript.

The code below show the placement of the code
        
         if (!this.postDialog)
         {
            this.postDialog = new Alfresco.module.SimpleDialog(this.id + "-configDialog").setOptions(
            {
               templateUrl: Alfresco.constants.URL_SERVICECONTEXT + "modules/mydashlet/mydashlet-post?mode=edit;

               doSetupFormsValidation:
               {
                  fn: function TravelLog_doSetupForm_callback(form)
                  {
                    
                     setSelectedIndex = function (s, v) {
                         
                         for ( var i = 0; i < s.options.length; i++ ) {
                            if ( s.options[i].text === v ) {
                               s.options[i].selected = true;
                              return;
                            }
                         }
                      }
                    
                        var element = new YAHOO.util.Element('country_value');
                        var country_text = element.get('value');

                      setSelectedIndex(document.getElementById(this.postDialog.id + "-country"), country_text);


In the view, I added a hidden field country_value to hold a copy of the dropdown selected value.

  <input type="hidden" name="country_value" id="country_value" value="${country}" />

   <select name="country" id="${args.htmlid}-country" size="1" >
          <option value="Afghanistan">Afghanistan</option>
     <option value="Albania">Albania</option>
     <option value="Algeria">Algeria</option>
      <option value="American Samoa">American Samoa</option>