JavaScript method control.mergeOptions()

Unofficial documentation how to do interesting things and work around problems in ProcessMaker

Moderator: amosbatto

Forum rules
Unofficial documentation for features which have not been tested by Quality Assurance or may change in future versions of ProcessMaker
amosbatto
Posts: 5247
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz

JavaScript method control.mergeOptions()

Unread postby amosbatto » Fri Feb 24, 2017 12:16 pm

The control.mergeOptions() method is used to set the list of additional options in a dropdown box, suggest box, checkgroup or radiogroup. Calling this method will replace any options normally set by the SQL query or pulled from a variable and the new options will be added after the fixed options which are set in the options property of the control. Note that this method is only available in the Backbone object for a control, but not in its jQuery object.

Code: Select all

getFieldById("control-id").mergeOptions(remoteOptions, returnOptions)

Parameters:
- remoteOptions: An array of option objects in the following format:

Code: Select all

[
  { value: "value1",  label: "label1"},
  { value: "value2",  label: "label2"},
  ...
]

Where the value is what is stored when the option is selected and the label is what is display to the user. Note that label can also be named text.
- returnOptions: If set to true, then the complete set of options for the control is returned, including the fixed list of options set in the control's options property.

Return Value:
The current list of variable options for the control are returned in an array of options objects:

Code: Select all

[
  { value: "value1",  label: "label1"},
  { value: "value2",  label: "label2"},
  ...
]

Example 1:
The options "Telephone" and "Fax" are added to the list of fixed options in the "howContact" dropdown box:

Code: Select all

getFieldById("howContact").mergeOptions( [
   {value: "phone", label: "Telephone"},
   {value: "fax", label: "Fax"}
] );

Example 2:
Add a new option to a checkgroup with the ID "selectClient", when the user fills out the "clientName" field and clicks on the "addClient" button. Once the new client is added to the list, it will be deleted from the "clientName" field, so another name can be entered.

Code: Select all

$("#addClient").click(function() {
   var client = $("#clientName").getValue();
   if (client == "") {
      alert("Please enter new client name before adding it to the list of clients.");
   }
   else {
      getFieldById("selectClient").mergeOptions( [{"value": client, "label": client}] );
      $("#clientName").setValue(""); //reset for another client name to be added
   }
] );

Note: The following code can be used if needing to clear the complete list of options in a dropdown box, suggest box, checkgroup or radiogroup, including the fixed options set with the control's options property:

Code: Select all

var fld = getFieldById("control-id");
fld.model.attributes.remoteOptions = [];
fld.model.attributes.optionsSql = [];
fld.model.attributes.options = [];
fld._setOptions([]);
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA-A

amosbatto
Posts: 5247
Joined: Mon Jun 22, 2009 10:28 am
Country: Bolivia
Location: La Paz

Re: JavaScript method control.mergeOptions()

Unread postby amosbatto » Thu Apr 06, 2017 9:30 pm

For an example using mergeOptions, see:
viewtopic.php?f=44&t=710318
Amos B. Batto ♦ ProcessMaker Technical Documentation Writer & Forum Manager ♦ Cofounder of ILLA-A


Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 1 guest