Mar 20, 2017 10:59 AM
2 Posts
topic has been resolvedResolved

Update field depending on a combobox value

  • Category: Dojo and Client Side JavaScript
  • Platform: Windows
  • Release: 9.0.1
  • Role: Administrator,Developer
  • Tags:
  • Replies: 2

Hello

I'm new to XPages and I've been struggling with this problem:

I have a combobox "combobox1" and a field "field1" in my XPage and I want to update "fiedl1" everytime the value of "combobox1" changes. The value for field1 needs to be calculated searching for a document in a view using the "combobox1" value as a key to obtain the value from "TargetField" in the document found, if any. The field "field1" should remain editable, so the user can change the value at will.

My idea was to use onchange event of "combobox1" to search for the given value in the search view and obtain the "TargetField", but it seems that the client side javascript is calculated upon page load and then my code doesn't trhow any result.

My code in the onchange event from "combobox1" field:

var idlist = '#{id:combobox1}';
var idfield = '#{id:field1}'; 
var list = XSP.getElementById(idlist).value;
var data = '#{javascript:@DbLookup(@DbName(),"(searchView)","' + list + '","TargetField")}';
XSP.getElementById(idfield).value = data;

The result is that the "data" variable receives a value equal to "", so "field1" receives always an update to "" value. I've checked that "list" variable receives the correct value, but I suspect that variable "data" is calculated BEFORE variable "list" (upon page load).

Any suggestions of how to do what I need?

Apr 4, 2017 4:26 PM
94 Posts
The solution is to use serverside javascript not client side

var val = getComponent("combobox1").getValue();

var tview = database.getView("(searchView)")

var vdoc = tview.getDocumentByKey(val,true);

if(vdoc!=null){

    getComponent("field1").setValue(vdoc.getItemValueString("TargetField"));

    //set the in memory doc field, too

    document1.replaceItemValue("field1",vdoc.getItemValueString("TargetField"))

}

 

Be sure to perform a partial update either the field itself or the panel containing it.

Apr 21, 2017 5:40 AM
2 Posts
Solved

Thanks for the info, Bob.

I've resolved this issue using the approach you suggested. ;)