Spalten Farbe setzen
Deklaration
CLASS z_color_view DEFINITION.
PUBLIC SECTION.
METHODS set_column_color
IMPORTING
icl_alv TYPE REF TO cl_salv_table
iv_column TYPE lvc_fname
iv_color TYPE int4
iv_intense TYPE int1
iv_inverse TYPE int4 .
ENDCLASS.
Implementation
CLASS z_color_view IMPLEMENTATION.
*----------------------------------------------------------------------*
* Spaltenfarbe setzen
*----------------------------------------------------------------------*
METHOD set_column_color.
DATA: lcl_columns_table TYPE REF TO cl_salv_columns_table,
lcl_column TYPE REF TO cl_salv_column_table,
ls_color TYPE lvc_s_colo.
IF icl_alv IS NOT INITIAL.
TRY.
lcl_columns_table = icl_alv->get_columns( ).
lcl_column ?= lcl_columns_table->get_column( iv_column ).
ls_color-col = iv_color.
ls_color-int = iv_intense.
ls_color-inv = iv_inverse.
IF ls_color IS NOT INITIAL AND
lcl_column IS NOT INITIAL.
lcl_column->set_color( ls_color ).
ENDIF.
CATCH cx_salv_not_found.
ENDTRY.
ENDIF.
ENDMETHOD.
ENDCLASS.
Aufruf
DATA: go_color TYPE REF TO z_color_view.
DATA: go_alv TYPE REF TO cl_salv_table.
*-----------------------------------------------------------------------
* Beginn ALV-Ausgabe
*-----------------------------------------------------------------------
* Kopieren Sie diesen Block an das Endes des Verarbeitungsblocks
* des ABAP-Programms
*-----------------------------------------------------------------------
"**************************************************************************
"* Instanz der Klasse cl_salv_table erzeugen
"**************************************************************************
CREATE OBJECT go_color.
TRY.
cl_salv_table=>factory(
* EXPORTING list_display = if_salv_c_bool_sap=>false
* r_container = cl_gui_custom_container=>default_screen
IMPORTING r_salv_table = go_alv"_detail
CHANGING t_table = gt_detail ).
CATCH cx_salv_msg INTO go_err_salv.
"Fehler anzeigen
gf_string = go_err_salv->get_text( ).
MESSAGE gf_string TYPE 'E'.
ENDTRY.
"**************************************************************************
"* Setze Spalten Farbe
"**************************************************************************
go_color->set_column_color( icl_alv = go_alv
iv_column = 'DOKUMENT'
iv_color = 6
iv_intense = 0
iv_inverse = 0 ).
go_color->set_column_color( icl_alv = go_alv
iv_column = 'BELNR'
iv_color = 6
iv_intense = 0
iv_inverse = 0 ).
"**************************************************************************
"* optimale Spaltenbreite aktivieren
"**************************************************************************
" Instanz für Spalten holen
go_columns = go_alv->get_columns( ).
" Spaltenbreite optimieren
go_columns->set_optimize( abap_true ). " nur ein 'X'
"**************************************************************************
"* Selection-Mode aktivieren
"**************************************************************************
" Instanz des Selection-Objektes holen
go_selections = go_alv->get_selections( ).
" Selektionsmodus setzen
go_selections->set_selection_mode( if_salv_c_selection_mode=>row_column ).
"**************************************************************************
"* Alle Standard-Funktionen auf aktiv setzen
"**************************************************************************
" Instanz für Funktionen holen Funktionstasten (Sortieren, Filtern, Excel-Export etc.)
go_functions = go_alv->get_functions( ).
" Alle Standardfunktionen aktivieren
go_functions->set_all( abap_true ). " nur ein 'X'
"**************************************************************************
"* Spalten Farbe setzen
"**************************************************************************
* go_col->SET_COLOR_COLUMN( value = gs_col ).
"**************************************************************************
"* Alle Standard-Anzeige Möglichkeiten definieren
"**************************************************************************
" Titel und/oder Streifenmuster
go_display = go_alv->get_display_settings( ).
" Titel
CONCATENATE 'Detailsicht von'
gs_cocks-bukrs
gs_cocks-stjahr
gs_cocks-lifnr
gs_cocks-tkonid INTO gf_titel SEPARATED BY space.
go_display->set_list_header( value = gf_titel ).
" Streifenmuster
go_display->set_striped_pattern( abap_true ).
"***************************************************************************
"* Alle Standard-Layout Möglichkeiten definieren
"***************************************************************************
" Instanz des Layouts holen get layout object
go_layout = go_alv->get_layout( ).
" set Layout save restriction
" 1. Set Layout Key .. Unique key identifies the Differenet ALVs
gs_key-report = sy-repid.
go_layout->set_key( gs_key ).
" 2. Remove Save layout the restriction.
go_layout->set_save_restriction( if_salv_c_layout=>restrict_none ).
" Default Layout hinterlegen set initial Layout
gf_variant = '/DEFAULT'.
go_layout->set_initial_layout( gf_variant ).
* "***************************************************************************
* "* Doppelklick Ereigniss einbauen.
* "***************************************************************************
" Ereignisse einlesen und Handler für Doppelklick registrieren
go_events = go_alv->get_event( ).
SET HANDLER lcl_event_handler=>on_double_click_field FOR go_events.
* "**************************************************************************
* "* Toolbar um eigene Funktionen erweitern
* "**************************************************************************
* TRY.
* " Neue Funktion für das ALV vorsehen
* go_functions->add_function(
* name = 'NFUNC'
* icon = '@L1@' " Irgendein ICON aus Tabelle ICON
* text = 'ZHR_LV_COCKPIT_VIEW bearbeiten'
* tooltip = 'Hinzugefügte Funktion'
* position = if_salv_c_function_position=>right_of_salv_functions ).
* CATCH cx_salv_existing INTO go_err_salv_exist.
* " Fehler anzeigen
* gf_string = go_err_salv_exist->get_text( ).
* MESSAGE gf_string TYPE 'E'.
* CATCH cx_salv_wrong_call INTO go_err_wrong_call.
* " Fehler anzeigen
* gf_string = go_err_wrong_call->get_text( ).
* MESSAGE gf_string TYPE 'E'.
* ENDTRY.
"**************************************************************************
"* Ereignis zum Behandeln des Button-Click registrieren
"**************************************************************************
* " Das Objekt für die möglichen ALV-Ereignisse holen
* go_events = go_alv->get_event( ).
* " Objekt für die Ereignisbehandlung erzeugen ...
* CREATE OBJECT go_event_handler.
** EXPORTING
** ir_salv = go_alv
** it_cockpit = gt_cockpit.
*
* " ... und bei den Plegeview aufruf registrieren
* SET HANDLER go_event_handler->handle_toolbar_click FOR go_events.
"**************************************************************************
"* Ampel einbauen
"*************************************************************************
* " Ampel einbauen
* " 1. Hole die Zeilen
* go_columns = go_alv->get_columns( ).
* " 2. Hole sag bei welcher Spalte eine Ampelfunktion hinterlegt werden soll.
* go_columns->set_exception_column( value = 'LIGHT' ).
* TRY.
* go_column ?= go_columns->get_column( 'LIGHT' ).
* go_column->set_medium_text( 'STATUS' ).
* go_column->set_long_text( 'STATUS' ).
* go_column->set_short_text( 'STATUS' ).
* CATCH cx_salv_not_found. "#EC NO_HANDLER
* ENDTRY.
"***************************************************************************
"* Ereignis zum Behandeln des Button-Click registrieren
"***************************************************************************
** Das Objekt für die möglichen ALV-Ereignisse holen
* go_events = go_alv->get_event( ).
** Objekt für die Ereignisbehandlung erzeugen ...
* CREATE OBJECT go_event_handler
* EXPORTING
* cr_salv = go_alv
* it_sflight = gt_sflight.
** ... und bei den ALV-Ereignissen registrieren
* SET HANDLER go_event_handler->handle_toolbar_click FOR go_events.
"**************************************************************************
"* ALV-Anzeige anstoßen
"**************************************************************************
" Liste anzeigen
go_alv->display( ).
"**************************************************************************
"* Toolbar um eigene Funktionen erweitern dafür braucht es ein Container
"**************************************************************************
"Trägerbildschirm" für Container rufen (Sonst findet es keinen Container)
* WRITE: space.
Alle Möglichen Farben
REPORT ZZ_COLOR.
TYPES:
BEGIN OF ts_data,
col TYPE lvc_col, "color 1-7
int TYPE lvc_int, "intensified 0-1
inv TYPE lvc_inv, "inverse 0-1
scol TYPE lvc_t_scol, "table for cell coloring
END OF ts_data,
tt_data TYPE TABLE OF ts_data.
DATA:
ls_data TYPE ts_data,
lt_data TYPE tt_data,
lv_index TYPE i,
ls_color TYPE lvc_s_scol,
lt_color TYPE lvc_t_scol,
lo_table TYPE REF TO cl_salv_table,
lo_columns TYPE REF TO cl_salv_columns_table,
lo_column TYPE REF TO cl_salv_column_list.
* empty row with default color
APPEND ls_data TO lt_data.
* rows with possible colors
DO 7 TIMES.
ls_color-color-col = sy-index. " 1-7
DO 2 TIMES.
lv_index = sy-index - 1.
ls_color-color-int = lv_index. " 0-1
DO 2 TIMES.
lv_index = sy-index - 1.
ls_color-color-inv = lv_index. " 0-1
APPEND ls_color TO lt_color.
ls_data-col = ls_color-color-col.
ls_data-int = ls_color-color-int.
ls_data-inv = ls_color-color-inv.
ls_data-scol = lt_color.
APPEND ls_data TO lt_data.
CLEAR lt_color.
ENDDO.
ENDDO.
ENDDO.
TRY.
CALL METHOD cl_salv_table=>factory
IMPORTING
r_salv_table = lo_table
CHANGING
t_table = lt_data.
lo_columns = lo_table->get_columns( ).
* set descriptions of columns
lo_column ?= lo_columns->get_column( 'COL' ).
lo_column->set_short_text( 'COL' ).
lo_column ?= lo_columns->get_column( 'INT' ).
lo_column->set_short_text( 'INT' ).
lo_column ?= lo_columns->get_column( 'INV' ).
lo_column->set_short_text( 'INV' ).
* set the column with the information about colors of rows and fields
lo_columns->set_color_column( 'SCOL' ).
lo_table->display( ).
CATCH cx_salv_msg. " cl_salv_table=>factory
WRITE: / 'cx_salv_msg exception'.
STOP.
CATCH cx_salv_data_error. " cl_salv_filters->add_filter()
WRITE: / 'cx_salv_data_error'.
STOP.
CATCH cx_salv_not_found. " cl_salv_columns_table->get_column()
WRITE: / 'cx_salv_not_found exception'.
STOP.
ENDTRY.