Default value DropDown

  • 14 November 2016
  • 3 replies
  • 20 views

Badge +3

When I open a new form, I want to set the default value of my dropdown to the first value of a list (indexid = 1) and it should also be saved ( as long as the user doesnt change the value) when the user clicks on the "Save" button. I tried it with code below, but nothing happened Nintex Form version 2.9.3.1.

dlookupID is the JavaScript variable name of my dropdown.

NWF.FormFiller.Events.RegisterAfterReady(function () {

setLookupFields();

});

 

function setLookupFields() {

         // Getting view model of my lookup field dropdown control

   vmForApplication = ko.dataFor(NWF$("#" + dlookupID)[0]);

   // Setting value to drop down

      vmForApplication.initialValue  = "1";

}


3 replies

Badge +1

Since we  upgraded to Forms version 2.10 (from an older version like 2.5) we have had a lot of trouble with this. The following code works for me but it isn't as clean as I would like it to be so if anyone has a suggestion for improvement, JS is not my specialty! There are two issues in my opinion:

  1. I'm not able to catch the change event of the child control as I should be. I have to trigger the change from the parent manually
  2. When the change happens it is too fast so I have to put in a delay loop to wait for the DDL to load completely. Seems like there should be a better way.

 

As I said, it works, it is just a little ugly. Note  that if you are not dealing with the parent/child relationship you can hardcode  default value really easily:

NWF$(document).ready(function() {
 NWF$("#" + myddl).val('3');
});

 

Here is the parent/child one:

 

NWF$(document).ready(function() {
 NWF$("#" + ddlParent).change(function () {       
  NWF$("#" + ddlChild).change();        
 }); 
   
  
 NWF$("#" + ddlChild).change(function () { 
  var ddlChildId = ddlChild.substring(0, ddlChild.length -4); 
  var len= NWF$("#" + ddlChildId ).find("option").length;      
  if(len>=2) {

 return delay(1000).then(function() {
   NWF$("#"      + ddlChildId).prop('selectedIndex',1);
   NWF$("input#" + ddlChildId).val(NWF$("#" + ddlChildId).find("option")[1]); 

});
  }
  else {
       while(i<10 && len==1) {
       i++;        
          return delay(1000).then(function() {
           len= NWF$("#" + ddlChildId ).find("option").length;
           if(len>=2) {
            NWF$("#"      + ddlChildId).prop('selectedIndex',1);
            NWF$("input#" + ddlChildId).val(NWF$("#" + ddlChildId).find("option")[1]);      
           }
          });        
   }
  }            
 });

 function delay(t) {
  return new Promise(function(resolve) {
   setTimeout(resolve, t)
  });
 }
});

Badge +9

Hi Ann,

Your script worked, but does not write the value of the child to the XML in a repeating section. Adding .change() fixed it.

i.e.NWF$("#" + ddlChildId).prop('selectedIndex',1).change();

The other problem is the script only works on the first item of a repeating section. How can we get it to work items that are subsequently added?

Badge +5
Thanks Furstlars, you saved my day. I have challenge to save the data to SharePoint column and now getting with your advise "Change()".
My drop down is in the repeating section, we have to reference with CSS Class instead of JavaScript ID, the ID keeps changes due to adding new rows. Define this name ".ddlChildId" in CSS class on that control.
NWF$(".ddlChildId").prop('selectedIndex',1).change();
It works for me. Thank you once again!

Reply