You are on page 1of 11

* _______ _______ _______ _______ _______ _______ ______ _______ _______

______ _______
* | _ | _ | _ | | | _ | || _ | _ |
_ | | _ |
* | |_| | |_| | |_| | _ | | |_| | _ | |_| | |_| |
| || | |_| |
* | | | | |_| | | | | | | | |
|_||_| |
* | | _ || | ___| _| | |_| | | _ ||
__ | |
* | _ | |_| | _ | | | |_| _ | | _ | |_| |
| | | _ |
* |__| |__|_______|__| |__|___| |_______|__| |__|______||__| |__|_______|
___| |_|__| |__|
*
www.abapcadabra.com

*---------------------------------------------------------------------
* include : ZABAPCADABRA_EASY_ALV
* holding : Local LCL_EASY_ALV class definition & implementation
* title : Easy ALV through lcl_easy_alv
* functional area : Cross modules
* environment : 4.7
* program Function : This local class can be included in any ABAP report
* and supports easy access to the standard SAP ALV
* suite of functionality. It's a wrapper class that
* sweetens the implementation of ALV reports.
* Documentation : Search for "Easy ALV" on AbapcadabrA.com
* There are simple "to-go" examples on ALV matters such
* as Field colors, Icons, Respond to field clicks and
* much more - using this local class.
* Previous version : This is the initial version
* Developer name : Wim Maasdam
* Development date : 18/11/2015
* Version : 0.1
*---------------------------------------------------------------------

*---------------------------------------------------------------------
* C L A S S D E F I N I T I O N
*---------------------------------------------------------------------
CLASS lcl_easy_alv DEFINITION.

PUBLIC SECTION.
CLASS-DATA:
gw_colored_field type lvc_s_scol ##NEEDED.
DATA:
RF_SELECTIONS type ref to CL_SALV_SELECTIONS,
RF_DISPLAY_SETTINGS type ref to CL_SALV_DISPLAY_SETTINGS,
RF_COLUMNS_TABLE type ref to CL_SALV_COLUMNS_TABLE,
RF_COLUMN_TABLE type ref to CL_SALV_COLUMN_TABLE,
RF_FUNCTIONS_LIST type ref to CL_SALV_FUNCTIONS_LIST,
RF_FUNCTIONAL_SETTINGS type ref to CL_SALV_FUNCTIONAL_SETTINGS,
RF_SORTS type ref to CL_SALV_SORTS,
RF_LAYOUT type ref to CL_SALV_LAYOUT,
* Freely usable custom container: if your ALV lives in part of a screen
RF_custom_container type ref to cl_gui_custom_container.

