Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Hide the value of certain cells in ALV GRID (not talking about whole columns/rows)

Sandra_Rossi
Active Contributor

Hello,

I had this below question and couldn't find a satisfactory answer in the forum.

For your information, I solved it and I will post the solution in 2 minutes, but you are welcome to add any information or solution.

I have this ALV which I display using CL_GUI_ALV_GRID, and I want to hide cell values (hide zeroes) in rows where the "price" column is lower than 650:

I thought about setting NO_ZERO = 'X' in the field catalog (LVC_S_FCAT) but it works only for a whole column, changing the types of numeric columns to characters in order to fully control the display but it's a bad trick at design level (a number should remain internally a number, it's only while displaying that hiding the value should occur), using colors in these cells or italic (but still displaying the zeroes) as a workaround but I find it very ugly.

Any solution?

Thanks.

Sandra

1 ACCEPTED SOLUTION

Sandra_Rossi
Active Contributor

Here is my solution:

The solution is based on the cell style (LVC_T_STYL and STYLEFNAME below), whose one special checkbox style value hides everything in the cell (constant ALV_STYLE_CHECKBOX_NO from the include <CL_ALV_CONTROL>). Whatever the cell value is, it's equivalent to make the content of a cell invisible.

Minimal reproducible code (you may also need to run the program SAPBC_DATA_GENERATOR to fill the table SFLIGHT):

REPORT.
INCLUDE <cl_alv_control>.
PARAMETERS dummy.
DATA alv_grid TYPE REF TO cl_gui_alv_grid.
TYPES: BEGIN OF ty_sflight.
         INCLUDE TYPE sflight.
         TYPES: t_style TYPE lvc_t_styl,
       END OF ty_sflight.
TYPES tt_sflight TYPE STANDARD TABLE OF ty_sflight WITH EMPTY KEY.
DATA flights TYPE tt_sflight.

AT SELECTION-SCREEN OUTPUT.
  IF alv_grid IS NOT BOUND.
    SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE @flights ORDER BY fldate.
    LOOP AT flights REFERENCE INTO DATA(flight)
        WHERE price < 650.
      flight->t_style = VALUE #( FOR <fieldname> IN VALUE string_table( ( `SEATSMAX` ) ( `SEATSOCC` )
                    ( `PAYMENTSUM` ) ( `SEATSMAX_B` ) ( `SEATSOCC_B` ) ( `SEATSMAX_F` ) ( `SEATSOCC_F` ) )
            ( fieldname = <fieldname>
              style     = alv_style_checkbox_no ) ).
    ENDLOOP.
    alv_grid = NEW cl_gui_alv_grid( i_parent = cl_gui_container=>screen0 ).
    alv_grid->set_table_for_first_display(
            EXPORTING  i_structure_name = 'SFLIGHT'
                       is_layout        = VALUE #( cwidth_opt = 'X' stylefname = 'T_STYLE' )
            CHANGING   it_outtab        = flights
            EXCEPTIONS OTHERS = 1 ).
  ENDIF.
9 REPLIES 9

Sandra_Rossi
Active Contributor

Here is my solution:

The solution is based on the cell style (LVC_T_STYL and STYLEFNAME below), whose one special checkbox style value hides everything in the cell (constant ALV_STYLE_CHECKBOX_NO from the include <CL_ALV_CONTROL>). Whatever the cell value is, it's equivalent to make the content of a cell invisible.

Minimal reproducible code (you may also need to run the program SAPBC_DATA_GENERATOR to fill the table SFLIGHT):

REPORT.
INCLUDE <cl_alv_control>.
PARAMETERS dummy.
DATA alv_grid TYPE REF TO cl_gui_alv_grid.
TYPES: BEGIN OF ty_sflight.
         INCLUDE TYPE sflight.
         TYPES: t_style TYPE lvc_t_styl,
       END OF ty_sflight.
TYPES tt_sflight TYPE STANDARD TABLE OF ty_sflight WITH EMPTY KEY.
DATA flights TYPE tt_sflight.

AT SELECTION-SCREEN OUTPUT.
  IF alv_grid IS NOT BOUND.
    SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE @flights ORDER BY fldate.
    LOOP AT flights REFERENCE INTO DATA(flight)
        WHERE price < 650.
      flight->t_style = VALUE #( FOR <fieldname> IN VALUE string_table( ( `SEATSMAX` ) ( `SEATSOCC` )
                    ( `PAYMENTSUM` ) ( `SEATSMAX_B` ) ( `SEATSOCC_B` ) ( `SEATSMAX_F` ) ( `SEATSOCC_F` ) )
            ( fieldname = <fieldname>
              style     = alv_style_checkbox_no ) ).
    ENDLOOP.
    alv_grid = NEW cl_gui_alv_grid( i_parent = cl_gui_container=>screen0 ).
    alv_grid->set_table_for_first_display(
            EXPORTING  i_structure_name = 'SFLIGHT'
                       is_layout        = VALUE #( cwidth_opt = 'X' stylefname = 'T_STYLE' )
            CHANGING   it_outtab        = flights
            EXCEPTIONS OTHERS = 1 ).
  ENDIF.

ibrahimsap.ibrahim Concerning your comment "I noticed that you didn't clear the values of the desired columns, you chose "t_style" to hide the value instead of clearing it. Why you did not clear the value .. loop at assigning <wa> where price <650 . if column-name in range then clear column-value?"

It won't work because I want to keep the field type as numeric in the ALV internal table.

See in my question the remark number 2:

  1. I thought about setting NO_ZERO = 'X' in the field catalog (LVC_S_FCAT) but it works only for a whole column,
  2. changing the types of numeric columns to characters in order to fully control the display but it's a bad trick at design level (a number should remain internally a number, it's only while displaying that hiding the value should occur),
  3. using colors in these cells or italic (but still displaying the zeroes) as a workaround but I find it very ugly.

0 Kudos

sandra.rossi Thank you for the information and good tip 🙂

matt
Active Contributor
0 Kudos

Changing the types of numeric columns to characters in order to fully control the display

That's exactly what I'd do. There is always a transformation between output and the actual data. Either you let the CL_SALV_TABLE classes handle it or you do it yourself. In my opinion, conceptually, it's the same.

Although for this particular requirement I'd question having blank rows - I'd rather just remove them entirely if they're not relevant.

Sandra_Rossi
Active Contributor
0 Kudos

There are no blank rows in my example, could you clarify?

matt
Active Contributor
0 Kudos

I mean that I'd exclude entirely the rows with the red boxes around them.

Sandra_Rossi
Active Contributor

Okay. In my productive program, it's a functional view of type "header-table left outer join item-table", the columns on the left side are from the header table, and the ones on the right side are from the item table.

0 Kudos

Hello Sandra,
I noticed that you didn't clear the values of the desired columns, you chose "t_style" to hide the value instead of clearing it. Why you did not clear the value .. loop at assigning <wa> where price <650 . if column-name in range then clear column-value?

Sandra_Rossi
Active Contributor
0 Kudos

ibrahimsap.ibrahim Here, you are posting on Matthew's answer. Let me comment below my answer 😉