Thursday, July 23, 2015

Getting and setting Apex page item values

The Apex JavaScript API has some very convenient functions to access the values of page items.
For example, if you wante to access the contents of a text field with JavaScript, would would need to reference it something like this:
$x("P2_TEXT_FIELD").value;
If the item you want to reference is a display only item, then the syntax changes:
x = $("#P2_DISPLAY_ONLY").text();
If you need to set the values of these items, the you need to use the varying syntax as well:
$x("P2_TEXT_FIELD").value = "Hello World!";
or
$("#P2_DISPLAY_ONLY").text("Hello World!");
Dealing with these various syntax constructs can be confusing. Fortunately the Apex JavaScript API makes this process much easier. To get page item values simply use $v(“<item_name>”):
x = $v("P2_TEXT_FIELD");
y = $v("P2_DISPLAY_ONLY");
To set the item values use:
$s("P2_TEXT_FIELD","Hello World!");
$s("P2_DISPLAY_ONLY","Hello World!");
See an example on my demo page.
The $v2() is handy if you need to access multiple items in an array. For example multiple selections from check boxes or from a shuttle control can be fetched as an array and handled that way in JavaScript:
myArr = $v2("P2_SHUTTLE_CONTROL");
for (idx=0; idx<myArr.length; idx++) {
  //do something with myArr[idx];
}

No comments:

Post a Comment