METHODS:
SET_FIELD
IMPORTING
FIELDNAME type string
IS_KEY type boolean OPTIONAL
IS_HIDDEN type boolean OPTIONAL
IS_UNAVAILABLE type boolean OPTIONAL
IS_ICON type boolean OPTIONAL
IS_HOTSPOT type boolean OPTIONAL
IS_CHECKBOX type boolean OPTIONAL
IS_BUTTON type boolean OPTIONAL
IS_EXCEPTION type boolean OPTIONAL
TOOLTIP type any OPTIONAL
DESCRIPTION_OVERALL type any OPTIONAL
DESCRIPTION_SHORT type any OPTIONAL
DESCRIPTION_MEDIUM type any OPTIONAL
DESCRIPTION_LONG type any OPTIONAL
LEADING_ZERO type boolean OPTIONAL
ZERO_VALUES type boolean OPTIONAL
DDIC_REFERENCE type string OPTIONAL
OUTPUT_LENGTH type LVC_OUTLEN OPTIONAL
POSITION type I OPTIONAL
ALIGNMENT type I OPTIONAL
QUANTITY_COLUMN type LVC_QFNAME OPTIONAL
CURRENCY_COLUMN type LVC_QFNAME OPTIONAL
DECIMAL_PLACES type LVC_DECMLS OPTIONAL,
SET_ALV_FROM_TEMPLATE
IMPORTING
CONTAINER type ref to CL_GUI_CUSTOM_CONTAINER OPTIONAL
TITLE type LVC_TITLE OPTIONAL
MENU_REPORT type SYREPID OPTIONAL
MENU_PFSTATUS type SYPFKEY OPTIONAL
SKIP_VARIANT_SUPPORT type BOOLEAN DEFAULT abap_false
CHANGING
CONTENT type TABLE
SALV_TABLE type ref to CL_SALV_TABLE OPTIONAL,
DISPLAY
IMPORTING
TITLE type ANY OPTIONAL
REFRESH type BOOLEAN DEFAULT abap_false,
SET_SORT
IMPORTING
FIELDNAME type string
DESCENDING type boolean DEFAULT abap_false
SUBTOTAL type boolean DEFAULT abap_false,
SET_LAYOUT_PROTECTION
IMPORTING
VALUE type BOOLEAN DEFAULT abap_true,
SET_NO_BUTTONS,
SET_HEADER_LINE
IMPORTING
COL1 type any OPTIONAL
COL2 type any OPTIONAL
COL3 type any OPTIONAL
COL4 type any OPTIONAL
COL5 type any OPTIONAL
COL6 type any OPTIONAL
RESET_HEADER type boolean DEFAULT ABAP_FALSE
PREFERRED PARAMETER COL1
RETURNING VALUE(HEADER) TYPE REF TO CL_SALV_FORM_LAYOUT_GRID,
SET_FOOTER_LINE
IMPORTING
COL1 type any OPTIONAL
COL2 type any OPTIONAL
COL3 type any OPTIONAL
COL4 type any OPTIONAL
COL5 type any OPTIONAL
COL6 type any OPTIONAL
RESET_FOOTER type boolean DEFAULT ABAP_FALSE
WITH_GRID_LINES type boolean DEFAULT ABAP_FALSE
PREFERRED PARAMETER COL1
RETURNING VALUE(FOOTER) TYPE REF TO CL_SALV_FORM_LAYOUT_GRID,
ADD_GENERAL_TOOLTIP
IMPORTING
TYPE type SALV_DE_CONSTANT DEFAULT CL_SALV_TOOLTIP=>C_TYPE_ICON
VALUE type ANY
TOOLTIP type ANY OPTIONAL,
SET_SELECTION_MODE
IMPORTING
VALUE type I DEFAULT 0.

PROTECTED SECTION.
DATA: RF_ALV_TABLE type ref to CL_SALV_TABLE.

PRIVATE SECTION.
DATA:
RF_ALV_HEADER type ref to CL_SALV_FORM_LAYOUT_GRID,
VA_ALV_HEADER_LINE type SYINDEX,
RF_ALV_FOOTER type ref to CL_SALV_FORM_LAYOUT_GRID,
VA_ALV_FOOTER_LINE type SYINDEX,
RF_TABLE_CONTENT type ref to DATA ##NEEDED.

ENDCLASS.

*---------------------------------------------------------------------
* C L A S S I M P L E M E N T A T I O N
*---------------------------------------------------------------------
CLASS lcl_easy_alv IMPLEMENTATION.

METHOD SET_FIELD .
* This method allows changes to the columns on the ALV report. An overview:
* IS_KEY - determines whether the field is a key field (placed up front)
* IS_HIDDEN - the field is not on the initial display, but the end user can
* add it to the report
* IS_UNAVAILABLE - the field is a technical field, not on display and can
* also not be added to the report
* TOOLTIP - bit of information that shows up when the mouse is hovering
over
* the column header name
* DESCRIPTION_OVERALL - the description that will be used for the short,
* medium as well as the long description. Please note: these descriptions
* are defaulted from the data dictionary, thus the description should
only
* be changed if it does not show correctly on the ALV report.
* DESCRIPTION_SHORT / _MEDIUM / _LONG - individual descriptions
* LEADING_ZERO - to make the report show leading zero's for the column
* ZERO_VALUES - for numeric columns: indicate that 0 should not be
displayed
* DDIC_REFERENCE - change the description and other properties of a field
* by setting the DDIC_REFERENCE to e.g. 'MARA-MATNR' (tablename hyphen
* fieldname).
* IS_ICON - the field holds an ICON code such as @AB@ which is recognized
* by the ALV. This setting allows usage of tooltip functionality on
* the level of reported lines.
* IS_HOTSPOT - the field (whole column) value is underlined and the report
* will respond to a a single click
* IS_EXCEPTION - the field holds a code for the exception or status setup
* which is defaulted to group 1 (traffic light icons, value 0 to 3).
Tooltip
* texts can also be made available for this.
* POSITION - when a column is not in the right location it can be
* relocated by setting the column position.
DATA: lva_scrtext_s TYPE scrtext_s,
lva_scrtext_m TYPE scrtext_m,
lva_scrtext_l TYPE scrtext_l,
lva_fieldname TYPE lvc_fname,
lva_tooltip TYPE lvc_tip,
lwa_ddic_reference TYPE salv_s_ddic_reference,
lva_hidden_flag TYPE sap_bool.

