How to set default for a lookup column using jquery in nintex


Badge +2

I was wondering anyone could shed some lights on this.

 

I want to set default to EN ENGLISH on my list lookup (Language) as below but its editable.

 

 

 

JQuery

NWF$(document).ready(function() {   
 
  //setLookupField(DetailLanguage, 'EN ENGLISH');
}); NWF.FormFiller.Events.RegisterAfterReady(function () {
 
 if (DetailLanguage == "Please select a value..."){
 
 setLookupField(DetailLanguage, 'EN ENGLISH');
    setTimeout(function(){ setLookupField(DetailLanguage, 'EN ENGLISH')},1000);
    setTimeout(function(){ setLookupField(DetailLanguage, 'EN ENGLISH')},2000);
 }else{
  setLookupField(DetailLanguage, 'EN ENGLISH');
    setTimeout(function(){ setLookupField(DetailLanguage, 'EN ENGLISH')},1000);
    setTimeout(function(){ setLookupField(DetailLanguage, 'EN ENGLISH')},2000);
  
 }});
function setLookupField(lookupField, lookupValue) {
 var lookupID = lookupField.substring(0, lookupField.length-4);
 var selectField = NWF$("#" + lookupID + " > option");
 
 for (var i = 0; i < selectField.length; i++){
  var option = selectField;
  if(option.text == lookupValue){
   NWF$("#" + lookupID).val(option.value);
   NWF$("#" + lookupField).val(option.getAttribute("data-nfchoicevalue"));
   NWF$("#" + lookupField).trigger("change");
   NWF$("#" + lookupField).trigger("blur");
   break;
  }
 }
}


13 replies

Userlevel 5
Badge +9

In O365, you can use this script to update a list lookup value:

var valueText = "EN ENGLISH";

var yourControl = NWF$("#"+yourControlId);

var value = yourControl.find("option[title='" + valueText + "']").attr("value");

yourControl.val(value);

yourControl.parent().find("input").val(valueText + "#--#" + value);

NWF$(NWF$(this).parent().find("input")[1]).val(value);

Hope this works

Badge +2

May I know what is [1] referring to?

Badge +2

where do i put this?

Badge +2

Hi Caroline Jung

it's working do you mind explaining these lines?

var valueText = "EN ENGLISH"; // default value which i needed

var yourControl = NWF$("#"+yourControlId); // jquery name of the above item

var value = yourControl.find("option[title='" + valueText + "']").attr("value"); // set var value to en english in option list lookup

yourControl.val(value); //set en english to yourControl

yourControl.parent().find("input").val(valueText + "#--#" + value);  //?

NWF$(NWF$(this).parent().find("input")[1]).val(value);  //? why the [1] ? is it the list item id of the particular item ?

Badge +2

Caroline Jung

Hi,

I dont know why suddenly the default to en english isnt working

its triggering from this line

yourControl.parent().find("input").val(valueText + "#--#" + value);  

Badge +4

If you know the ID of your element, you can do this :

var control = ko.dataFor(NWF$("#" + varMyControlID)[0]);
control.initialValue  = "X";

where X is the ID of your element in the list

Userlevel 5
Badge +9

Hi, 

Sorry for the delay, here are explainations:

var valueText = "EN ENGLISH"; // Setting the title of the option to select in the list lookup field

var yourControl = NWF$("#"+yourControlId); // Getting the list lookup control on the form

var value = yourControl.find("option[title='" + valueText + "']").attr("value"); // Getting the value of the option to select in the list lookup

yourControl.val(value); // Setting the value of the list lookup control

yourControl.parent().find("input").val(valueText + "#--#" + value);  // As setting the value of the list lookup control is not enough to be taken into account by the Nintex Forms, another value has to be set:it's the value of an hidden input which should have a specific format

NWF$(NWF$(this).parent().find("input")[1]).val(value); // This is a mistake: you don't need to add this line

Don't hesitate to ask if it's not clear

Userlevel 5
Badge +9

In the list lookup, does the option with this title still exist "EN ENGLISH"?

It doesn't seem to find the value corresponding to the title "EN ENGLISH".

If you're executing this script when the form loads, I advice you to use this script to be sure that it's executed when the list lookup has been loaded (maybe your script is executing when the list lookup is not loaded and doesn't have any option):

NWF$(document).ready(function() {

setDefaultValue();

});

 

function setDefaultValue(){

var valueText = "EN ENGLISH";

var yourControl = NWF$("#"+yourControlId);

if(yourControl.parent().find(".nf-lookup-loading").length > 0 && yourControl.parent().find(".nf-lookup-loading")[0].style.display != "none") {

setTimeout(setDefaultValue,200);

} else {

var value = yourControl.find("option[title='" + valueText + "']").attr("value");

yourControl.val(value);

yourControl.parent().find("input").val(valueText + "#--#" + value);

}

}

Hope this helps

Userlevel 5
Badge +9

Thanks a lot for this, it works really great

Badge +7

What does ko.dataFor refer to? The knockout.js library? How does this work or is enabled for use with Nintex forms?

Badge +4

Yes it is knockout.js and it is loaded by Nintex Forms

They use it for lookup list field

Badge +7

Is the use of knockout.js documented anywhere within Nintex help and/or encouraged for use? Also, if Nintex decide to change to a different javascript library, then anything using knockout would cease to function. So something to be wary of I guess?

Badge
worked perfectly for me.

Reply