44233767 WEB DYNPRO Dynamic Runtime Modifications

Embed Size (px)

Citation preview

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    1/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Introduction

    Modifying the context structure at runtime

    Modifying the UI Element hierarchy at runtime The use of dynamic actions

    Contents:

    Dynamic Modifications at Runtime

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    2/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Dynamic Modifications at Runtime: Unit Objectives

    After completing this unit, you will be able to:

    Understand what is dynamic programming

    Dynamically modify and create UI elements

    Dynamically create context elements

    Dynamically bind UI element values to context

    elements

    Dynamically create actions

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    3/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Dynamic Runtime Modifications

    What is type of dynamic modifications can be made at

    runtime?

    Dynamic Context ManipulationThe creation, modification and deletion of context nodes andattributes

    Dynamic UI ManipulationThe creation, modification and deletion of UI elements

    DynamicAssignment ofActions to UI Elements

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    4/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Dynamic Runtime Modifications

    There are several situations in which type of coding could berequired:

    When the structure of your data is not known until runtime

    When you want the behaviour of a screen to be generic andtherefore dynamic

    When you are writing utility components that must function ina generic manner

    Etc

    Under what circumstances should I write coding that

    performs dynamic modifications?

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    5/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Dynamic Context Data Creation

    Example of context

    metadata to be created

    dynamically?Context Root (c=1..1, s=true)FLIGHTS (c=0..n, s=true)

    Context Metadata to be

    created at runtime

    BOOKINGS (c=0..n, s=false)

    PRICE

    CARRID

    CONNID

    FLDATE

    BOOKID

    CUSTOMID

    CLASS

    PASSNAME

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    6/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Coding steps:

    Obtain a reference to the metadata of

    the context node that will act as the new

    nodes parent. In this case, we are

    creating an independent node, therefore

    we get a reference to the metadata of

    the root node.

    Call static method

    create_nodeinfo_from_struct( ) from

    helper class cl_wd_dynamic_tool

    A DDIC Structure can be used for the

    attribute creation.

    Dynamic Node Creation (1)

    Context Root (c=1..1, s=true)

    FLIGHTS (c=0..n, s=true)

    Context Metadata to be

    created at runtime

    BOOKINGS (c=0..n, s=false)

    PRICE

    CARRID

    CONNID

    FLDATE

    BOOKID

    CUSTOMID

    CLASS

    PASSNAME

    DDIC Structure SFLIGHT

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    7/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Dynamic Node Creation related to a structure

    Coding steps:

    Obtain a reference to the metadata of the context node that will act as the

    new nodes parent.

    Call static method create_nodeinfo_from_struct( ) from helper class

    cl_wd_dynamic_tool to create from a DDIC structure a node.

    DATA: rootnode_info TYPE REF TO if_wd_context_node_info,table_name type string value 'SFLIGHT',node_name type string value 'CONNECTIONS'.

    * get root node info of contextrootnode_info = wd_context->get_node_info( ).

    * create node named CONNECTIONS of sflightcl_wd_dynamic_tool=>create_nodeinfo_from_struct(parent_info = rootnode_infonode_name = node_namestructure_name = table_name

    is_multiple = abap_falseis_mandatory = abap_true ).

    Cardinality 11

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    8/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Dynamic Sub Node Creation related to a structure

    DATA: dyn_node type ref to if_wd_context_node,dyn_node_info TYPE REF TO if_wd_context_node_info,node_name TYPE string value 'FLIGHTS',. . .

    * navigate from to via lead selectiondyn_node = wd_Context->get_Child_Node( Name = node_name ).dyn_node_info = dyn_node->get_node_info( ).

    * create sub node named BOOKINGS of sbookcl_wd_dynamic_tool=>create_nodeinfo_from_struct(parent_info = dyn_node_infonode_name = 'BOOKINGS'structure_name = 'SBOOK'is_multiple = abap_trueis_mandatory = abap_false ).

    Context Root (c=1..1, s=true)

    FLIGHTS (c=0..n, s=true)

    BOOKINGS (c=0..n,s=false)

    PRICE

    CARRID

    CONNID

    FLDATE

    BOOKID

    CUSTOMID

    CLASS

    PASSNAME

    Structure SBOOK

    Coding steps:

    Obtain a reference to node and reference to the

    metadata of the context node that will act as the new

    nodes parent

    Call static method create_nodeinfo_from_struct( ) to

    create from a DDIC structure a node

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    9/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Add a Dynamic Attribute to a Node

    Context Root (c=1..1, s=true)

    UI_ATTRIBUTES

    BUTTON_VISIBILITY

    TEXT_VISIBILITY

    * add context attribute to nodedata: Ui_Attributes_info type ref to If_Wd_Context_Node_info.data: ls_att type WDR_CONTEXT_ATTRIBUTE_INFO.

    * get node info of contextUi_Attributes_info = Node_Ui_Attributes->get_node_info( ).

    ls_att-name = `TEXT_VISIBILITY`.ls_att-TYPE_NAME = 'WDUI_VISIBILITY'.

    Ui_Attributes_info->add_attribute( ATTRIBUTE_INFO = ls_att ).

    Coding steps:

    Obtain a reference to the metadata of the parent node

    that will contain the attribute

    Fill structure ( WDR_CONTEXT_ATTRIBUTE_INFO )

    with attribute properties

    Add attribute to parent node

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    10/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Standard Hook Method View controller

    Only a view controller has these hook methods.

    The method WDDOMODIFYVIEWwill only be called if:

    The view is part of the current view assembly and this is the

    first time the view is required, or

    The view is part of the current view assembly and an action

    belonging to the same view controller has been processed.

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    11/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Principles of UI element manipulation

    The following coding principles must be adhered to during UI element

    manipulation:1. Only perform direct manipulation of UI element objects when it is not

    possible to control their behaviour through context binding.

    2. UI manipulation is only permitted within the wdDoModifyView()

    method of a view controller.

    3. wdDoModifyView() has a boolean parameter called firstTime.Typically, you will only build a dynamic UI element hierarchy whenfirstTime == true. This avoids rebuilding the UI element hierarchy

    unnecessarily.

    4. Do NOT implement any coding in wdDoModifyView() that modifies

    the context! The context should be considered read-only during the

    execution of this method.

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    12/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Dynamic UI manipulation (1)

    Context Root

    Connections

    PRICE

    CARRID

    CONNID

    FLDATE

    Context Metadata UI Element Hierarchy tobe created at runtime

    RootUIElementContainer

    CARRIDLabel

    CARRIDInput

    CONNIDLabel

    CONNIDInput

    FLDATELabel

    FLDATEInput

    PRICELabel

    PRICEInput

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    13/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Dynamic UI manipulation (2)

    Coding steps:

    Check that this is the

    first time the view has

    been rendered

    Obtain a reference to

    the root UI elementcontainer

    UI Element Hierarchy tobe created at runtime

    RootUIElementContainer

    Context Root

    Connections

    PRICE

    CARRID

    CONNID

    FLDATE

    Context Metadata

    method WDDOMODIFYVIEW .

    data: wd_container type ref to cl_wd_uielement_container,

    . . .

    * Check if first timecheck first_time = abap_true.

    wd_container ?= view->get_element( 'ROOTUIELEMENTCONTAINER' ).

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    14/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Dynamic UI manipulation (3)

    Coding steps:

    Create a new InputField UI

    element object (bind to

    context attribute

    Create a new Label UI

    element object

    Set the Labels properties

    as required

    Add the Label object to the

    UI element container

    Set the InputFields

    properties as required

    Add the InputField to the

    UI element container

    InputField

    UI Element Hierarchy tobe created at runtime

    RootUIElementContainer

    InputField

    Label

    Label

    bind

    add

    Context Root

    Connections

    PRICE

    CARRID

    CONNID

    FLDATE

    Context Metadata

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    15/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    * Create label and input field** create a input fieldwd_input_field = cl_wd_input_field=>new_input_field( view = view

    bind_value = 'CONNECTIONS.CARRID').

    ** create a label for the input fieldwd_label = cl_wd_label=>new_label( label_for = wd_input_field->id ).

    ** set matrix_head_data for the labelcl_wd_matrix_head_data=>new_matrix_head_data( element = wd_label ).

    ** add label to container

    wd_container->add_child( wd_label ).

    ** set matrix_data for the input_fieldcl_wd_matrix_data=>new_matrix_data( element = wd_input_field ).

    ** add input field to containerwd_container->add_child( wd_input_field ).

    Dynamic UI manipulation (4)

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    16/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Dynamic Actions

    Certain UI elements can trigger client-side events (e.g. pressing enter

    in an InputField, toggling a CheckBox or selecting the row of atable).

    In order for the client-side event to trigger the execution of a server-

    side method, Web Dynpro uses the concept ofActions.

    Actions can either be assigned declaratively to UI element events atdesign time, or dynamically at runtime.

    Actions assigned dynamically can only refer to existing server-side

    action handler methods. It is not possible to define the coding of an

    action event handler dynamically; only to define which existing action

    handler will be called when a client-side event is trapped.

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    17/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Action Declaration

    Declared action

    Coding requiredfor dynamic

    assigned action

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    18/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    * Create button

    ** create button UI elementwd_button = cl_wd_button=>new_button( text = 'Show Flights'on_action = 'SELECT_FLIGHTS' ).

    ** set matrix_head_data for the labelcl_wd_matrix_head_data=>new_matrix_head_data( element = wd_button ).

    ** add button to containerwd_container->add_child( wd_button ).

    Create a Dynamic Button

    Coding steps: Create a new Button UI

    element object (assign an

    predefined action)

    Set the Button properties

    as required

    Add the Button to the UI

    element containerButton

    UI Element Hierarchy tobe created at runtime

    RootUIElementContainer

    InputField

    Label

    Context Root

    Connections

    PRICE

    CARRID

    CONNID

    FLDATE

    Context Metadata

    Button

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    19/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Was this a good approach?

    Development Principle

    Only if the required functionality of your application does notpermit design time declarations, then use a dynamicmodification approach.

    All context node/attribute and UI elements which can be

    created during design time should be created during designtime.

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    20/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Example for Dynamic Programming

    Display the content ofANY table

    Dynamic Context

    Dynamic table UI element

    Dynamic data retrieval

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    21/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Example for Dynamic Programming I

    DATA:group_1 TYPE REF TO cl_wd_uielement_container,

    new_tab TYPE REF TO cl_wd_table,dyn_node TYPE REF TO if_wd_context_node,tabname_node TYPE REF TO if_wd_context_node,rootnode_info TYPE REF TO if_wd_context_node_info,stru_tab TYPE REF TO data,tablename TYPE string.

    FIELD-SYMBOLS TYPE table.

    * get node info of context root noderootnode_info = wd_context->get_node_info( ).

    * Get the name of the table to be createdtabname_node = wd_context->get_child_node( name = 'TABLE_DATA' ).tabname_node->get_attribute( EXPORTING name = 'NAME' IMPORTING value =tablename ).

    translate tablename to upper case.

    * create sub node of structure (tablename)cl_wd_dynamic_tool=>create_nodeinfo_from_struct(parent_info = rootnode_infonode_name = tablenamestructure_name = tablenameis_multiple = abap_true ).

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    22/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    Example for Dynamic Programming II

    * remove "old" table UI element from view , if necessarygroup_1 ?= view->get_element( 'GROUP_1' ).

    group_1->remove_child( id = 'TESTTAB' ).

    * * get instance of new nodedyn_node = wd_context->get_child_node( name = tablename ).

    DATA new_tab TYPE REF TO cl_wd_table.

    * create new UI element tablenew_tab = cl_wd_dynamic_tool=>create_table_from_node(ui_parent = group_1table_id = 'TESTTAB'node = dyn_node ).

    ** fill context node with data* create internal table of (tabletype)

    CREATE DATA stru_tab TYPE TABLE OF (tablename).ASSIGN stru_tab->* TO .

    * Get table contentSELECT * FROM (tablename) INTO CORRESPONDING FIELDS OF TABLE .

    * Bind internal table to context nodedyn_node->bind_table( ).

  • 8/6/2019 44233767 WEB DYNPRO Dynamic Runtime Modifications

    23/23

    SAP AG 2005, Title of Presentation / Speaker Name / #

    You should now be able to:

    Understand what is dynamic programming

    Dynamically modify and create UI elements

    Dynamically create context elements

    Dynamically bind UI element values to context

    elements

    Dynamically create actions

    Web Dynpro Dynamic Programming: Unit Summary