CHECK me->rf_alv_table IS BOUND.

TRY.
lva_fieldname = fieldname.
SET LOCALE LANGUAGE sy-langu.
TRANSLATE lva_fieldname TO UPPER CASE.

IF position IS SUPPLIED.
rf_columns_table->set_column_position( columnname = lva_fieldname
position = position ).
ENDIF.
rf_column_table ?= rf_columns_table->get_column( lva_fieldname ).
IF is_hidden IS SUPPLIED.
IF ( is_hidden = abap_true ).
lva_hidden_flag = abap_false.
ELSE.
lva_hidden_flag = abap_true.
ENDIF.
rf_column_table->set_visible( lva_hidden_flag ).
ENDIF.
IF is_key IS SUPPLIED.
rf_column_table->set_key( is_key ).
ENDIF.
IF is_icon IS SUPPLIED.
rf_column_table->set_icon( is_icon ).
ENDIF.
IF is_hotspot IS SUPPLIED.
IF is_hotspot = abap_true.
rf_column_table->set_cell_type( if_salv_c_cell_type=>hotspot ).
ELSE.
rf_column_table->set_cell_type( if_salv_c_cell_type=>text ).
ENDIF.
ENDIF.
IF is_checkbox IS SUPPLIED.
IF is_checkbox = abap_true.
rf_column_table->set_cell_type( if_salv_c_cell_type=>checkbox ).
ELSE.
rf_column_table->set_cell_type( if_salv_c_cell_type=>text ).
ENDIF.
ENDIF.
IF is_button IS SUPPLIED.
IF is_button = abap_true.
rf_column_table->set_cell_type( if_salv_c_cell_type=>button ).
ENDIF.
ENDIF.
IF is_exception IS SUPPLIED.
rf_columns_table->set_exception_column( value = lva_fieldname group
= '1' ).
ENDIF.

CLEAR: lva_scrtext_s, lva_scrtext_m, lva_scrtext_l.


IF description_overall IS SUPPLIED.
lva_scrtext_s = description_overall.
lva_scrtext_m = description_overall.
lva_scrtext_l = description_overall.
ENDIF.
IF description_short IS SUPPLIED.
lva_scrtext_s = description_short.
ENDIF.
IF description_medium IS SUPPLIED.
lva_scrtext_m = description_medium.
ENDIF.
IF description_long IS SUPPLIED.
lva_scrtext_l = description_long.
ENDIF.
IF NOT lva_scrtext_s IS INITIAL.
rf_column_table->set_short_text( lva_scrtext_s ).
ENDIF.
IF NOT lva_scrtext_m IS INITIAL.
rf_column_table->set_medium_text( lva_scrtext_m ).
ENDIF.
IF NOT lva_scrtext_l IS INITIAL.
rf_column_table->set_long_text( lva_scrtext_l ).
ENDIF.
IF tooltip <> space.
lva_tooltip = tooltip.
rf_column_table->set_tooltip( lva_tooltip ).
ENDIF.
IF leading_zero <> space.
rf_column_table->set_leading_zero( abap_true ).
ENDIF.
IF zero_values is supplied.
rf_column_table->set_zero( zero_values ).
ENDIF.
IF ddic_reference IS SUPPLIED.
SPLIT ddic_reference AT '-' INTO
lwa_ddic_reference-table lwa_ddic_reference-field.
rf_column_table->set_ddic_reference( lwa_ddic_reference ).
ENDIF.
IF quantity_column IS SUPPLIED.
rf_column_table->set_quantity_column( quantity_column ).

ENDIF.

IF currency_column IS SUPPLIED.
rf_column_table->set_currency_column( currency_column ).

