*&---------------------------------------------------------------------* *& Report Name : /EUTIN/DEMO_ALV_GRID *& Title : Demo: ALV Grid (Templates) *& Developer : Michael Eutin - 27.06.2025 *& Development Class : /EUTIN/TOOLS *& Description : *& Display content of internal table using ALV grid display *& *&---------------------------------------------------------------------* *& CHANGE HISTORY *&---------------------------------------------------------------------* *& Date Description *&---------------------------------------------------------------------* *& *& *& *&---------------------------------------------------------------------* REPORT /eutin/demo_alv_grid NO STANDARD PAGE HEADING. *---------------------------------------------------------------------- *--- Definition of local classes *---------------------------------------------------------------------- *---------------------------------------------------------------------- *--- CLASS lcl_alv *---------------------------------------------------------------------- CLASS lcl_alv DEFINITION FINAL. PUBLIC SECTION. CLASS-METHODS alv_display IMPORTING iv_title TYPE lvc_title OPTIONAL CHANGING ct_table TYPE STANDARD TABLE. ENDCLASS. "CLASS lcl_alv DEFINITION *----------------------------------------------------------------------* * CLASS lcl_alv IMPLEMENTATION *----------------------------------------------------------------------* * *----------------------------------------------------------------------* CLASS lcl_alv IMPLEMENTATION. * ---------------------------------------------------------------------------------------+ * | Instance Public Method ALV_DISPLAY * +-------------------------------------------------------------------------------------------------+ * | [--->] IT_TABLE TYPE STANDARD TABLE * +-------------------------------------------------------------------------------------- METHOD alv_display. DATA: lo_alv_table TYPE REF TO cl_salv_table, lo_column_table TYPE REF TO cl_salv_column_table, lo_tabledescr TYPE REF TO cl_abap_tabledescr, lo_structdescr TYPE REF TO cl_abap_structdescr, lx_salv_msg TYPE REF TO cx_salv_msg, lx_salv_not_found TYPE REF TO cx_salv_not_found, lx_salv_data_error TYPE REF TO cx_salv_data_error, lx_salv_existing TYPE REF TO cx_salv_existing, lx_root TYPE REF TO cx_root, * lt_componentdescr TYPE STANDARD TABLE OF abap_componentdescr WITH DEFAULT KEY, lv_error TYPE string, lv_header_count TYPE c LENGTH 20, lv_header_text TYPE string, lv_columnname TYPE lvc_fname, lv_scrtext_s TYPE scrtext_s, lv_scrtext_m TYPE scrtext_m, lv_scrtext_l TYPE scrtext_l. * FIELD-SYMBOLS: * LIKE LINE OF lt_componentdescr. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - IF ct_table[] IS INITIAL. *--- do not display empty table RETURN. ENDIF. TRY. *--- create ALV table cl_salv_table=>factory( IMPORTING r_salv_table = lo_alv_table CHANGING t_table = ct_table[] ). *--- activate all standard functions DATA(lo_functions_list) = lo_alv_table->get_functions( ). lo_functions_list->set_all( if_salv_c_bool_sap=>true ). *--- set optimized column size DATA(lo_columns_table) = lo_alv_table->get_columns( ). lo_columns_table->set_optimize( if_salv_c_bool_sap=>true ). *--- set zebra layout DATA(lo_display_settings) = lo_alv_table->get_display_settings( ). lo_display_settings->set_striped_pattern( if_salv_c_bool_sap=>true ). *--- set title IF iv_title IS NOT INITIAL. lo_display_settings->set_list_header( iv_title ). ENDIF. *--- set header and display number of displayed entries DESCRIBE TABLE ct_table LINES DATA(lv_counts). WRITE lv_counts TO lv_header_count LEFT-JUSTIFIED. CONCATENATE 'Number of entries:'(001) lv_header_count INTO lv_header_text SEPARATED BY space. DATA(lo_header_info) = NEW cl_salv_form_header_info( text = lv_header_text ). IF lo_header_info IS BOUND. lo_alv_table->set_top_of_list( EXPORTING value = lo_header_info ). ENDIF. *--- populate column header (short/medium/long) with technical field name lo_tabledescr ?= cl_abap_tabledescr=>describe_by_data( ct_table ). lo_structdescr ?= lo_tabledescr->get_table_line_type( ). IF lo_structdescr IS BOUND. data(lt_componentdescr) = lo_structdescr->get_components( ). LOOP AT lt_componentdescr ASSIGNING FIELD-SYMBOL(). lv_columnname = -name. lv_scrtext_s = -name. lv_scrtext_m = -name. lv_scrtext_l = -name. lo_column_table ?= lo_columns_table->get_column( lv_columnname ). lo_column_table->set_short_text( lv_scrtext_s ). lo_column_table->set_medium_text( lv_scrtext_m ). lo_column_table->set_long_text( lv_scrtext_l ). ENDLOOP. ENDIF. *--- display table lo_alv_table->display( ). *--- error handling CATCH cx_salv_msg INTO lx_salv_msg. CLEAR lv_error. lv_error = lx_salv_msg->if_message~get_text( ). IF lv_error IS NOT INITIAL. MESSAGE lv_error TYPE 'I' DISPLAY LIKE 'E'. ENDIF. MESSAGE 'METH DISPLAY: Exception CX_SALV_MSG was caught' TYPE 'E' ##NO_TEXT. "pragma or pseudo comment NO_TEXT: Okay, technical error message CATCH cx_salv_not_found INTO lx_salv_not_found. CLEAR lv_error. lv_error = lx_salv_not_found->if_message~get_text( ). IF lv_error IS NOT INITIAL. MESSAGE lv_error TYPE 'I' DISPLAY LIKE 'E'. ENDIF. MESSAGE 'METH DISPLAY: Exception CX_SALV_NOT_FOUND was caught' TYPE 'E' ##NO_TEXT. "pragma or pseudo comment NO_TEXT: Okay, technical error message CATCH cx_salv_data_error INTO lx_salv_data_error. CLEAR lv_error. lv_error = lx_salv_data_error->if_message~get_text( ). IF lv_error IS NOT INITIAL. MESSAGE lv_error TYPE 'I' DISPLAY LIKE 'E'. ENDIF. MESSAGE 'METH DISPLAY: Exception CX_SALV_DATA_ERROR was caught' TYPE 'E' ##NO_TEXT. "pragma or pseudo comment NO_TEXT: Okay, technical error message CATCH cx_salv_existing INTO lx_salv_existing. CLEAR lv_error. lv_error = lx_salv_existing->if_message~get_text( ). IF lv_error IS NOT INITIAL. MESSAGE lv_error TYPE 'I' DISPLAY LIKE 'E'. ENDIF. MESSAGE 'METH DISPLAY: Exception CX_SALV_EXISTING was caught' TYPE 'E' ##NO_TEXT. "pragma or pseudo comment NO_TEXT: Okay, technical error message CATCH cx_root INTO lx_root. "#EC CATCH_ALL *--- ATC message: Every class-based exception is found. *--- Finding can be suppressed with pseudo comment "#EC CATCH_ALL *--- okay, one to catch them all! ==> error tolerant mode: do not cause catchable runtime errors! CLEAR lv_error. lv_error = lx_root->if_message~get_text( ). IF lv_error IS NOT INITIAL. MESSAGE lv_error TYPE 'I' DISPLAY LIKE 'E'. ENDIF. MESSAGE 'METH DISPLAY: Exception CX_ROOT was caught' TYPE 'E' ##NO_TEXT. "pragma or pseudo comment NO_TEXT: Okay, technical error message ENDTRY. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ENDMETHOD. "METHOD alv_display ENDCLASS. "CLASS lcl_alv IMPLEMENTATION START-OF-SELECTION. PERFORM main. *&---------------------------------------------------------------------* *& Form MAIN *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* FORM main. TYPES: BEGIN OF ts_alv, seqno TYPE n LENGTH 3, msgnr TYPE t100-msgnr, text TYPE t100-text, END OF ts_alv. DATA: lt_t100 TYPE STANDARD TABLE OF t100 WITH DEFAULT KEY, lt_alv TYPE STANDARD TABLE OF ts_alv WITH DEFAULT KEY, ls_alv LIKE LINE OF lt_alv, lv_title TYPE lvc_title. FIELD-SYMBOLS: LIKE LINE OF lt_t100. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - *--- select all messages of SAP standard message class S# CLEAR: lt_t100[], lt_alv[]. SELECT * INTO TABLE lt_t100 FROM t100 "#EC CI_SGLSELECT *--- ATC message: Access to single record buffered table T100 cannot use buffer *--- Finding can be suppressed with pseudo comment "#EC CI_GENBUFF or "#EC CI_SGLSELECT WHERE sprsl EQ sy-langu AND arbgb EQ 'S#' ORDER BY PRIMARY KEY. IF sy-subrc NE 0. MESSAGE 'Nothing found'(002) TYPE 'S' DISPLAY LIKE 'W'. RETURN. ENDIF. *--- sort in correct order by MSGNR ascending and fill internal field SEQNR with sy-tabix SORT lt_t100 ASCENDING BY msgnr. LOOP AT lt_t100 ASSIGNING . ls_alv-seqno = sy-tabix. ls_alv-msgnr = -msgnr. ls_alv-text = -text. APPEND ls_alv TO lt_alv. ENDLOOP. CLEAR lt_t100[]. *--- display selected entries in ALV grid lv_title = 'Content of Table T100 with Message Class "S#"'(003). lcl_alv=>alv_display( EXPORTING iv_title = lv_title CHANGING ct_table = lt_alv[] ). * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ENDFORM. *--- The End!