SALV Hierarchical Table 1 – Simple table display

By | October 3, 2008 | SALV HS, SALV Tutorial, Tutorials | 36,929 | 6

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:


*&---------------------------------------------------------------------*
*& 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
    TYPESBEGIN 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
    TYPESBEGIN 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
    DATAt_vbak TYPE STANDARD TABLE OF ty_vbak,
          t_vbap TYPE STANDARD TABLE OF ty_vbap.
*
*   Hierarchical ALV reference
    DATAo_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.
  DATAlo_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.
*
    DATAlx_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.
    DATAlt_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.

Entire series can be found at Tutorials > SALV HS Display

Way to make the SALV editable, follow: SALV Table Display – Editable ALV

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

6 Comments

  • Dear Mr Naimesh ,

    Thanks a lot for this wonderful blog.
    This is one of best and have become my favourite
    Can you cover following subject in blog

    a) Making ALV cells editable with cl_salv_table
    b) Any way to mix classical screen gui controls with OO ALV
    c) I want to add features to OOALV for creating a record and editing the record for ZTABLE (CRUD capabilities) How this can be done ?

    Regards ,

    AJIT

  • Hello Ajit,

    A)
    In the documentation of the class CL_SALV_TABLE, it says:
    “Tables displayed with ALV are not ready for input.”

    But I am trying to overcome this restriction.

    B) If I will be able to get it point A working than OO ALV features can be mixed in the SALV model.

    Regards,
    Naimesh Patel

  • Updated the blog with the link which shows a way to make SALV as editable and give us the object of the CL_GUI_ALV_GRID to work with its methods.

    Regards,
    Naimesh Patel

  • hey!
    awesome plot..
    congratulations.. keep up the good work..

  • Hi Naimesh,

    Kindly let me know how to send the Hierarchy List to spool?

    Thanks in advance.

  • sid

    Good!

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.