Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

SAP Screen Personas


 

Change the value set of a combo box through scripting.


SAP SCREEN PERSONAS KNOWLEDGE BASE - by Tamas Hoznek , Clemens Gantert , Regina Sheynblat

Purpose

A much asked for feature since the introduction of SAP Screen Personas 3.0 was the ability to change the value set of a combo box dynamically through scripting. With SP03, this feature has become a reality.

Overview

 A how to guide is provided explaining the differences between standard and custom combo boxes. For the typical use cases, sample scripts are provided.

 

Changing the value set of a combo box

If you take a look at type GuiComboBox in the API documentation, you will notice that the property "entries" was changed from read-only to "readable and writeable". The other change is that the type of the property was changed from GuiCollection to GuiComboBoxEntryCollection.

The idea is to read the "entries" property, then manipulate the retrieved GuiComboBoxEntryCollection object and, finally, to reassign the manipulated collection back to the combo box's entries property.

Simple Script removing all entries from the combo box
var oCombo = session.findById("<combo id>");
var oEntries =oCombo.entries;
oEntries.removeAll();
oCombo.entries = oEntries;


It's important to understand that by reading the entries property you receive a "detached" collection and that only the assigning of a collection to the entries property of the combo box actually changes the value set of the combo box.


There are differences between what can be done with a custom (flavor-created) combo box and a standard (DYNPRO) combo box. Just like in the flavor editor, standard combo boxes can only be changed in the following way:

  1. Existing entries can be removed or hidden (use the removeAt() or removeAll() methods)
  2. Existing entries can be reordered (use removeAt() in conjuction with add() and addAt() )
  3. The label texts of existing entries can be changed by changing the "value" property of a GuiComboBoxEntry object. Note that in the flavor editor UI, every combo box entry you create for it has a "Key" and a "Label", in scripting however a GuiComboBoxEntry object has a "key" property and a "value" property, that latter corresponding to the label.


Custom combo boxes can be filled with new entries via the factory method createComboBoxEntry(String key, String value) that the GuiComboBoxEntryCollection object provides. Note that you can add entries created even to standard combo boxes provided that the key existed before (and there are no duplicate entries with the same key).

Script that creates new entries
var oCombo = session.findById("<combo id>");
var oEntries =oCombo.entries;
oEntries.removeAll();
var oEntry1 = oEntries.createComboBoxEntry("LH", "Lufthansa");
oEntries.add(oEntry1);
var oEntry2 = oEntries.createComboBoxEntry("AA", "American Airlines");
oEntries.add(oEntry2);
oCombo.entries = oEntries; // this will work even for a standard combo box if the original had entries with keys "LH" and "AA"
Script to modify label texts
var oCombo = session.findById("<combo id>");
var oEntries =oCombo.entries;
for(var i=0; i<oEntries.length; i++){
	oEntries.elementAt(i).value= oEntries.elementAt(i).key+" - " + oEntries.elementAt(i).value;
}
oCombo.entries = oEntries;

 

Every time you read the "entries" property you get a new GuiComboBoxEntryCollection. So, reading "entries" is like invoking a factory method for GuiComboBoxEntryCollection objects. This can be used for reordering. See script below.

Script that reverses the order of all current entries
var oEntries1 =oCombo.entries;
var oEntries2 =oCombo.entries;
oEntries2.removeAll();
var oEntry;
while(oEntries1.length>0){
 //remove from the end
 oEntry = oEntries1.removeAt(oEntries1.length-1);
 // append to the collection 
 oEntries2.add(oEntry); 
}
oCombo.entries = oEntries2;

 

In this last example we make use of the Array.sort() method with a comparison method to sort the value set in descending order of their key.

Sorting by key (descending)
var i, aEntries = [];
var oCombo = session.findById("<combo id>");
var oEntries =oCombo.entries;

// move entries into an array
for(i=0; i<oEntries.length; i++){
 aEntries.push(oEntries.elementAt(i));
}

// sort the array
aEntries = aEntries.sort( 
 function(a,b) { // compare method sorting by descending key
 return b.key.localeCompare(a.key);
 }
);

// move entries from array back into collection
oEntries.removeAll();
for(i=0; i<aEntries.length; i++){
 aEntries[i].value = aEntries[i].key +" - " + aEntries[i].value; // change the label to better illustrate order
 oEntries.add(aEntries[i]);
}

oCombo.entries = oEntries;

 

Related Content

Related Search Terms:

SAP Screen Personas, Scripting, Combo Box

Related SAP Notes/KBAs

N/A