2023 Aug 06 2:13 PM
Hey,
I created an alv table and I want to colour cell with date that is less before today.
I debugged it but couldn't find the reason why it's not working.
Please see code below:
*&---------------------------------------------------------------------*
*& Include Z8RE_UPDATE_VACA_OPTIONS_FRM
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM get_data.
DATA lt_vacations TYPE tt_vacations.
APPEND VALUE #( zvacation_code = p_code
zstart_date = str_date
zend_date = end_date
babysitting = cb_bbyst
extreme = cb_extrm
tutorials = cb_tutor
) TO gt_vacation.
MODIFY z8re_vacation FROM TABLE gt_vacation.
COMMIT WORK.
SELECT *
FROM z8re_vacation
INTO CORRESPONDING FIELDS OF TABLE @gt_vacations.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form TABLE_DISPLAY
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM table_display .
IF gr_cont IS BOUND.
gr_grid->refresh_table_display( ).
ELSE.
DATA: ls_layout TYPE lvc_s_layo,
lt_fldct TYPE lvc_t_fcat,
lt_exclude TYPE ui_functions.
* lr_event TYPE REF TO lcl_handle_events.
* Create custom container
CREATE OBJECT gr_cont
EXPORTING
container_name = 'CC_CUSTOM'
EXCEPTIONS
OTHERS = 1.
IF sy-subrc NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
* Create alv grid
CREATE OBJECT gr_grid
EXPORTING
i_parent = gr_cont
EXCEPTIONS
OTHERS = 1.
IF sy-subrc NE 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'Z8RE_VACATION_S'
CHANGING
ct_fieldcat = lt_fldct
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc NE 0.
CLEAR lt_fldct[].
ENDIF.
* Field Catalog
LOOP AT lt_fldct ASSIGNING FIELD-SYMBOL(<fs_fldct>).
CASE <fs_fldct>-fieldname.
WHEN 'ZSTART_DATE'.
<fs_fldct>-edit = abap_true.
WHEN 'ZEND_DATE'.
<fs_fldct>-edit = abap_true.
* WHEN 'VKORG' OR 'VTWEG' OR 'OBJNR'.
* <fs_fldct>-tech = abap_true.
ENDCASE.
ENDLOOP.
* Layout
ls_layout-cwidth_opt = abap_true.
* Exclude Toolbar
APPEND cl_gui_alv_grid=>mc_fc_loc_undo TO lt_exclude.
APPEND cl_gui_alv_grid=>mc_fc_check TO lt_exclude.
APPEND cl_gui_alv_grid=>mc_fc_loc_delete_row TO lt_exclude.
APPEND cl_gui_alv_grid=>mc_fc_loc_insert_row TO lt_exclude.
APPEND cl_gui_alv_grid=>mc_fc_loc_append_row TO lt_exclude.
APPEND cl_gui_alv_grid=>mc_fc_loc_copy TO lt_exclude.
APPEND cl_gui_alv_grid=>mc_fc_loc_copy_row TO lt_exclude.
APPEND cl_gui_alv_grid=>mc_fc_loc_cut TO lt_exclude.
APPEND cl_gui_alv_grid=>mc_fc_loc_move_row TO lt_exclude.
APPEND cl_gui_alv_grid=>mc_fc_loc_paste TO lt_exclude.
APPEND cl_gui_alv_grid=>mc_fc_loc_paste_new_row TO lt_exclude.
* Display table
CALL METHOD gr_grid->set_table_for_first_display
EXPORTING
is_layout = ls_layout
it_toolbar_excluding = lt_exclude
CHANGING
it_outtab = gt_vacation
it_fieldcatalog = lt_fldct[]
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4.
gr_grid->register_edit_event( cl_gui_alv_grid=>mc_evt_modified ).
gr_grid->register_edit_event( cl_gui_alv_grid=>mc_evt_enter ).
* CREATE OBJECT lr_event.
* SET HANDLER lr_event->on_data_changed FOR gr_grid.
* SET HANDLER lr_event->on_changed_finished FOR gr_grid.
* Set defaults for ALV
* ls_layout-grid_title = gv_rep_title.
ls_layout-zebra = 'X'.
ls_layout-stylefname = 'CELLTAB'.
ls_layout-info_fname = 'ROWCOLOR'.
ls_layout-ctab_fname = 'COLOR_CELL'.
* g_variant-report = sy-repid.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form SET_CELL_COLOURS
*&---------------------------------------------------------------------*
*& Set colour of individual ALV cell, field
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM set_cell_colours .
*--- Work area declaration
DATA: wa_vacations TYPE tt_vacations,
wa_layout TYPE slis_layout_alv,
wa_cellcolor TYPE lvc_s_scol,
celltab TYPE lvc_t_styl,
rowcolor(4) TYPE c,
color_cell TYPE lvc_t_scol,
lv_date TYPE sy-datum.
* DATA: wa_cellcolor TYPE lvc_s_scol.
* DATA: ld_date TYPE sy-datum.
LOOP AT gt_vacations ASSIGNING FIELD-SYMBOL(<fs_vacations>).
* ld_index = sy-tabix.
* Set colour of ZSTART_DATE field to various colors based on sy-datum value
IF <fs_vacations>-zstart_date < sy-datum.
wa_cellcolor-fname = 'ZSTART_DATE'.
wa_cellcolor-color-col = 4.
wa_cellcolor-color-int = '0'.
wa_cellcolor-color-inv = '0'.
* APPEND wa_cellcolor TO <fs_vacations>-cellcolor.
CLEAR: wa_cellcolor.
ENDIF.
* wa_cellcolor-color-col = sy-tabix. "color code 1-7, if outside rage defaults to 7
* wa_cellcolor-color-int = '1'. "1 = Intensified on, 0 = Intensified off
* wa_cellcolor-color-inv = '0'. "1 = text colour, 0 = background colour
* APPEND wa_cellcolor TO wa_ekko-cellcolor.
* MODIFY gt_vacations FROM gs_vacations INDEX ld_index TRANSPORTING cellcolor.
** Set colour of ZEND_DATE field to color 4 if gt 0
* IF wa_ekko-netpr GT 0.
* wa_cellcolor-fname = 'ZEND_DATE'.
* wa_cellcolor-color-col = 4. "color code 1-7, if outside rage defaults to 7
* wa_cellcolor-color-int = '0'. "1 = Intensified on, 0 = Intensified off
* wa_cellcolor-color-inv = '0'. "1 = text colour, 0 = background colour
* APPEND wa_cellcolor TO wa_ekko-cellcolor.
* MODIFY it_ekko FROM wa_ekko INDEX ld_index TRANSPORTING cellcolor.
* ENDIF.
*
*
** Set colour of AEDAT field text to red(6)
* wa_cellcolor-fname = 'AEDAT'.
* wa_cellcolor-color-col = 6. "color code 1-7, if outside rage defaults to 7
* wa_cellcolor-color-int = '0'. "1 = Intensified on, 0 = Intensified off
* wa_cellcolor-color-inv = '1'. "1 = text colour, 0 = background colour
* APPEND wa_cellcolor TO wa_ekko-cellcolor.
* MODIFY it_ekko FROM wa_ekko INDEX ld_index TRANSPORTING cellcolor.
ENDLOOP.
ENDFORM. " SET_CELL_COLOURS
**************************************************
* SALV SALV SALV SALV SALV SALV
*
**************************************************
*&---------------------------------------------------------------------*
*& Form DISPLAY_SALV_REPORT
*&---------------------------------------------------------------------*
*& Display report using SALV-Factory
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM display_salv_report .
* DATA lt_vaca_options TYPE tt_vaca_options.
*
* CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
* EXPORTING
* i_structure_name = 'Z8RE_VACATION'
* TABLES
* t_outtab = lt_vaca_options
* EXCEPTIONS
* program_error = 1
* OTHERS = 2.
* IF sy-subrc <> 0.
*
* ENDIF.
DATA: lo_alv TYPE REF TO cl_salv_table,
lo_func TYPE REF TO cl_salv_functions,
lo_layo TYPE REF TO cl_salv_layout,
lo_dset TYPE REF TO cl_salv_display_settings,
ls_layout_key TYPE salv_s_layout_key.
* lr_event TYPE REF TO lcl_handle_events.
* ASSIGN pdr_out->* TO <out_tab>.
TRY.
CALL METHOD cl_salv_table=>factory
IMPORTING
r_salv_table = lo_alv
CHANGING
t_table = gt_vacations.
CATCH cx_salv_msg .
MESSAGE 'ALV Error' TYPE 'I'.
RETURN.
ENDTRY.
" handle columns
" functions
lo_func = lo_alv->get_functions( ).
lo_func->set_all( ).
" title + zebra (off)
* lo_dset = lo_alv->get_display_settings( ).
* lo_dset->set_striped_pattern( abap_false ).
* PERFORM alv_set_title USING lo_dset
* ps_period.
" 'save layout' functionality
ls_layout_key-report = sy-repid.
lo_layo = lo_alv->get_layout( ).
lo_layo->set_key( ls_layout_key ).
lo_layo->set_save_restriction( if_salv_c_layout=>restrict_none ).
lo_layo->set_default( abap_true ).
* Event
* CREATE OBJECT lr_event.
* SET HANDLER lr_event->on_link_click FOR lo_alv->get_event( ).
* " display
lo_alv->display( ).
ENDFORM.
2023 Aug 06 3:12 PM
I guess you did not check carefully your code.
So, you want a "color by cell", which is ls_layout-ctab_fname = 'COLOR_CELL'.
So, you should have the component COLOR_CELL of type LVC_T_SCOL in your internal table GT_VACATION.
You fill <fs_vacations>-color_cell with field name and color in each concerned row of GT_VACATION.
CTAB_FNAME must contain the exact name which is in <fs_vacations>.
Make sure by debug that LS_LAYOUT is initialized correctly and the colors are defined, when you call SET_TABLE_FOR_FIRST_DISPLAY.
That's it.
2023 Aug 07 4:56 AM
based on your code, you are calling the set_table_for_first_display first instead of filling the value of the layout workarea first for the ctab_fname, so I guess no wonder your cell color does not work as intended?