09-10-2023 5:11 PM
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
09-10-2023 5:17 PM
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.
09-10-2023 5:17 PM
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.
09-12-2023 6:03 PM
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:
09-12-2023 7:33 PM
09-10-2023 6:57 PM
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.
09-10-2023 7:38 PM
09-11-2023 12:26 PM
I mean that I'd exclude entirely the rows with the red boxes around them.
09-11-2023 2:46 PM
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.
09-12-2023 10:34 AM
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?
09-12-2023 5:58 PM
ibrahimsap.ibrahim Here, you are posting on Matthew's answer. Let me comment below my answer 😉