<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Display Values Dynamically in alv report in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415427#M11202</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;You can try like this:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;I have considered one column - &lt;STRONG&gt;ELOC&lt;/STRONG&gt;. When there will be no data in the &lt;STRONG&gt;ELOC&lt;/STRONG&gt;, the column won't be displayed but if there is a single record or more records then the whole column has to be displayed.&lt;/EM&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;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 &amp;lt;&amp;gt; 0.
* Implement suitable error handling here
ENDIF.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;When &lt;STRONG&gt;ELOC &lt;/STRONG&gt;has all the records:&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/24293-capture.jpg" /&gt;&lt;/P&gt;&lt;P&gt;When &lt;STRONG&gt;ELOC &lt;/STRONG&gt;has only one records:&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/24294-capture2.jpg" /&gt;&lt;/P&gt;&lt;P&gt;When &lt;STRONG&gt;ELOC &lt;/STRONG&gt;has no records:&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/24295-capture3.jpg" /&gt;&lt;/P&gt;&lt;P&gt;Since I have done &lt;STRONG&gt;ls_fcat-no_out = 'X', &lt;/STRONG&gt;in the ALV layout the hidden column will be shown. However, if you want to hide the column from ALV layout, then do: &lt;STRONG&gt;ls_fcat-tech = 'X'.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Hope this helps!!&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;EDIT:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;CORRECTING THE MAIN LOOP:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;************ 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.
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 08 Mar 2017 11:52:51 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2017-03-08T11:52:51Z</dc:date>
    <item>
      <title>Display Values Dynamically in alv report</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415425#M11200</link>
      <description>&lt;P&gt;Hi Team,&lt;/P&gt;&lt;P&gt;I have a scenario like below .&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;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&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;I have used the "REUSE_ALV_GRID_DISPLAY" but its fixed in positions and can i know&lt;/P&gt;&lt;P&gt;how to achieve my above scenario .&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Karthik S&lt;/P&gt;</description>
      <pubDate>Fri, 03 Mar 2017 08:12:02 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415425#M11200</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2017-03-03T08:12:02Z</dc:date>
    </item>
    <item>
      <title>Re: Display Values Dynamically in alv report</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415426#M11201</link>
      <description>&lt;P&gt;Best to use "CL_SALV_TABLE" instead of the FM, check for existing data and set the Visibility of the specific Column ...&lt;/P&gt;&lt;P&gt;Quick sample : &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;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 &amp;lt;&amp;gt; 0.
    "Msg ...
    EXIT.
  ENDIF.

  TRY.
      cl_salv_table=&amp;gt;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-&amp;gt;get_columns( ).
        lr_column ?= lr_columns-&amp;gt;get_column( 'COUNTRYFR' ).
        lr_column-&amp;gt;set_visible( abap_false ).
      ENDIF.

    CATCH cx_salv_msg INTO DATA(lr_msg).
      "Msg ...
  ENDTRY.

  lr_table-&amp;gt;display( ).&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Mar 2017 09:25:17 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415426#M11201</guid>
      <dc:creator>NTeunckens</dc:creator>
      <dc:date>2017-03-03T09:25:17Z</dc:date>
    </item>
    <item>
      <title>Re: Display Values Dynamically in alv report</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415427#M11202</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;You can try like this:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;I have considered one column - &lt;STRONG&gt;ELOC&lt;/STRONG&gt;. When there will be no data in the &lt;STRONG&gt;ELOC&lt;/STRONG&gt;, the column won't be displayed but if there is a single record or more records then the whole column has to be displayed.&lt;/EM&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;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 &amp;lt;&amp;gt; 0.