ENDIF.

IF is_unavailable IS SUPPLIED.
rf_column_table->set_technical( is_unavailable ).
ELSE.
* Default setting: the field should be marked as non technical
rf_column_table->set_technical( abap_false ).
ENDIF.
IF alignment IS SUPPLIED.
rf_column_table->set_alignment( alignment ).
ENDIF.
IF output_length IS SUPPLIED.
rf_column_table->set_output_length( output_length ).
ENDIF.
if decimal_places IS SUPPLIED.
rf_column_table->set_decimals( decimal_places ).
endif.

CATCH cx_salv_not_found
cx_salv_data_error.
ENDTRY.

ENDMETHOD.

method SET_ALV_FROM_TEMPLATE .
* The ALV report is linked to a (single) internal table with data
* which is linked to the ALV class via this method. When the data
* table is altered after linking it to the class, the changes are
* picked up automatically (the display( ) method has a refresh
* option).
data: lrf_exception type ref to cx_salv_msg ##NEEDED,
lva_container_name type string,
lva_layout_key type salv_s_layout_key.

* Creates an instance of the ALV table object


try.
if container is bound.
lva_container_name = container->get_name( ).
cl_salv_table=>factory( exporting r_container = container
container_name =
lva_container_name
importing r_salv_table = rf_alv_table
changing t_table = content ).
else.
cl_salv_table=>factory( importing r_salv_table = rf_alv_table
changing t_table = content ).
endif.
* Hang on to a reference of the data, used in method DOWNLOAD.
get reference of content into me->RF_TABLE_CONTENT.

catch cx_salv_msg into lrf_exception.


* The wrapper class does not raise the exception, to avoid having to
* catch it.
endtry.

* Set up General Display settings - striped display


rf_display_settings = rf_alv_table->get_display_settings( ).
rf_display_settings->set_striped_pattern( abap_true ).
if not title is initial.
rf_display_settings->set_list_header( title ).
endif.

* Set columns settings, - optimize width - remove column MANDT


rf_columns_table = rf_alv_table->get_columns( ).
rf_columns_table->set_optimize( abap_true ).

* Remove the MANDT field from the ALV reporting output


try.
rf_column_table ?= rf_columns_table->get_column( 'MANDT' ).
rf_column_table->set_technical( abap_true ).
catch cx_salv_not_found.
endtry.

* Set the (fullscreen mode only) menu


if not menu_report is initial and not menu_pfstatus is initial.
* Note - the user interface MAIN_MENU on this program was copied from
* SAPLSLVC_FULLSCREEN / STANDARD_FULLSCREEN. If you are using the class
* in non full-screen mode, a function can be added to the toolbar with
* ADD_FUNCTION (class CL_SALV_FUNCTIONS).
rf_alv_table->set_screen_status( report = menu_report pfstatus =
menu_pfstatus ).
endif.

* Set the functions


rf_functions_list = rf_alv_table->get_functions( ).
rf_functions_list->set_all( ).

* Layout's are available for the end user to control


if skip_variant_support = abap_false.
rf_layout = rf_alv_table->get_layout( ).
lva_layout_key-report = sy-cprog.
lva_layout_key-logical_group = 'BREZ'.
* The key for layouts needs to be set to enable it's functionality.
rf_layout->set_key( lva_layout_key ).
rf_layout->set_default( abap_true ).
rf_layout-
>set_save_restriction( if_salv_c_layout=>RESTRICT_USER_DEPENDANT ).
endif.

salv_table = rf_alv_table.

endmethod.

method DISPLAY .
data: lv_title type LVC_TITLE.
* When the ALV report is ready to be displayed, this method will
* compose the ALV and display it full screen.
check me->rf_alv_table is bound.

if rf_alv_header is bound.
rf_alv_table->set_top_of_list( rf_alv_header ).
rf_alv_table->set_top_of_list_print( rf_alv_header ).
endif.
if rf_alv_footer is bound.
rf_alv_table->set_end_of_list( rf_alv_footer ).
rf_alv_table->set_end_of_list_print( rf_alv_footer ).
endif.

if not title is initial.


lv_title = title.
rf_display_settings->set_list_header( lv_title ).
endif.

