Vous êtes sur la page 1sur 16

ALV with Dynamic Structure in Web DynPro

Applies to:
Web DynPro ABAP. For more information, visit the Web Dynpro ABAP homepage.

Summary
Though its always better to give all the design to your WDC before running it, sometimes dynamic creation of ALV and the layout can be a good idea. It saves the effort thatd go into creating nodes and modifying the attributes time and again. This manual guides you through the creation of an ALV WebdynPro Component that builds itself up even as it runs. Author: Pushpraj Singh

Company: Infosys technologies Limited Created on: 09 December 2009

Author Bio
Pushpraj Singh is currently placed as a senior ABAP consultant at Infosys. He has worked on a few Web DynPro developments and find the field very interesting.

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 1

ALV with Dynamic Structure in Web DynPro

Table of Contents
Pre requisite Skills......................................................................................................................................... 3 Steps to create a dynamically structured ALV WebdynPro Application ........................................................... 3 Related Content .......................................................................................................................................... 15 Disclaimer and Liability Notice ..................................................................................................................... 16

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 2

ALV with Dynamic Structure in Web DynPro

Pre requisite Skills


You should know the ABCs of Web DynPros : node, attribute, view, application, etc are. Having created a static ALV WDC before would be really recommended.

Steps to create a dynamically structured ALV WebdynPro Application


1. Go to the transaction SE80 and choose Web Dynpro Comp. / Intf. In the list. Give a name and press Enter.

2. Youd be asked to enter a description. Enter and press enter.

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 3

ALV with Dynamic Structure in Web DynPro

3. Go to the WDC (WebdynPro Component) you just created and add the usage of the ALV component (Component SALV_WD_TABLE)

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 4

ALV with Dynamic Structure in Web DynPro

4. Now go to the MAIN view and to the Properties tab. (In case your SAPGUI doesnt create the MAIN view automatically create it manually) Click on the Create Controller Usage button and choose both the ALV component usages from the pop up.

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 5

ALV with Dynamic Structure in Web DynPro

5. Go to the Layout tab of the view and create a group. In this group add a View Container UI Element with the id view_cont.

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 6

ALV with Dynamic Structure in Web DynPro

6. Now go to the Methods tab of the view and write the following code in the method WDDOMODIFYVIEW (Method for Modifying the View Before Rendering).
METHOD wddomodifyview.

"this check avoids the creation of the node 'ALV' again on "any ALV event and thus prevents dump
IF first_time EQ abap_true. TYPES : lst_sflight TYPE STANDARD TABLE OF sflight. DATA : li_sflight TYPE lst_sflight, lw_sflight TYPE sflight.

"add the fields to be used as ALV columns


DATA : li_comp TYPE cl_abap_structdescr=>component_table.

********************************************************************** "this is the portion of the code where we can add or remove fields "to or from the ALV
wd_this->fill_comp( EXPORTING fv_name = 'CARRID'

" this would be the name of the attribute unde

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 7

ALV with Dynamic Structure in Web DynPro

r the node 'ALV'


fv_data_element = 'S_CARR_ID' " data element IMPORTING fi_comp = li_comp " cl_abap_structdescr=>component_table ). wd_this->fill_comp( EXPORTING fv_name = 'CONNID' " this would be the name of the attribute unde

r the node 'ALV'


fv_data_element = 'S_CONN_ID' " data element IMPORTING fi_comp = li_comp " cl_abap_structdescr=>component_table ). wd_this->fill_comp( EXPORTING fv_name = 'FLDATE' " this would be the name of the attribute unde

r the node 'ALV'


fv_data_element = 'S_DATE' " data element IMPORTING fi_comp = li_comp " cl_abap_structdescr=>component_table ). wd_this->fill_comp( EXPORTING fv_name = 'PRICE' " this would be the name of the attribute under

the node 'ALV'


fv_data_element = 'S_PRICE' " data element IMPORTING fi_comp = li_comp " cl_abap_structdescr=>component_table ).

********************************************************************** "create the desired structure and assign it to a new dynamic context node
DATA: lo_root_info TYPE REF TO if_wd_context_node_info, lo_node_info TYPE REF TO if_wd_context_node_info, lo_structdescr TYPE REF TO cl_abap_structdescr.

CALL METHOD cl_abap_structdescr=>create EXPORTING p_components = li_comp RECEIVING p_result = lo_structdescr.

* Get context node info


lo_root_info = wd_context->get_node_info( ).

* Generate new node 'ALV' with the dynamic structure


CALL METHOD lo_root_info->add_new_child_node EXPORTING name = 'ALV' is_initialize_lead_selection = abap_false static_element_rtti = lo_structdescr is_static = abap_false RECEIVING child_node_info = lo_node_info.

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 8

ALV with Dynamic Structure in Web DynPro

* get instance of new node


DATA : lo_dyn_node TYPE REF TO if_wd_context_node. lo_dyn_node = wd_context->get_child_node( name = 'ALV' ).

"Populate the internal table


SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE li_sflight UP TO 38 ROWS.

"transfer the contents of the internal table to the node


lo_dyn_node->bind_table( li_sflight ).

*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( lo_dyn_node ). ENDIF. ENDMETHOD.

Go to the method List of the View and add a method fill_comp.

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 9

ALV with Dynamic Structure in Web DynPro

METHOD fill_comp. DATA : lw_comp TYPE cl_abap_structdescr=>component, lv_type TYPE REF TO cl_abap_datadescr. lw_comp-name = fv_name. lv_type ?= cl_abap_typedescr=>describe_by_name( p_name = fv_data_element ). lw_comp-type = lv_type. APPEND lw_comp TO fi_comp. ENDMETHOD.

7. In order 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 View Container UI Element (view_cont) that was created earlier. Right click on it.

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 10

ALV with Dynamic Structure in Web DynPro

8. Choose TABLE of the ALV component.

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 11

ALV with Dynamic Structure in Web DynPro

9. Right Click on the component and create a WebdynPro Application.

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 12

ALV with Dynamic Structure in Web DynPro

10. Activate everything. 11. Execute the WDA.

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 13

ALV with Dynamic Structure in Web DynPro

As we can see, without having to create (or change) the context we can get any structure we want by simply doing a few changes to the code written in the method WDDOMODIFYVIEW.

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 14

ALV with Dynamic Structure in Web DynPro

Related Content
http://www.sdn.sap.com/irj/scn/elearn?rid=/library/uuid/201ddd3b-b4ce-2b10-8883-880ae8147f89 http://www.saptechnical.com/Tutorials/WebDynproABAP/DynamicALV/Demo.htm ?blog=/pub/wlg/2888 For more information, visit the Web Dynpro ABAP homepage.

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 15

ALV with Dynamic Structure in Web DynPro

Disclaimer and Liability Notice


This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade. SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document, and anyone using these methods does so at his/her own risk. SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample, including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this document.

SAP COMMUNITY NETWORK 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 16

Vous aimerez peut-être aussi