Sunday, March 11, 2012
ALV Output in External View with ALV Configuration Model
  1. Create Context node in component controller. If necessary, map the component controller context to view Context. 
  2. Define a component usage for ALV component SALV_WD_TABLE in web dynpro component. In doing so, we specify a name for the component usage (for example, ALV_COMPONENT).
  3. Define the usage of this component in the properties for your view. Since we need the object model for our changes, choose the With Controller Access variant (component interface).
  4. Using external context mapping, we provide the DATA context node in the ALV interface controller with our application context node.
  5. Generate the UI element ViewContainerUIElement at the required position in the layout of view.
  6. We have already embedded our view in the window of your application. The name of the UI element ViewContainerUIElement that we prepared in the previous step for the ALV output is displayed beneath this view. Under this entry, we then embed the TABLE view for the SALV_WD_TABLE ALV component.
  7. To configure the ALV output, we must follow two additional steps:
    • Instantiate the used ALV component in a method of your view (for example, WDDOINIT).
    • Get the ALV configuration model and with it the object model, field objects, and column objects.
Example Program with steps
  • Create Web Dynpro component and Save it as local object. 
  • Define component usage for SALV_WD_TABLE in web dynpro component and Specify the name of component usage - ALV_COMPONENT. 
  • Go to Component Controller ->Context tab ->Create node ALV_TAB with cardinality 0..n. as shown below. 
  • Component controller ->Methods tab ->Create method -GET_DATA ->Write code to get the records or elements for ALV_TAB node. 
  • GET_DATA method code
  • METHOD get_data .
      DATA:
             lo_nd_alv_tab TYPE REF TO if_wd_context_node,
             lt_alv_tab    TYPE wd_this->elements_alv_tab.
    
    *  navigate from <context> to <alv_tab> via lead selection
      lo_nd_alv_tab = wd_context->get_child_node( name = wd_this->wdctx_alv_tab ).
      "Get employees information from PA0008
      SELECT *
        FROM pa0008
        INTO CORRESPONDING FIELDS OF TABLE lt_alv_tab.
      lo_nd_alv_tab->bind_table( new_items = lt_alv_tab set_initial_elements = abap_true ).
    
    ENDMETHOD.
    
  • Go to view ALV_V -> Layout tab -> Create View Container UI element VIEWCONTAINER as shown below.
  • Go to ALV Interface Controller ->Map our Component Controller Context node ALV_TAB with ALV context node DATA. This is called External Context Mapping. 

  • When we create Web Dynpro component with Window and View, view is automatically embedded. We need to embed TABLE view of SALV_WD_TABLE component in VIWCONTAINER created in View ALV_V. 
  • Create Web Dynpro Application and Save it as local object .
  • Run Web Dynpro application.