if refresh = abap_true.
rf_alv_table->refresh( ).
else.
rf_alv_table->display( ).
endif.

endmethod.

method SET_SORT .
* Set a sort field, also allows subtotalling to be switched on. The
* latter will only have effect when set_aggregate( ) is also used.
* Add several sort fields by calling this method several times.
data: lva_fieldname type lvc_fname,
lva_sort_sequence TYPE SALV_DE_SORT_SEQUENCE.

check me->rf_alv_table is bound.


lva_fieldname = fieldname.
if rf_sorts is not bound.
rf_sorts = rf_alv_table->get_sorts( ).
endif.
try.
if descending = abap_false.
lva_sort_sequence = IF_SALV_C_SORT=>SORT_UP.
else.
lva_sort_sequence = IF_SALV_C_SORT=>SORT_DOWN.
endif.

rf_sorts->add_sort(
columnname = lva_fieldname
subtotal = subtotal
sequence = lva_sort_sequence ).
set_field( fieldname ).
catch cx_salv_not_found
cx_salv_existing
cx_salv_data_error.
endtry.

endmethod.

method SET_LAYOUT_PROTECTION .
* In some reports the sequence in which the records are displayed should
* not be changed. This method can be used to remove the sort and filter
* buttons from available ALV functions.
data: lva_not_value type sap_bool.

check me->rf_alv_table is bound.


if value = abap_true.
lva_not_value = abap_false.
else.
lva_not_value = abap_true.
endif.
rf_functions_list->set_group_sort( lva_not_value ).
rf_functions_list->set_group_filter( lva_not_value ).

endmethod.

method SET_NO_BUTTONS .

data: lrf_functions_list type ref to cl_salv_functions_list.


* Remove menu from the detail ALV only =>
lrf_functions_list = me->RF_ALV_TABLE->get_functions( ).
lrf_functions_list->set_all( abap_false ).

endmethod.

method SET_HEADER_LINE .
* The ALV report supports headers, a block of text that is presented on
* every ALV screen. The block consists of several lines and each line
* has up to 6 columns. This method add's a line to the header.
* NOTE: headers and footers do not work in custom controls (dynpro)
data: lrf_flow type ref to cl_salv_form_layout_flow.

check me->rf_alv_table is bound.


if reset_header = abap_true.
clear rf_alv_header.
clear va_alv_header_line.
endif.
if rf_alv_header is not bound.
create object rf_alv_header.
endif.
add 1 to va_alv_header_line.
if col1 is supplied.
lrf_flow = rf_alv_header->create_flow( row = va_alv_header_line column
= 1 ).
lrf_flow->create_text( text = col1 ).
endif.
if col2 is supplied.
lrf_flow = rf_alv_header->create_flow( row = va_alv_header_line column
= 2 ).
lrf_flow->create_text( text = col2 ).
endif.
if col3 is supplied.
lrf_flow = rf_alv_header->create_flow( row = va_alv_header_line column
= 3 ).
lrf_flow->create_text( text = col3 ).
endif.
if col4 is supplied.
lrf_flow = rf_alv_header->create_flow( row = va_alv_header_line column
= 4 ).
lrf_flow->create_text( text = col4 ).
endif.
if col5 is supplied.
lrf_flow = rf_alv_header->create_flow( row = va_alv_header_line column
= 5 ).
lrf_flow->create_text( text = col5 ).
endif.
if col6 is supplied.
lrf_flow = rf_alv_header->create_flow( row = va_alv_header_line column
= 6 ).
lrf_flow->create_text( text = col6 ).
endif.

header = rf_alv_header.

endmethod.

method SET_FOOTER_LINE .
* As with the header - this method adds a line to the footer of the ALV
* Please note: if the value is numeric, it will be displayed right-aligned.
data: lrf_text type ref to cl_salv_form_text,
lrf_layout_data type ref to cl_salv_form_layout_data_grid,
lrf_str type string.

check me->rf_alv_table is bound.


