What is multiselect?
Control with ability to select multiple items. In IBM BPM we have option to make a dropdown as multiselect but it does not look good as we will have rows of values with checkbox.
With multiselect control, it is easy to select the values and also widget looks good with scrolling option if values are more.
So below are few steps to create a custom multiselect control in IBM BPM.
Step 1: Create a coach view and name it as multiselect. Drag an vertical section to layout area and create an output text for label and a custom HTML control as shown below.
Step 2: Create a variable to map the selected values as ‘Business Data’.
Step 3: Create a configuration option to map ajax service to get the values dynamically as shown below.
Step 4: In the custom HTML space paste the below code, where dom node with id ‘multiselect’ will have the multiselect control.
<select id=”multiselect”></select>
<style>
#dynamic{
height: 130px;
width: 180px;
}
</style>
here specifying height and the width is important.
Step 5: Create required AMD dependencies as shown below.
Step 6: Write the below code in ‘load’ event handler.
//Input for Ajax service. Here we don’t need it.
var inputText = {text: “Dummy”};
//Get the URL to call the ajax
var ajaxUrl = this.context.options.dataService.url;
var customers = [];
var _self = this;
//Prepare AJAX call
var xhrArgs = {
//Set the URL
url: ajaxUrl,
content: JSON.stringify(inputText),
//Specify the result type expected
handleAs: “json”,
//Headers are required when we run this in Mozilla or IE. Could not find why.
headers: {
“Content-Type”: “application/json”,
“Accept”: “application/json”,
},
load: function(response) {
//Copy the result to an object
customers = response.data.data.results.items;
//Get the dom using the id
var sel = dom.byId(‘multiselect’);
var n = 0;
//Loop for values and append it to the node inner HTML
for(var i=0;i<customers.length;i++){
//Creating element of type option as needed by any select controls
var c = win.doc.createElement(‘option’);
c.innerHTML = customers[i];
c.value = n++;
sel.appendChild(c);
}
//Creating an object of type DOJO Multiselect and adding values present in ‘sel’
var myMultiSelect = new MultiSelect({ name: ‘dynamic’ }, sel);
var submitButton = dojo.query(“.submitBtn”)[0];
//To get the selected values on click of some button
var myObject = {
id: “myObject”,
onClick: function(evt){
var selected = [];
selectedCustomers = [];
selected = myMultiSelect.get(‘value’);
//Get all the selected values and bind it to the object
for(var i=0;i<selected.length;i++){
selectedCustomers[i] = customers[selected[i]];
}
selectedCustomersString = selectedCustomers.toString();
_self.context.binding.set(“value”, selectedCustomersString);
},
};
//On click of the button, call onClick funtion to get the values to object binded.
dojo.connect(submitButton, “onclick”, myObject.onClick);
},
error: function(e) {
console.log(“service call failed: ” + ajaxUrl + e );
}
}
//Call the Ajax
xhr.post(xhrArgs);
Done. ! 🙂 Drag this coach view just like an UI element in the coach or other coach view to use.