abapalvdynpro

How to hide a SALV table?


I'm trying to bind several SALV tables to the same container to let the user switch between the displayed SALV table. Only one SALV table should be visible at a time. I've tried doing this by binding the SALV table's containers to a common parent container, and setting the hidden SALV' containers by passing abap_false to their set_visible( ) method.

The full hierarchy of containers is:

CL_GUI_CUSTOM_CONTAINER        <-- custom container 'CC1000' created in painter
 |-CL_GUI_SPLITTER_CONTAINER   <-- splitter container
   |-CL_GUI_SIMPLE_CONTAINER   <-- left container
   |-CL_GUI_SIMPLE_CONTAINER   <-- right container
     |-CL_GUI_SIMPLE_CONTAINER <-- leaf container

The SALV table is not visible if it is bound to leaf container. It is visible if it is bound to right container.

Why am I doing this ? To control the visibility of the leaf container. The right container is always visible. Several leaf containers have the right container as their parent. Only one leaf container is visible at a time. Each leaf container is bound to a different SALV table. This should allow the user to switch between views using the list of views on the left.

SAP documentation states:

Since containers are themselves controls, you can nest them.

Minimal reproductible code:

Create dynpro screen 1000 and draw a custom container inside, named 'CC1000'.

If you bind lo_salv_table to leaf_container, the SALV won't be visible.