Creating Totals and Sub totals
  •   Define the usage of ALV component ALV_COMPONENT in the properties for your view. Since we need the object model for our changes, choose the With Controller Access variant (component interface).
  • Go to view ALV_V -> Methods tab ->Create GET_TOTAL(Write the code for creating subtotal of Annual Salary(ANSAL)->Call that method in WDDOINIT. 
  • Steps in GET_TOTAL method. 
    1. Getting the instance of ALV Component and Interface Controller. 
    2. Getting all columns
    3. Looping at columns and declaring aggregation rule for ANSAL & DIVGV column.
    4. For creating subtotal based on TRFGB(Pay Scale Area), Create a sort rule for TRFGB.
  • GET_TOTAL method code
  •   "Create instance for ALV Component usage.
      DATA lo_cmp_usage TYPE REF TO if_wd_component_usage.
    
      lo_cmp_usage =   wd_this->wd_cpuse_alv_component( ).
      IF lo_cmp_usage->has_active_component( ) IS INITIAL.
        lo_cmp_usage->create_component( ).
      ENDIF.
    
     "Get config model
      DATA lo_interfacecontroller TYPE REF TO iwci_salv_wd_table .
      lo_interfacecontroller =   wd_this->wd_cpifc_alv_component( ).
      DATA lr_config_table TYPE REF TO cl_salv_wd_config_table.
      lr_config_table = lo_interfacecontroller->get_model( ).
    
      "Total
      DATA:lr_field_amnt TYPE REF TO cl_salv_wd_field.
      lr_field_amnt =  lr_config_table->if_salv_wd_field_settings~get_field( fieldname = 'ANSAL' ).
      lr_field_amnt->if_salv_wd_aggr~create_aggr_rule( aggregation_type = IF_SALV_WD_C_AGGREGATION=>AGGRTYPE_TOTAL ).
    
      DATA:lr_field_hrs TYPE REF TO cl_salv_wd_field.
      lr_field_hrs =  lr_config_table->if_salv_wd_field_settings~get_field( fieldname = 'DIVGV' ).
      lr_field_hrs->if_salv_wd_aggr~create_aggr_rule( aggregation_type = IF_SALV_WD_C_AGGREGATION=>AGGRTYPE_TOTAL ).
    
      "Sub total based on Payscale area
      DATA:lr_field_trfgb TYPE REF TO cl_salv_wd_field.
      lr_field_trfgb =  lr_config_table->if_salv_wd_field_settings~get_field( fieldname = 'TRFGB' ).
      lr_field_trfgb->if_salv_wd_sort~create_sort_rule( sort_order = IF_SALV_WD_C_SORT=>SORT_ORDER_ASCENDING
                                                    group_aggregation = abap_true ).
    
    METHOD get_total .
      DATA:
            lo_cmp_usage           TYPE REF TO if_wd_component_usage,
            lo_interfacecontroller TYPE REF TO iwci_salv_wd_table,
            lr_config_table        TYPE REF TO cl_salv_wd_config_table,
            lr_column_settings     TYPE REF TO if_salv_wd_column_settings,
            lr_field_settings      TYPE REF TO if_salv_wd_field_settings,
            lr_aggr_rule           TYPE REF TO cl_salv_wd_aggr_rule,
            lr_sort_rule           TYPE REF TO cl_salv_wd_sort_rule.
      DATA:
            lr_column              TYPE REF TO cl_salv_wd_column,
            lr_field_amnt          TYPE REF TO cl_salv_wd_field,
            lr_field_hrs           TYPE REF TO cl_salv_wd_field,
            lr_field_trfgb         TYPE REF TO cl_salv_wd_field.
      DATA:
            lt_column              TYPE salv_wd_t_column_ref,
            ls_column              TYPE salv_wd_s_column_ref.
    
      "Create an instance of ALV component
      lo_cmp_usage =   wd_this->wd_cpuse_alv_component( ).
      "If not initialized, then initialize
      IF lo_cmp_usage->has_active_component( ) IS INITIAL.
        lo_cmp_usage->create_component( ).
      ENDIF.
      "Create an instance of ALV Interface Controller
      lo_interfacecontroller =   wd_this->wd_cpifc_alv_component( ).
      "Configuration of the ALV Output
      lr_config_table = lo_interfacecontroller->get_model( ).
      "Casting
      lr_column_settings ?= lr_config_table.
      "Get columns
      lt_column       = lr_column_settings->get_columns( ).
    
      LOOP AT lt_column INTO ls_column.
        CASE ls_column-id.
          WHEN 'ANSAL'.
            CALL METHOD lr_config_table->if_salv_wd_field_settings~get_field
              EXPORTING
                fieldname = 'ANSAL'
              RECEIVING
                value     = lr_field_amnt.
            " Create aggregate rule as total
            CALL METHOD lr_field_amnt->if_salv_wd_aggr~create_aggr_rule
              EXPORTING
                aggregation_type = if_salv_wd_c_aggregation=>aggrtype_total
              RECEIVING
                value            = lr_aggr_rule.
          WHEN 'DIVGV'.
            CALL METHOD lr_config_table->if_salv_wd_field_settings~get_field
              EXPORTING
                fieldname = 'DIVGV'
              RECEIVING
                value     = lr_field_hrs.
            " Create aggregate rule as total
            CALL METHOD lr_field_hrs->if_salv_wd_aggr~create_aggr_rule
              EXPORTING
                aggregation_type = if_salv_wd_c_aggregation=>aggrtype_total
              RECEIVING
                value            = lr_aggr_rule.
          WHEN 'TRFGB'.
            CALL METHOD lr_config_table->if_salv_wd_field_settings~get_field
              EXPORTING
                fieldname = 'TRFGB'
              RECEIVING
                value     = lr_field_trfgb.
            " Sub totals based on contract currency.
            CALL METHOD lr_field_trfgb->if_salv_wd_sort~create_sort_rule
              EXPORTING
                sort_order        = if_salv_wd_c_sort=>sort_order_ascending
                group_aggregation = abap_true
              RECEIVING
                value             = lr_sort_rule.
        ENDCASE.
      ENDLOOP.
    
    ENDMETHOD.
    
  • Save and Activate the Web Dynpro Component 
  • Run the Application again. 

3 comments:

Your useful comments, suggestions are appreciated.Your comments are moderated.

Followers

Contact Form

Name

Email *

Message *

Web Dynpro ABAP Book

An SAP Consultant

Follow US


Want to Contribute ?

If you are interested in writing about the new stuff you learn everyday while working, please write to the.sap.consultants@gmail.com.

Click on Contribution for more details.