# Nodes SAP ABAP Web Dynpro

We can directly create a node in the context root node or as child for other node. In Fig. we show how we can create a node.

Creating a node

For each node we create, we have to set some properties, as: cardinality, selection, etc. We can individually create the attributes for a node, or we can use some repository objects from the ABAP Dictionary. For example, when we work with tables, structures or views defined in the ABAP Dictionary, we can use them to define our node, and the attributes will be automatically generated.

Creating a Node that Uses an ABAP Dictionary Repository Object

We create a context node that uses the ABAP Dictionary structure YSTR_PERSON.

Node properties

The Button “Add Attribute from Structure” allows us to add all the structure components or only a part of them.

Selecting components of Structure

Because we need a proper attribute for all these structure components, we select all of them. As result, we have created a node, the attributes being automatically generated according to the structure components we have selected.

Context node STUDENT at design time

The properties of the node we have chosen are: cardinality 0. . .n, singleton – yes and Init. Lead Selection – yes.Web Dynpro Code Wizard,reading a context node or attribute.

The cardinality properties are very important, because they tell us how many elements a context node may have at runtime. We have four possible values:

• 1. . .1 Exactly one context element is instantiated
• 0. . .1 Maximum one context element is instantiated
• 0. . .n Zero or more context elements are instantiated
• 1. . .n One or more context elements are instantiated

The singleton property can be set YES or NO. When a node is singleton at the runtime, we have only one instance of this node. As usage example, for a nonsingleton node we can specify the context structure required for a Tree UI Element – sequential implementation.The Lead Selection Initialization property determines if the lead selection should be automatically set or manually programmed. In our case, this property is set “YES”, meaning that the lead selection is AUTOMATIC and the first element of this node it automatically selected. More details about lead selection – Table UI element.

For our node, we have defined the supply function named supply_student. Each node may have a supply function defined for it and automatically called by the Web Dynpro Framework. The scope of using a supply function is to populate a context node. In certain cases, we can use the Hook Method wdDoInit instead of a supply function. For more details, see the Hook Methods chapter.

The coding presented in Listing shows how we can use the supply function method to populate the context node STUDENT with three values.We have defined two variables: ls_student and lt_student. Ls_student is of type if_view_name=>element_student, where “view_name” represents the view name and “student” represents the node name. ”if_view_name” represents the programming interface for our view controller. By double-clicking on his name or clicking on the icon Display Controller Interface we can see the coding of this interface. Listing shows a coding part for the context node STUDENT.

Example of supply function Method

Example of view controller programming interface.

constants:
wdctx_Student type string value STUDENT.
types:
Element_Student type YSTR_PERSON,
Elements_Student type
standard table of Element_Student
with default key.
…..

As can be seen, Element_Student is of YSTR_PERSON type (our ABAP Dictionary structure).But, when we manually create context nodes without dictionary structure,in the view controller programming interface,a new structured type is created (Listing).

Example of view controller programming interface

This is why we use the following form to append values:ls_student-firstname = ‘Ionescu’.

The way we append values in an ABAP structure is:structure_name-component_name = value

With the declaration “DATA lt_student LIKE TABLE OF ls_student”,we declare an internal table.We use APPEND statement to add each new line at the end of the last line of the internal table.

At the end,we use the bind_structure method to populate the node with values.Instead of the declaration:

DATA ls_student TYPE if_view_name=>element_student.

we can use:
DATA ls_student TYPE wd_this->element_student.

In this way, we don’t need the view name anymore,because we use the wd_this self-reference.

The runtime structure

The node STUDENT was set Singleton, it has only an instance at runtime and the cardinality was set 0. . .n, meaning that, at runtime, we can have from zero to n elements. Because the lead selection was set “YES”, it was selected the first element of the node.To read, set or append a context node or attribute, we can use the Web Dynpro Code Wizard.

The option “As table operation” can be used in combination with the Read, Set or Append options for a node that allows the usage of this combination. We can read, for example, the entire context node STUDENT in an internal table.

Web Dynpro Code Wizard, reading a context node or attribute

In this case, the wizard has generated a variable lt_student of type wd_this -> elements_student. In Listing we saw that, in Interface Controller, besides the definition of a variable, the Element_structure Framework has also defined a variable Elements_Student, with the form:

Elements_Student typestandard table of Element_Studentwith default key.

This is the reason why the Wizard offers the possibility to read all the values from the STUDENT node in lt_student.

As we have mentioned in the last chapter, we can use a table type to populate with data a context node via a supply function. Our node STUDENT has its attributes from the YSTR_PERSON structure.The table type YTABLE_TYPE_STRUCTURE defined in the ABAP Dictionary has the same YSTR_PERSON structure, as line type.Listing shows how we can use a table type to populate with data a context node.

Example of supply function Method

Working with Child Nodes

We create the context structure presented.

Example of child node

It has a context node FACULTY, cardinality 1. . .1, Singleton with child node ADDRESS, cardinality 1. . .1, Singleton and two attributes FACULTY_NAME type STRING and SPECIALIZATION type STRING.

We read data from the attributes of the node ADDRESS as a child node for FACULTY. Listing shows a possible method to read these attributes.

With the first data definition, we create two variable lr_node and lr_subnode of if_wd_context_node type. With the second data definition, we create three variables with the same data type as the attributes we want to read. Data type Y_CHAR represents a data element defined in the ABAP Dictionary of CHAR type, length 6. Instead of the declaration:

lr_node = wd_context->get_child_node( ’FACULTY’).

we can use the form:

lr_node = wd_context->get_child_node( name = wd_this->wdctx_faculty).

In the first case we have to write with capital letters the node name, and in the second case we use the constant wdctx_faculty of string type, defined by the Framework in Interface Controller, with the form:

constants:

wdctx_faculty type string value ‘FACULTY’.

By using the get_child_node method of if_wd_context_node interface, we successively access the node FACULTY and the node ADDRESS, and by using the method get_attribute, we pass the values of the attributes in our local variable.Another possibility to read the values of the ADDRESS child node attributes is presented in Listing.