If you bind lo_salv_table to right_container (leaf's parent container), it will be visible.

REPORT ztest.

class lcl_report definition final.

  public section.

    class-methods:

      class_constructor,

      display.

  protected section.

    CLASS-DATA:
      gt_sflight type table of sflight.

  private section.

endclass.

CLASS lcl_report IMPLEMENTATION.

  method display.

    TYPES: begin of ty_s_view,
      name  type FPB_OBJECT,
      view  type ref to cl_gui_container,
    end of ty_s_view,

    ty_t_views type standard table of ty_s_view with key name.

    DATA: lo_column_table     type ref to cl_salv_column_table,
          lo_salv_viewlist    type ref to cl_salv_table,
          lo_salv_table       type ref to cl_salv_table,
          lt_views            type ty_t_views.

    data(lo_custom_container) = new cl_gui_custom_container(
      container_name          = 'CC1000'
    ).

    data(lo_splitter_container) = new cl_gui_splitter_container(
      parent                  = lo_custom_container
      rows                    = 1
      columns                 = 2
    ).

    lo_splitter_container->set_column_width(
      EXPORTING
        id                = 1
        width             = 15
    ).

    data(left_container) = lo_splitter_container->get_container(
                           row    = 1
                           column = 1
                         ).

    data(right_container) = lo_splitter_container->get_container(
                           row    = 1
                           column = 2
                         ).

    data(leaf_container) = new cl_gui_simple_container(
      parent    = right_container
    ).

    lt_views = value #( ( name = 'View 1' view = leaf_container ) ).

    cl_salv_table=>factory(
      EXPORTING
        r_container    = left_container
      IMPORTING
        r_salv_table   = lo_salv_viewlist
      CHANGING
        t_table        = lt_views
    ).

    lo_salv_viewlist->get_functions( )->set_all( abap_false ).

    cast cl_salv_columns_table( lo_salv_viewlist->get_columns( ) )->set_optimize( ).

    lo_column_table ?= cast cl_salv_columns_table( lo_salv_viewlist->get_columns( ) )->get_column( columnname = 'NAME' ).
    lo_column_table->set_cell_type( value = if_salv_c_cell_type=>hotspot ).

    lo_salv_viewlist->get_display_settings( )->set_striped_pattern( value = abap_true ).

    cl_salv_table=>factory(
      EXPORTING
        r_container    = leaf_container
      IMPORTING
        r_salv_table   = lo_salv_table
      CHANGING
        t_table        = gt_sflight
    ).

    lo_salv_viewlist->display( ).
    lo_salv_table->display( ).

  endmethod.

  METHOD class_constructor.

    SELECT * FROM SFLIGHT INTO TABLE gt_sflight.

  ENDMETHOD.

endclass.

MODULE status_1000 OUTPUT.
* SET PF-STATUS 'xxxxxxxx'.
* SET TITLEBAR 'xxx'.
  lcl_report=>display( ).
ENDMODULE.

  START-OF-SELECTION.

  CALL SCREEN 1000.

Solution

  • Concerning the simple container:

    About how to make your ALV invisible inside a container:

    ADDENDUM:

    If you want to share one container by several ALV grids, only one visible at a time, you may extend the above logic. Below is a full working demonstration based on the table SFLIGHT, you may display the flights of airlines LH, JL or UA by clicking the left button: 3 ALV Grid controls but only 1 at a time visible on the right

    REPORT zdemo.
    PARAMETERS lh RADIOBUTTON GROUP rbg1 USER-COMMAND rbg1 DEFAULT 'X'.
    PARAMETERS jl RADIOBUTTON GROUP rbg1.
    PARAMETERS ua RADIOBUTTON GROUP rbg1.
    
    CLASS lcl_app DEFINITION DEFERRED.
    LOAD-OF-PROGRAM.
      DATA o_app TYPE REF TO lcl_app.
      CREATE OBJECT o_app TYPE ('LCL_APP').
    INITIALIZATION.
      CALL METHOD lcl_app=>('CREATE_ALV') EXPORTING i_carrid = 'LH'.
      CALL METHOD lcl_app=>('CREATE_ALV') EXPORTING i_carrid = 'JL'.
      CALL METHOD lcl_app=>('CREATE_ALV') EXPORTING i_carrid = 'UA'.
      CALL METHOD lcl_app=>('SELECT_ALV').
    AT SELECTION-SCREEN.
      CALL METHOD lcl_app=>('SELECT_ALV').
    
    CLASS lcl_salv_helper DEFINITION.
      PUBLIC SECTION.
        METHODS constructor
          IMPORTING io_container TYPE REF TO cl_gui_container.
        METHODS get_latest_alv_grid
          RETURNING VALUE(ro_alv_grid) TYPE REF TO cl_gui_alv_grid.
      PRIVATE SECTION.
        DATA o_container TYPE REF TO cl_gui_container.
        DATA t_alv_grid  TYPE SORTED TABLE OF REF TO cl_gui_alv_grid WITH UNIQUE KEY table_line.
    ENDCLASS.
    CLASS lcl_app DEFINITION.
      PUBLIC SECTION.
        TYPES tt_sflight TYPE STANDARD TABLE OF sflight WITH EMPTY KEY.
        CLASS-DATA o_docking_right TYPE REF TO cl_gui_docking_container.
        CLASS-DATA o_salv_helper   TYPE REF TO lcl_salv_helper.
        CLASS-DATA t_alv_grid      TYPE STANDARD TABLE OF REF TO cl_gui_alv_grid WITH EMPTY KEY.
        CLASS-DATA t_t_sflight     TYPE STANDARD TABLE OF tt_sflight WITH EMPTY KEY.
        CLASS-METHODS create_alv IMPORTING i_carrid TYPE sflight-carrid.
        CLASS-METHODS select_alv.
    ENDCLASS.
    CLASS lcl_app IMPLEMENTATION.
      METHOD create_alv.
        IF o_docking_right IS NOT BOUND.
          o_docking_right = NEW #( repid = sy-repid
                                   dynnr = sy-dynnr
                                   side  = cl_gui_docking_container=>dock_at_right
                                   extension = 800 ).
        ENDIF.
        SELECT * FROM sflight WHERE carrid = @i_carrid INTO TABLE @DATA(Lt_sflight).
        INSERT lt_sflight INTO TABLE t_t_sflight REFERENCE INTO DATA(ref_t_sflight).
        cl_salv_table=>factory( EXPORTING r_container  = o_docking_right
                                IMPORTING r_salv_table = DATA(lo_salv_table)
                                CHANGING  t_table      = ref_t_sflight->* ).
        lo_salv_table->display( ).
        o_salv_helper = NEW lcl_salv_helper( o_docking_right ).
        DATA(lo_alv_grid) = o_salv_helper->get_latest_alv_grid( ).
        INSERT lo_alv_grid INTO TABLE t_alv_grid.
        lo_alv_grid->set_visible( cl_gui_control=>visible_false ).
      ENDMETHOD.
      METHOD SELECT_ALV.
        LOOP AT t_alv_grid INTO DATA(lo_alv_grid).
          lo_alv_grid->set_visible( cl_gui_control=>visible_false ).
        ENDLOOP.
        CASE abap_true.
          WHEN lh.
            t_alv_grid[ 1 ]->set_visible( cl_gui_control=>visible_true ).
          WHEN jl.
            t_alv_grid[ 2 ]->set_visible( cl_gui_control=>visible_true ).
          WHEN ua.
            t_alv_grid[ 3 ]->set_visible( cl_gui_control=>visible_true ).
        ENDCASE.
      ENDMETHOD.
    ENDCLASS.
    CLASS lcl_salv_helper IMPLEMENTATION.
      METHOD constructor.
        o_container = io_container.
      ENDMETHOD.
      METHOD get_latest_alv_grid.
        DATA controls TYPE STANDARD TABLE OF REF TO cl_gui_control.
        controls = VALUE #( ( o_container ) ).
        LOOP AT controls INTO DATA(control).
          TRY.
              INSERT LINES OF CAST cl_gui_container( control )->children
                     INTO TABLE controls.
            CATCH cx_root ##NO_HANDLER.
              " Control is not CL_GUI_CONTAINER
          ENDTRY.
          TRY.
              INSERT CAST #( control )
                     INTO TABLE t_alv_grid.
              IF sy-subrc = 0.
                ro_alv_grid = CAST #( control ).
              ENDIF.
            CATCH cx_root ##NO_HANDLER.
              " Control is not CL_GUI_ALV_GRID
          ENDTRY.
        ENDLOOP.
      ENDMETHOD.
    ENDCLASS.