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


 

How to use the onSelect event of tables in scripting


SAP SCREEN PERSONAS KNOWLEDGE BASE - by Clemens Gantert , Regina Sheynblat , Sebastian Steinhauer

Purpose

This article explains how a table's onSelect event can be used in conjuction with the table's selectedRowsAbsolute property to write scripts that do something specific with the selected row or rows.

Overview

Both table types, GUI table (GuiTableControl) and ALV (GuiGridView) provide an onSelect event. Both table types have a property selectedRowsAbsolute which provides the currently selected rows of the table. These can be used to create scripts that determine the selected row or rows and perform certain actions for each row.

Usage Patterns for the onSelect event and the selectedRowsAbsolute property

It's important to understand that only after the selectedRowsAbsolute property has been updated the onSelect event is called. In other words, the handler script "sees" the currently selected rows in the selectedRowsAbsolute property, even the row whose selection fired the onSelect event itself.

Tables come in single and multi-selection mode. For multi selection tables we recommend not to use the onSelect event but to create a scripting button. The script button's onClick script can read the selectedRowsAbsolute property of the the table, parse it and then perform the required actions. See example 2.

Example 1: onSelect event script for a single-select table

This example is based on report RSDEMO02, but can be used for any table as long as the "source" parameter of the onSelect event is used to access the table (Of course you have to adjust the column name).

var sRawRow = source.selectedRowsAbsolute;
var sRow = sRawRow.replace(/;/g, ''); // removes semicolons, because some GUIs represent a single selected row 4 as "4;" or even as ";4;"
var nRow = Number(sRow); // converts the line number string into a JS number
var departureCity = source.getCellValue(nRow, "IT_SPFLI-CITYFROM");
alert(departureCity);

 

Example 2: Script to process the selected rows of a multi-select table

The script below is also based on report RSDEMO02. You can set the report to turn the table into a multi-select table. We assume that in this case the script is triggered from a script button. This means we don't get the table into the script as 'source' parameter, but we have to access the table via the session.findById() method. A big part of the script is there to parse the selectedRowsAbsolute property. Remember that the property can contain ranges and leading and trailing semicolons. For instance ";2;5;8-10;14;" is a valid value.

The script defines utility function parseSelectedRowsAbsolute() which takes the raw selectedRowsAbsolute property and returns an JS array contain all selected row indices (as JS number, not string).

var table = session.findById("wnd[0]/usr/tblRSDEMO02TC_SPFLI");
var aRows = parseSelectedRowsAbsolute(table.selectedRowsAbsolute);
for(var i=0; i<aRows.length; i++){
		session.utils.log(""+aRows[i]+":"+table.getCellValue(aRows[i], "IT_SPFLI-CITYFROM"));
}
function parseSelectedRowsAbsolute(sRows) {
	var i, j, aRows = [], aTokens, aRange;
	if (sRows ==null || sRows=="") return aRows;
	
	if(sRows.charAt(0)==";") { 
		sRows = sRows.substring(1); // trim leading semicolon
	}
	if(sRows.charAt(sRows.length-1)==";") { 
		sRows = sRows.substring(0, sRows.length-1); // trim trailing semicolon
	}
	aTokens = sRows.split(";");
	for (var i=0; i<aTokens.length; i++){
		if (aTokens[i].indexOf("-")>0) {
			aRange = aTokens[i].split("-");
			for(j=Number(aRange[0]); j<=Number(aRange[1]); j++){
				aRows.push(j);
			}
		}
		else {
			aRows.push(Number(aTokens[i]));
		}
	}
	return aRows;
}

 

 

Related Content

Related Search Terms:

SAP Screen Personas, table, row selection, onSelect

Related SAP Notes/KBAs

Available Events in Scripting