Web Dynpro for ABAP(6):Context in Detail


3.10 Context in Detail

1.Context定义WDA设计时和运行时,使用的结构。

cardinalities:0..n ,1..n表示内表;

cardinalities:1..1表示结构体;

2.Programming of the Context,通过Runtime控制Context中保存value值

Node对应接口:IF_WD_CONTEXT_NODE

Element对应接口:IF_WD_CONTEXT_ELEMENT

Component Controller、View、Window都有自己的Context,Attribute:WD_CONTEXT。

获取Context子节点

方法:

GET_CHILD_NODE

3.获取多层级节点

方式1:依层级获取

示例代码:

method EXAMPLE .

data: l_node type ref to IF_WD_CONTEXT_NODE,

l_snode_11 type ref to IF_WD_CONTEXT_NODE,

L_snode_21 type ref to IF_WD_CONTEXT_NODE.

l_node = wd_context ->get_child_node( wd_this->wdctx_node_1 ).

l_snode_11 = l_node ->get_child_node( wd_this->wdctx_subnode_11 ).

l_snode_21 = l_snode_11 ->get_child_node( wd_this->wdctx_subnode_21 )

endmethod.

方式2:通过路径获取

示例代码:

method EXAMPLE .

data: l_snode_21 type ref to IF_WD_CONTEXT_NODE.

l_snode_21 = wd_context-> path_get_node ( 'NODE_1.SUBNODE_11.SUBNODE_21' ).

endmethod. 

4.获取、设置Context值

示例代码1:通过element读取attributes值

method EXAMPLE.

data: l_node type ref to IF_WD_CONTEXT_NODE,

    l_element type ref to IF_WD_CONTEXT_ELEMENT,

    l_my_string type string

l_node = wd_context->get_child_node ( wd_this->wdctx_node_1 ).

l_element= l_node-> get_element().

l_element-> get_attribute ( exporting name = 'ATTR1_1' importing value = l_my_string ).

endmethod. 

示例代码2:直接通过context读取attributes值

method EXAMPLE .

data: l_node type ref to IF_WD_CONTEXT_NODE,

    l_my_string type string.

l_node = wd_context->get_child_node ( wd_this->wdctx_node_1 ).

l_node-> get_attribute ( exporting name = 'ATTR1_1' importing value = l_my_string ).

endmethod. 

示例代码3:通过index获取element,读取attributes值

method EXAMPLE .

data: l_node type ref to IF_WD_CONTEXT_NODE,

    l_element type ref to IF_WD_CONTEXT_ELEMENT,

    l_my_string type string.

l_node = wd_context->get_child_node ( wd_this->wdctx_node_1 ) .

l_element= l_node-> get_element( 2 ) .

l_element->get_attribute( exporting name = 'ATTR1_1' importing value = l_my_string ).

endmethod. 

示例代码4:直接通过context,使用index,读取attributes值

method EXAMPLE .

data: l_node type ref to IF_WD_CONTEXT_NODE,

    l_my_string type string.

l_node = wd_context->get_child_node ( wd_this->wdctx_node_1 ) .

l_node->get_attribute( exporting index = 2 exporting name = 'ATTR1_1' importing value = l_my_string ).

endmethod. 

示例代码5:Component Controller,View,Window的Attributes中WD_THIS,表示当前Local Controller。通过Node,直接读取Structure值

method EXAMPLE .

data: l_node  type ref to IF_WD_CONTEXT_NODE,

    l_my_struc  type ref to IF_MY_CONTROLLER=>element_node_1.

l_node  = wd_context->get_child_node( wd_this->wdctx_node_1 ).

l_node->get_static_attributes( importing static_attributes = l_my_struc ).

endmethod. 

示例代码6:通过Node,index读取Structure值

method EXAMPLE .

data: l_node  type ref to IF_WD_CONTEXT_NODE,

    l_my_struc  type ref to IF_MY_CONTROLLER=>element_node_1.

l_node  = wd_context->get_child_node( wd_this->wdctx_node_1 ).

l_node->get_static_attributes( exporting index = 2 importing static_attributes = l_my_struc ).

endmethod.

示例代码7:通过Element,读取Structure值

method EXAMPLE .

data: l_node  type ref to IF_WD_CONTEXT_NODE,

    l_element  type ref to IF_WD_CONTEXT_ELEMENT,

    l_my_struc  type ref to IF_MY_CONTROLLER=>element_node_1.

l_node  = wd_context->get_child_node( wd_this->wdctx_node_1 ).

l_element  = l_node->get_element( 2 ).

l_element->get_static_attributes( importing static_attributes = l_my_struc ).

endmethod. 

示例代码8:通过Node,设置Structure值

method EXAMPLE .

data: l_node type ref to IF_WD_CONTEXT_NODE,

    l_my_struc type ref to IF_MY_CONTROLLER=>element_node_1.

