Saturday, December 18, 2010

Programming dynamic ALV in ABAP Web Dynpro

Scenario: To create a dynamic ALV based on a dynamic context node in Web Dynpro for ABAP. As a Case study, we’ll use the SFLIGHT flight data to select which data fields we want to show in an ALV.

Procedure:

1. First, we’ll create the ZWD_SFLIGHT Web Dynpro component.

2. Next, we create a view

So, we have a WD component and a view. Now, we can insert a ViewContainerUIElement element in the view to show the ALV. The component’s name must be VCU_ALV.

We can activate in this moment.

3. In the Used Components tab of the Web Dynpro Component, we have to define the SALV_WD_TABLE component in order to use the ALV component model within the view.

4. Next, we must define a Controller Usage in the COMPONENTCONTROLLER and in the View Properties tab. To do that, press the button .

Then, we have an ALV component as well as its controller.

5. To show the ALV inside the View, we must embed the TABLE View of the SALV_WD_TABLE component. Go to the Window and select the ViewContainerUIElement (VCU_ALV) that we have created before.

6. Then, select the next option.

7. Now, we are ready to build the dynamic context node to give ALV its structure.

For example purposes, we’ll create 5 checkboxes which means the columns that can be showed (not necessary all of them) in the ALV. In this case, we’ll use CARRID, CONNID, FLDATE, PRICE and CURRENCY of SFLIGHT transparent table.

NOTE: You also have to create a button and an event handler method where you will validate which checkboxes are marked in order to know which fields you will show in the ALV.

8. We have to validate checkboxes entries to know which fields we must include in the ALV. Next code reads the context node and validates each checkbox in order to build the structure.

******* First, read the context node: *******
DATA:
node_param TYPE REF TO if_wd_context_node,
elem_param TYPE REF TO if_wd_context_element,
stru_param TYPE if_v_main=>element_param .
* navigate from to via lead selection
node_param = wd_context->get_child_node( name = if_v_main=>wdctx_param ).
* get element via lead selection
elem_param = node_param->get_element( ).
* get all declared attributes
elem_param->get_static_attributes(
IMPORTING
static_attributes = stru_param ).
******* Next, validate which fields you will use as ALV columns *******
DATA: ls_component TYPE cl_abap_structdescr=>component,
lr_type TYPE REF TO cl_abap_datadescr,
lt_components TYPE cl_abap_structdescr=>component_table.
  IF stru_param-carrid = 'X'.
lr_type ?= cl_abap_typedescr=>describe_by_name( p_name = 'S_CARR_ID' ).
ls_component-name = 'CARRID'.
ls_component-type = lr_type .
APPEND ls_component TO lt_components.
ENDIF.
IF stru_param-connid = 'X'.
lr_type ?= cl_abap_typedescr=>describe_by_name( p_name = 'S_CONN_ID' ).
ls_component-name = 'CONNID'.
ls_component-type = lr_type .
APPEND ls_component TO lt_components.
ENDIF.
IF stru_param-fldate = 'X'.
lr_type ?= cl_abap_typedescr=>describe_by_name( p_name = 'S_DATE' ).
ls_component-name = 'FLDATE'.
ls_component-type = lr_type .
APPEND ls_component TO lt_components.
ENDIF.
IF stru_param-price = 'X'.
lr_type ?= cl_abap_typedescr=>describe_by_name( p_name = 'S_PRICE' ).
ls_component-name = 'PRICE'.
ls_component-type = lr_type .
APPEND ls_component TO lt_components.
ENDIF.
IF stru_param-currency = 'X'.
lr_type ?= cl_abap_typedescr=>describe_by_name( p_name = 'S_CURRCODE' ).
ls_component-name = 'CURRENCY'.
ls_component-type = lr_type .
APPEND ls_component TO lt_components.
ENDIF.
******* Next, create a new structure and assign it to a new dynamic context node *******
DATA: lr_root_info TYPE REF TO if_wd_context_node_info ,
lr_node_info TYPE REF TO if_wd_context_node_info,
lr_structdescr TYPE REF TO cl_abap_structdescr.
  call method cl_abap_structdescr=>create
exporting
p_components = lt_components
receiving
p_result = lr_structdescr.
* Get context node info
lr_root_info = wd_context->get_node_info( ).
* Generate new node with the dynamic structure
CALL METHOD lr_root_info->add_new_child_node
EXPORTING
name = 'DATA'
is_initialize_lead_selection = abap_false
static_element_rtti = lr_structdescr
is_static = abap_false
RECEIVING
child_node_info = lr_node_info.
* get instance of new node
DATA: dyn_node TYPE REF TO if_wd_context_node.
  dyn_node = wd_context->get_child_node( name = 'DATA' ).
******* Last, Get reference to model *******
DATA: lo_interfacecontroller type ref to iwci_salv_wd_table.
  lo_interfacecontroller =   wd_this->wd_cpifc_alv( ).
  lo_interfacecontroller->set_data( dyn_node ).

9. Now, you need to create a Web Dynpro Application.

10. After that, let’s run our program. We can show an ALV table with a dynamic structure based on the checked fields.

No comments:

Tutorials on SAP-ABAP

Adobe Interactive Forms Tutorials

Business Server Pages (BSP)

Userexits/BADIs

Web Dynpro for ABAP (Step by step procedure for web dynpro,Tutorials on Web Dynpro,)

ALV Tutorials

Blog Archive

goodsites