if reset_footer = abap_true.
clear rf_alv_footer.
clear va_alv_footer_line.
endif.
if rf_alv_footer is not bound.
create object rf_alv_footer.
if with_grid_lines = abap_true.
rf_alv_footer->set_grid_lines( value =
if_salv_form_c_grid_lines=>lines ).
endif.
endif.
add 1 to va_alv_footer_line.
if col1 is supplied.
* lrf_grid = rf_alv_footer->create_grid( row = va_alv_footer_line
column = 1 ).
lrf_text = rf_alv_footer->create_text( text = col1 row =
va_alv_footer_line column = 1 ).
move col1 to lrf_str.
if ( lrf_str co '01234567890., ' ).
lrf_layout_data ?= lrf_text->get_layout_data( ).
lrf_layout_data->set_h_align( if_salv_form_c_h_align=>right ).
endif.
endif.
if col2 is supplied.
lrf_text = rf_alv_footer->create_text( text = col2 row =
va_alv_footer_line column = 2 ).
move col2 to lrf_str.
if ( lrf_str co '01234567890., ' ).
lrf_layout_data ?= lrf_text->get_layout_data( ).
lrf_layout_data->set_h_align( if_salv_form_c_h_align=>right ).
endif.
endif.
if col3 is supplied.
rf_alv_footer->create_text( text = col3 row = va_alv_footer_line
column = 3 ).
move col3 to lrf_str.
if ( lrf_str co '01234567890., ' ).
lrf_layout_data ?= lrf_text->get_layout_data( ).
lrf_layout_data->set_h_align( if_salv_form_c_h_align=>right ).
endif.
endif.
if col4 is supplied.
rf_alv_footer->create_text( text = col4 row = va_alv_footer_line
column = 4 ).
move col4 to lrf_str.
if ( lrf_str co '01234567890., ' ).
lrf_layout_data ?= lrf_text->get_layout_data( ).
lrf_layout_data->set_h_align( if_salv_form_c_h_align=>right ).
endif.
endif.
if col5 is supplied.
rf_alv_footer->create_text( text = col5 row = va_alv_footer_line
column = 5 ).
move col5 to lrf_str.
if ( lrf_str co '01234567890., ' ).
lrf_layout_data ?= lrf_text->get_layout_data( ).
lrf_layout_data->set_h_align( if_salv_form_c_h_align=>right ).
endif.
endif.
if col6 is supplied.
rf_alv_footer->create_text( text = col6 row = va_alv_footer_line
column = 6 ).
move col6 to lrf_str.
if ( lrf_str co '01234567890., ' ).
lrf_layout_data ?= lrf_text->get_layout_data( ).
lrf_layout_data->set_h_align( if_salv_form_c_h_align=>right ).
endif.
endif.
footer = rf_alv_footer.

endmethod.

method ADD_GENERAL_TOOLTIP .
data: lrf_tooltips type ref to cl_salv_tooltips,
lva_value type lvc_value,
lva_tooltip type lvc_tip.

if rf_functional_settings is not bound.


rf_functional_settings = rf_alv_table->get_functional_settings( ).
endif.
lrf_tooltips = rf_functional_settings->get_tooltips( ).
lva_value = value.
lva_tooltip = tooltip.
try.
lrf_tooltips->add_tooltip( type = type value = lva_value tooltip =
lva_tooltip ).
catch cx_salv_existing.
endtry.

endmethod.

method SET_SELECTION_MODE .
* Protected method: call from local class to use. This method
* can be used to control how line/column selections should work.
* Check interface IF_SALV_C_SELECTION_MODE for possible values:
* 0=No selection, 1=Individual selection, 2=Mult.selection
* 3= Cell selection and 4=Line and Column Selection
check me->rf_alv_table is bound.
if rf_selections is not bound.
rf_selections = rf_alv_table->get_selections( ).
endif.
rf_selections->set_selection_mode( value ).

endmethod.

ENDCLASS.

* _______ _______ _______ _______ _______ _______ ______ _______ _______


______ _______
* | _ | _ | _ | | | _ | || _ | _ |
_ | | _ |
* | |_| | |_| | |_| | _ | | |_| | _ | |_| | |_| |
| || | |_| |
* | | | | |_| | | | | | | | |
|_||_| |
* | | _ || | ___| _| | |_| | | _ ||
__ | |
* | _ | |_| | _ | | | |_| _ | | _ | |_| |
| | | _ |
* |__| |__|_______|__| |__|___| |_______|__| |__|______||__| |__|_______|
___| |_|__| |__|
*
www.abapcadabra.com

You might also like