* Implement suitable error handling here
ENDIF.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;When &lt;STRONG&gt;ELOC &lt;/STRONG&gt;has all the records:&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/24293-capture.jpg" /&gt;&lt;/P&gt;&lt;P&gt;When &lt;STRONG&gt;ELOC &lt;/STRONG&gt;has only one records:&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/24294-capture2.jpg" /&gt;&lt;/P&gt;&lt;P&gt;When &lt;STRONG&gt;ELOC &lt;/STRONG&gt;has no records:&lt;/P&gt;&lt;P&gt;&lt;IMG class="migrated-image" src="https://community.sap.com/legacyfs/online/storage/attachments/storage/7/attachments/24295-capture3.jpg" /&gt;&lt;/P&gt;&lt;P&gt;Since I have done &lt;STRONG&gt;ls_fcat-no_out = 'X', &lt;/STRONG&gt;in the ALV layout the hidden column will be shown. However, if you want to hide the column from ALV layout, then do: &lt;STRONG&gt;ls_fcat-tech = 'X'.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Hope this helps!!&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;EDIT:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;CORRECTING THE MAIN LOOP:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;************ 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.
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Mar 2017 11:52:51 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415427#M11202</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2017-03-08T11:52:51Z</dc:date>
    </item>
    <item>
      <title>Re: Display Values Dynamically in alv report</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415428#M11203</link>
      <description>&lt;P&gt;If you can switch to the class &lt;STRONG&gt;cl_gui_alv_grid_ext, &lt;/STRONG&gt;it does have a method &lt;STRONG&gt;set_hide_empty_columns&lt;/STRONG&gt; which works beautifully... unfortunately I can't find it in CL_SALV_TABLE. &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;* create and initialize ALV display
  CREATE OBJECT ob_grid
    EXPORTING
      i_parent = cl_gui_custom_container=&amp;gt;screen0
    EXCEPTIONS
      OTHERS   = 1.

* hide all visible empty columns at first display:
  ob_grid-&amp;gt;set_hide_empty_columns( abap_true ).
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Mar 2017 13:35:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415428#M11203</guid>
      <dc:creator>raghug</dc:creator>
      <dc:date>2017-03-08T13:35:58Z</dc:date>
    </item>
    <item>
      <title>Re: Display Values Dynamically in alv report</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415429#M11204</link>
      <description>&lt;P&gt;Hi Karthik,&lt;/P&gt;&lt;P&gt;   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:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;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 &amp;lt;&amp;gt; 0.
* Implement suitable error handling here
ENDIF.&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Mar 2017 06:52:59 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415429#M11204</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2017-03-09T06:52:59Z</dc:date>
    </item>
    <item>
      <title>Re: Display Values Dynamically in alv report</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415430#M11205</link>
      <description>&lt;P&gt;It should be noted that to do this seriously for several columns some kind of dynamic field/column name assignment would be needed. Otherwise I bet OP will happily copy-paste this code for each and every column in the table and then the next person to maintain this will want to kill themselves. &lt;/P&gt;</description>
      <pubDate>Thu, 09 Mar 2017 22:30:35 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415430#M11205</guid>
      <dc:creator>Jelena_Perfiljeva</dc:creator>
      <dc:date>2017-03-09T22:30:35Z</dc:date>
    </item>
    <item>
      <title>Re: Display Values Dynamically in alv report</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415431#M11206</link>
      <description>&lt;P&gt;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. &lt;/P&gt;</description>
      <pubDate>Thu, 09 Mar 2017 22:40:29 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415431#M11206</guid>
      <dc:creator>Jelena_Perfiljeva</dc:creator>
      <dc:date>2017-03-09T22:40:29Z</dc:date>
    </item>
    <item>
      <title>Re: Display Values Dynamically in alv report</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415432#M11207</link>
      <description>&lt;P&gt;What on Earth? Why are you copying and deleting data? What if the table has tens of thousands of records? A simple LOOP IS INITIAL / IS NOT INITIAL or even a READ would do. C'mon now...&lt;/P&gt;</description>
      <pubDate>Thu, 09 Mar 2017 22:47:13 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415432#M11207</guid>
      <dc:creator>Jelena_Perfiljeva</dc:creator>
      <dc:date>2017-03-09T22:47:13Z</dc:date>
    </item>
    <item>
      <title>Re: Display Values Dynamically in alv report</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415433#M11208</link>
      <description>&lt;P&gt;Yes you are right ofcourse. &lt;/P&gt;&lt;P&gt;Well I hope a well-informed decision will be made, fetching the FieldCatalog using the "CL_SALV_CONTROLLER_METADATA" and / or RTTS-functionality ... &lt;/P&gt;&lt;P&gt;Thanks for the suggestion &lt;/P&gt;&lt;P&gt;Nic T.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Mar 2017 07:42:59 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415433#M11208</guid>
      <dc:creator>NTeunckens</dc:creator>
      <dc:date>2017-03-10T07:42:59Z</dc:date>
    </item>
    <item>
      <title>Re: Display Values Dynamically in alv report</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415434#M11209</link>
      <description>&lt;P&gt;Hi Rashmith,&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;I think what Jelena is suggesting is this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;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.&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Mar 2017 09:01:33 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/display-values-dynamically-in-alv-report/m-p/415434#M11209</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2017-03-10T09:01:33Z</dc:date>
    </item>
  </channel>
</rss>

