Download Android App of sapabap-vamsi

sapabap-vamsi : Download Android App for Mobiles

Saturday, August 20, 2011

SALV Hierarchical Table 1 - Simple table display

The new model of ALV - SALV Model - also provides the interface for application developer to develop the Hierarchical list. This type of list are also known as the Sequential List.

We use the hierarchical-sequential table to display simple hierarchical structures. In addition, exactly two hierarchy levels are available for the use: the header level and the position level. For each hierarchy level, we have to specify a table and also the key columns, like foreign keys.

We can use the model by using the reference of the calss CL_SALV_HIERSEQ_TABLE. We can use this class instead of the function module REUSE_ALV_HIERSEQ_LIST_DISPLAY.
Class CL_SALV_HIERSEQ_TABLE has a FACTORY method. We will call this method and ask for the object for the Hierarchical Table. After getting the HS (Hierarchical - Sequential) Object, we will call the DISPLAY method to generate a list on the screen. Isn't it sound too simple? Infact, it is simple.

You can visit the here to know more about the SALV models: SAP List Viewer : New Programming Model
Check out the Code snippet for the simplest Hierarchical Table:

Code Snippet to generate Hierarchical List
 
*&---------------------------------------------------------------------*
*& This code snippet will show how to use the CL_SALV_HIERSEQ_TABLE to
*&   generate the simplest Hierarchical (Sequential) ALV
*&---------------------------------------------------------------------*REPORT  zsalv_hs_simple.*
*----------------------------------------------------------------------*
*       CLASS lcl_report DEFINITION
*----------------------------------------------------------------------*CLASS lcl_report DEFINITION.*  PUBLIC SECTION.*
*   Final Header output table    TYPES: BEGIN OF ty_vbak,
           vbeln TYPE vbak-vbeln,
           erdat TYPE erdat,
           auart TYPE auart,
           kunnr TYPE kunnr,
           expand   TYPE char01,    "Column for Expand / Collapse
           END   OF ty_vbak.*
*   FInal Item output table    TYPES: BEGIN OF ty_vbap,
           vbeln TYPE vbap-vbeln,
           posnr TYPE vbap-posnr,
           matnr TYPE vbap-matnr,
           arktx TYPE vbap-arktx,
           END   OF ty_vbap.*
*   Standard internal tables    DATA: t_vbak TYPE STANDARD TABLE OF ty_vbak,
          t_vbap TYPE STANDARD TABLE OF ty_vbap.*
*   Hierarchical ALV reference    DATA: o_hs_alv TYPE REF TO cl_salv_hierseq_table.*    METHODS:*     data selection      get_data,*
*     Generating output      generate_output.*
*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*
*    In this section we will define the private methods which can
*      be implemented to set the properties of the ALV and can be
*      called in the GENERATE_OUTPUT method
*
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*ENDCLASS.                    "lcl_report DEFINITION*
*START-OF-SELECTION.
  DATA: lo_report TYPE REF TO lcl_report.*  CREATE OBJECT lo_report.*  lo_report->get_data( ).*  lo_report->generate_output( ).*
*----------------------------------------------------------------------*
*       CLASS lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*CLASS lcl_report IMPLEMENTATION.*  METHOD get_data.*   data selection - Header    SELECT vbeln erdat auart kunnr
           INTO  TABLE t_vbak
           FROM  vbak
           UP TO 10 ROWS.*
*   data selection - Item    SELECT vbeln posnr matnr arktx
           INTO  TABLE t_vbap
           FROM  vbap
           FOR   ALL ENTRIES IN t_vbak
           WHERE vbeln = t_vbak-vbeln.*  ENDMETHOD.                    "get_data*
*.......................................................................  METHOD generate_output.* New ALV instance
*   We are calling the static Factory method which will give back
*   the ALV object reference.
*    DATA: lx_data_err   TYPE REF TO cx_salv_data_error,
          lx_not_found  TYPE REF TO cx_salv_not_found.*
*   Fill the Binding table. Here we hae to provide the relationship
*     between all the Common Key fields in the Master and Slave
*     table. Based on this relationship, we will get the output.    DATA: lt_bind TYPE salv_t_hierseq_binding,
          la_bind LIKE LINE OF lt_bind.*    la_bind-master = 'VBELN'.    " VBELN as field of my T_VBAK
    la_bind-slave  = 'VBELN'.    " VBELN as field of my T_VBAP
    APPEND la_bind TO lt_bind.*
*   call factory method to generate the output    TRY.
        CALL METHOD cl_salv_hierseq_table=>factory
          EXPORTING
            t_binding_level1_level2 = lt_bind
          IMPORTING
            r_hierseq               = o_hs_alv
          CHANGING
            t_table_level1          = t_vbak
            t_table_level2          = t_vbap.
      CATCH cx_salv_data_error INTO lx_data_err.
      CATCH cx_salv_not_found  INTO lx_not_found.
    ENDTRY.*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*
*    In this area we will call the methods which will set the
*      different properties to the ALV
*
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
* Displaying the ALV
*   Here we will call the DISPLAY method to get the output on the screen    o_hs_alv->display( ).*  ENDMETHOD.                    "generate_output*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
*
*    In this area we will implement the methods which are defined in
*      the class definition
*
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*
*ENDCLASS.                    "lcl_report IMPLEMENTATION


This code will generate an output like this:


Note: I had tried to build this report in the Object Oriented manner. You can check the UML diagram as below. This desgin will provide us the great flexibility to add more methods will set and get different properties of the O_HS_ALV object. For example, if want to add the expand & collapse button, we will add one private method SET_EXPAND_OPTION. We will implement this method by adding the relative code and call this method before the list display.


In the code snippet, I have tried to differential three section which can be changed easily. In next blog posts, I will use this program as the base program and provide the code snippet to add the new functionality. Like: Section of CODE_ADD_1 can be replaced with the code provided in the future blog post.

1 comment:

  1. Copycat .. Copied from
    http://zevolving.com/2008/10/salv-hierarchical-table-1-simple-table-display/

    Regards,
    Naimesh Patel

    ReplyDelete