Skip to Content
avatar image
-1
Former Member

Display Values Dynamically in alv report

Hi Team,

I have a scenario like below .

If the Values are available for the Column, then only i need to display values in the report Output , else Column itself should not be visible in the report output.

I have used the "REUSE_ALV_GRID_DISPLAY" but its fixed in positions and can i know

how to achieve my above scenario .

Regards,

Karthik S

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Mar 08, 2017 at 01:35 PM

    If you can switch to the class cl_gui_alv_grid_ext, it does have a method set_hide_empty_columns which works beautifully... unfortunately I can't find it in CL_SALV_TABLE.

    * create and initialize ALV display
      CREATE OBJECT ob_grid
        EXPORTING
          i_parent = cl_gui_custom_container=>screen0
        EXCEPTIONS
          OTHERS   = 1.
    
    * hide all visible empty columns at first display:
      ob_grid->set_hide_empty_columns( abap_true ).
    
    Add comment
    10|10000 characters needed characters exceeded

  • Mar 03, 2017 at 09:25 AM

    Best to use "CL_SALV_TABLE" instead of the FM, check for existing data and set the Visibility of the specific Column ...

    Quick sample :

    REPORT ztmp_test.
    
    DATA:
      lt_spfli   TYPE TABLE OF spfli,
      lr_table   TYPE REF TO cl_salv_table,
      lr_columns TYPE REF TO cl_salv_columns_table,
      lr_column  TYPE REF TO cl_salv_column_table.
    
    START-OF-SELECTION.
      SELECT * UP TO 10 ROWS
        INTO TABLE lt_spfli
        FROM spfli.
      IF sy-subrc <> 0.
        "Msg ...
        EXIT.
      ENDIF.
    
      TRY.
          cl_salv_table=>factory(
            IMPORTING
              r_salv_table   = lr_table
            CHANGING
              t_table        = lt_spfli ).
    
          LOOP AT lt_spfli TRANSPORTING NO FIELDS
            WHERE countryfr IS INITIAL.
            EXIT.
          ENDLOOP.
          IF sy-subrc = 0.
            lr_columns = lr_table->get_columns( ).
            lr_column ?= lr_columns->get_column( 'COUNTRYFR' ).
            lr_column->set_visible( abap_false ).
          ENDIF.
    
        CATCH cx_salv_msg INTO DATA(lr_msg).
          "Msg ...
      ENDTRY.
    
      lr_table->display( ).
    Add comment
    10|10000 characters needed characters exceeded

    • Yes you are right ofcourse.

      Well I hope a well-informed decision will be made, fetching the FieldCatalog using the "CL_SALV_CONTROLLER_METADATA" and / or RTTS-functionality ...

      Thanks for the suggestion

      Nic T.

  • Mar 08, 2017 at 11:52 AM
    -1

    Hi,

    You can try like this:

    I have considered one column - ELOC. When there will be no data in the ELOC, the column won't be displayed but if there is a single record or more records then the whole column has to be displayed.

    TYPES: BEGIN OF lty_tab,
             eid   TYPE char10,
             ename TYPE char10,
             eloc  TYPE char10,
           END OF lty_tab.
    
    DATA: lt_tab  TYPE TABLE OF lty_tab,
          ls_tab  TYPE lty_tab,
          lt_fcat TYPE slis_t_fieldcat_alv,
          ls_fcat TYPE slis_fieldcat_alv
          .
    
    CLEAR: ls_tab.
    ls_tab-eid = '1'.
    ls_tab-ename = 'Test1'.
    ls_tab-eloc = 'IND'.
    APPEND ls_tab TO lt_tab.
    
    CLEAR: ls_tab.
    ls_tab-eid = '2'.
    ls_tab-ename = 'Test2'.
    ls_tab-eloc = 'RU'.
    APPEND ls_tab TO lt_tab.
    
    ********* fieldcatalog **********
    
    CLEAR: ls_fcat.
    ls_fcat-fieldname = 'EID'.
    ls_fcat-seltext_m = 'Emp ID'.
    APPEND ls_fcat TO lt_fcat.
    
    CLEAR: ls_fcat.
    ls_fcat-fieldname = 'ENAME'.
    ls_fcat-seltext_m = 'Emp Name'.
    APPEND ls_fcat TO lt_fcat.
    
    CLEAR: ls_fcat.
    ls_fcat-fieldname = 'ELOC'.
    ls_fcat-seltext_m = 'Emp Loc'.
    
    ************ LOOP TO CHECK WHETHER THE COLUMN CONTAINS ANY RECORD OR NOT **************
    LOOP AT lt_tab INTO ls_tab WHERE ( eloc IS INITIAL ).
      DATA: lv_index TYPE sy-tabix.
      lv_index =  sy-tabix.
      lv_index = lv_index - 1.
      READ TABLE lt_tab INTO ls_tab INDEX lv_index. "CHECK TO SEE IF THE PREVIOUS RECORD IS ALSO BLANK OR NOT
      IF ls_tab-eloc IS INITIAL.
        ls_fcat-no_out = 'X'.
      ENDIF.
    ENDLOOP.
    APPEND ls_fcat TO lt_fcat.
    
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
        i_callback_program = sy-repid
        it_fieldcat        = lt_fcat
      TABLES
        t_outtab           = lt_tab.
    IF sy-subrc <> 0.
    * Implement suitable error handling here
    ENDIF.
    

    When ELOC has all the records:

    When ELOC has only one records:

    When ELOC has no records:

    Since I have done ls_fcat-no_out = 'X', in the ALV layout the hidden column will be shown. However, if you want to hide the column from ALV layout, then do: ls_fcat-tech = 'X'.

    Hope this helps!!

    EDIT:

    CORRECTING THE MAIN LOOP:

    ************ LOOP TO CHECK WHETHER THE COLUMN CONTAINS ANY RECORD OR NOT **************
    LOOP AT lt_tab INTO ls_tab WHERE ( eloc IS NOT INITIAL ).
      EXIT.
    ENDLOOP.
    IF sy-subrc NE 0.
      ls_fcat-no_out = 'X'.
    ENDIF.
    APPEND ls_fcat TO lt_fcat.
    
    Add comment
    10|10000 characters needed characters exceeded

    • You can't be serious about that LOOP with READ inside... no_out only needs to be set once per column and only a simple check for whether any value exists is needed. Example in Nic's code was fine.

  • avatar image
    Former Member
    Mar 09, 2017 at 06:52 AM
    -1

    Hi Karthik,

    I liked the code posted by @Subhajit Roy. That's the simple way of doing it without using OOPS. But, that code works when there are 2 or 3 records in the final table, it doesn't work when there are more that 3 records(say 4) and lets say that the first record has ELOC with some value and the rest 3 are initial. According to the 'LOOP' logic above, it puts ls_fcat-no_out = 'X' and the field doesn't show up in the ALV output although there is a value in ELOC. This logic below, would work in any case:

    TYPES: BEGIN OF lty_tab,
             eid   TYPE char10,
             ename TYPE char10,
             eloc  TYPE char10,
           END OF lty_tab.
    
    DATA: lt_tab  TYPE TABLE OF lty_tab,
          lt_tab1 TYPE TABLE OF lty_tab,
          ls_tab  TYPE lty_tab,
          lt_fcat TYPE slis_t_fieldcat_alv,
          ls_fcat TYPE slis_fieldcat_alv
          .
    
    CLEAR: ls_tab.
    ls_tab-eid = '1'.
    ls_tab-ename = 'Test1'.
    ls_tab-eloc = 'IND'.
    APPEND ls_tab TO lt_tab.
    
    CLEAR: ls_tab.
    ls_tab-eid = '2'.
    ls_tab-ename = 'Test2'.
    ls_tab-eloc = 'RU'.
    APPEND ls_tab TO lt_tab.
    
    ********* fieldcatalog **********
    
    CLEAR: ls_fcat.
    ls_fcat-fieldname = 'EID'.
    ls_fcat-seltext_m = 'Emp ID'.
    APPEND ls_fcat TO lt_fcat.
    
    CLEAR: ls_fcat.
    ls_fcat-fieldname = 'ENAME'.
    ls_fcat-seltext_m = 'Emp Name'.
    APPEND ls_fcat TO lt_fcat.
    
    CLEAR: ls_fcat.
    ls_fcat-fieldname = 'ELOC'.
    ls_fcat-seltext_m = 'Emp Loc'.
    
    ************ CHECK WHETHER THE COLUMN CONTAINS ANY RECORD OR NOT **************
    lt_tab1 = lt_tab.
    DELETE lt_tab1 WHERE eloc IS INITIAL.
    IF lt_tab1 IS INITIAL.
     ls_fcat-no_out = 'X'.
    ENDIF.
    APPEND ls_fcat TO lt_fcat.
    
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
        i_callback_program = sy-repid
        it_fieldcat        = lt_fcat
      TABLES
        t_outtab           = lt_tab.
    IF sy-subrc <> 0.
    * Implement suitable error handling here
    ENDIF.
    Add comment
    10|10000 characters needed characters exceeded

    • Hi Rashmith,

      I am new to SAP ABAP. I just overlooked the fact that my code will also not work if there is some blank at the beginning of ELOC. So, thanks for pointing out the mistake and also providing a solution for that.

      I think what Jelena is suggesting is this:

      LOOP AT lt_tab INTO ls_tab WHERE ( eloc IS NOT INITIAL ).
         EXIT.
      ENDLOOP.
      IF sy-subrc NE 0.
        ls_fcat-no_out = 'X'.
      ENDIF.
      APPEND ls_fcat TO lt_fcat.