How to set loop values to lists` elements

While reading a List we may require to set one or more values of the list’s elements. This tutorial will show you how to use javascript to set one or more values of the list`s elements.

Scenario:
We will create a list of employees with the first names Mary and Jeeny. We will use the for loop inside the javascript action to set their last name as Alison. We`ll use a label inside a repeater to display the data from the employee list.

Steps

  1. Open a new application.
  2. Create a Type employee with 2 fields : firstname and lastname

image

  1. In the StartPage.Load, drag and drop a List and set its properties as shown.
    image

  2. Drag and drop javascript action after the List.

  3. Copy and paste the following code inside the javascript code

var myList = ~.EmployeeList;

for(var i=0; i<~.EmployeeList.length; i++){
	myList[i].lastname = 'Alison';
}
  1. Drag and drop a repeater control on the StartPage designer and choose ListItemType as employee in the properties section.

  2. Drag and drop a Label inside the repeater.

  3. Drag and drop a SetValue action inside the Repeater.Event.Load page as follows

  1. In StartPage.Load, drag and drop SetValue action and set its properties as in picture

Click on preview. You will see the data from the employee list as below.

image

Sample
SetListElementsUsingJavascript.sapz (7.6 KB)