2008-06-22

web dynpro for abap基础知识(4)

本篇是关于如何访问controller context。

访问一个context node

对于每一个controller,系统都会为其自动创建一个interface,名为IF_<ctrol_name>。在这个interface中,对于每一个context node都会有一个相应的WDCTX_<node_name>存在。访问root node直接使用WD_CONTEXT,访问root node的子node使用get_child_node( )方法,要注意的是方法名作为参数被传递时必须是大写!方法返回指向指定node的引用,类型为IF_WD_CONTEXT_NODE。这个方法还有一个可选参数,是要访问的element在父node中的索引编号。

访问一个node element

获得一个指向node的引用后就可以使用get_element( )方法来获得指向element at selection的引用,类型为IF_WD_CONTEXT_ELEMENT。

DATA: lo_nd_flights TYPE REF TO lf_wd_context_node.
DATA: lo_el_flights  TYPE REF TO if_wd_context_element.

lo_wd_flights = wd_context->get_child_node( name = wd_this->wdctx_flights ).

*get element at lead selection
lo_el_flight = lo_flight_flights->get_element( ).

*if lead selection is not set, ELEM_FLIGHTS will be initial
IF ( lo_el_flights IS INITIAL ).
...
ENDIF.

要获得索引为n的element,使用方法get_element( index = n )。get_element_count( ) 方法返回一个collection中的element的数目。

访问单个node的attribute

一旦获得了指向一个node element的引用,就可以使用下面两个方法来访问这个node的attribute。get_attribute( )可以访问任意一个attribute,所需的参数是要访问的attribute的名字。另一个方法是get_static_attributes( ),用于访问static defined attributes(此处不明白)。

DATA: lo_nd_flights TYPE REF TO lf_wd_context_node.
DATA: lo_el_flights  TYPE REF TO if_wd_context_element.
DATA: lv_connid TYPE wd_this->element_flights-connid.

lo_wd_flights = wd_context->get_child_node( name = wd_this->wdctx_flights ).
lo_el_flight = lo_flight_flights->get_element( ).

*get single attribute
lo_el_flights->get_attribute(
EXPORTING
name = 'CONNID'
IMPORTING
value = lv_connid ).

controller context中的每个node在interface IF_<ctrl_name>中都有一个隐含的名为element_<node_name>structure和名为elements_<node_name>的standard internal table,其line type是element_<node_name>。这个internal table用于存放multiple node elements的attributes。(此处不明白,既然每个node都有一个这样对应的internal table,为什么还说存放multiple node elements的attributes??)

一个使用get_static_attributes( )访问一个node 的static attributes的例子:
DATA: lo_nd_flights TYPE REF TO lf_wd_context_node.
DATA: lo_el_flights  TYPE REF TO if_wd_context_element.
DATA: ls_flights TYPE wd_this->element_flights.

lo_nd_flights = wd_context->get_child_node( name = wd_this->wdctx_flights ).
lo_el_flights = lo_nd_flights->get_elements

*get all static declared attributes
lo_el_flights->get_static_attributes(
IMPORTING
static_attributes = ls_flights ).

获取所有node elements的static attribues:

DATA: lo_nd_flights TYPE REF TO lf_wd_context_node.
DATA: lt_flights TYPE wd_this->element_flights.

lo_nd_flights = wd_context->get_child_node( name = wd_this->wdctx_flights ).

*get all static declared attributes for all node element
lo_nd_flights->get_static_attributes_table(
IMPORTING
table = lt_flights ).

没有评论:

发表评论