l_node = wd_context->get_child_node ( wd_this->wdctx_node_1 ) .

l_node-> set_static_attributes ( static_attributes = l_my_struc ).

endmethod. 

示例代码9:通过创建Element,添加新记录,bind_element可以输入index,设置设置添加记录位置

method EXAMPLE .

data: l_node  type ref to IF_WD_CONTEXT_NODE,

    l_new_element  type ref to IF_WD_CONTEXT_ELEMENT,

    l_my_struc  type ref to IF_MY_CONTROLLER=>element_node_1.

l_node  = wd_context->get_child_node( wd_this->wdctx_node_1 ).

l_new_element  = l_node->create_element( ).

l_new_element->set_static_attributes( static_attributes = l_my_struc ).

l_node->bind_element( l_new_element ).

endmethod. 

示例代码10:通过bind structure添加新记录

method EXAMPLE .

data: l_node   type ref to IF_WD_CONTEXT_NODE,

    l_my_struc  type ref to IF_MY_CONTROLLER=>element_node_1.

l_node  = wd_context->get_child_node( wd_this->wdctx_node_1 ).

l_node->bind_structre( new_item = l_my_struc ).

endmethod.
 示例代码11:通过bind table添加新记录,通过参数set_initial_elements,设置false,保留原记录,插入新记录,默认为true。
method EXAMPLE .

data: l_node   type ref to IF_WD_CONTEXT_NODE,

    l_my_table  type ref to IF_MY_CONTROLLER=>elements_node_1.

l_node  = wd_context->get_child_node( wd_this->wdctx_node_1 ).

l_node->bind_table( new_items = l_my_table ).

endmethod.

5.Properties of Context Attributes

示例:DEMO_CONTEXT_PROP

设置Table Column控件中InputField属性properties

点击readOnly后面帮助,设置绑定Property

示例代码:

METHOD refresh_context .
  DATA: lr_flightnode TYPE REF TO if_wd_context_node,
        lr_elem TYPE REF TO if_wd_context_element,
        l_restrict TYPE wdy_boolean,
        l_arrtime TYPE spfli-arrtime.
* We set the context properties accordingly.
  lr_flightnode = wd_context->get_child_node( wd_this->wdctx_flights ).
  DO.
    lr_elem = lr_flightnode->get_element( sy-index ).
    IF lr_elem IS NOT BOUND.
      EXIT.
    ENDIF.
    lr_elem->get_attribute( EXPORTING name = 'ARRTIME'  IMPORTING  value  = l_arrtime ).
    IF sy-uzeit >= l_arrtime.
      l_restrict = 'X'.
    ELSE.
      l_restrict = space.
    ENDIF.
    CALL METHOD lr_elem->set_attribute_property
      EXPORTING
        attribute_name = 'ARRTIME'
        property       = lr_elem->e_property-read_only
        value          = l_restrict.
  enddo.
ENDMETHOD

6.Data Types of Context Node

接口:IF_WD_CONTEXT_NODE_INFO

设置attribute对应reference field

方法:

SET_ATTRIBUTE_REFERENCE_FIELD

设置Attribute显示格式

方法:

SET_ATTRIBUTE_FORMAT_PROPS

GET_ATTRIBUTE_FORMAT_PROPS

注:CL_WD_DYNAMIC_TOOL=>CHECK_MANDATORY_ATTR_ON_VIEW检查attribute格式

7Data Binding Using Index and Key

Dropdown Listbox和RadioButtonGroups数据绑定方式。

示例:WDR_TEST_EVENTS

Index绑定:

DropDownByIndex

RadioButtonGroupByIndex

Key绑定:

DropDownByKey

RadioButtonGroupByKey

Index绑定

创建Node节点(cardinality0..n);

通过接口方法IF_WD_CONTEXT_NODE_INFO=>SET_ATTRIBUTE_VALUE_SET设置值

示例代码:

data: NODE_INFO type ref to IF_WD_CONTEXT_NODE_INFO.

data:LT_VALUESET type WDR_CONTEXT_ATTR_VALUE_LIST,

L_VALUE type WDR_CONTEXT_ATTR_VALUE.

 

NODE_INFO = WD_CONTEXT->GET_NODE_INFO( ).

NODE_INFO = NODE_INFO->GET_CHILD_NODE( 'NODE1' ).

L_VALUE-VALUE= 'V1'.

L_VALUE-TEXT= 'yesterday'.

INSERT L_VALUE into table LT_VALUESET.

L_VALUE-VALUE= 'V2'.

L_VALUE-TEXT= 'today'.

INSERT L_VALUE into table LT_VALUESET.

L_VALUE-VALUE= 'V3'.

L_VALUE-TEXT= 'tomorrow'.

INSERT L_VALUE into table LT_VALUESET.

NODE_INFO->SET_ATTRIBUTE_VALUE_SET (

 NAME = 'ATTRIBUTE1'

 VALUE_SET = LT_VALUESET).