ALV reort using CL_SALV_TABLE

 REPORT ztest_pr.

 TABLES: afko.
 TYPES: BEGIN OF ty_final,
          aufnr TYPE afko-aufnr,
          matnr TYPE resb-matnr,
          auart TYPE aufk-auart,
          abjnr TYPE aufk-objnr,
          aufpl TYPE afvc-aufpl,
          aplzl TYPE afvc-aplzl,
          vornr TYPE afvc-vornr,
          arbid TYPE afvc-arbid,
          steus TYPE afvc-steus,
          objnr TYPE afvc-objnr,
          stext TYPE bsvx-sttxt,
        END OF ty_final.

 DATA: lt_final TYPE TABLE OF ty_final,
             ls_final TYPE ty_final.
 DATA: lv_stext LIKE bsvx-sttxt.
 
SELECT-OPTIONS: s_aufnr FOR afko-aufnr.

********************************************************
* Class Definition
********************************************************
 CLASS lc_salv DEFINITION.
   PUBLIC SECTION.
     METHODS: fetch_data,
              create_salv_obj RAISING cx_static_check ,
              display_toolbar,
              set_layout,
              set_column_attr RAISING cx_static_check,
              event_handle_click FOR EVENT double_click OF cl_salv_events_table
                  IMPORTING row column,
              event_handle_link FOR EVENT link_click OF cl_salv_events_table
                  IMPORTING row column,
              set_events,
              create_header_obj,
              display_output.
   PROTECTED SECTION.
     DATA: lo_salv   TYPE REF TO cl_salv_table,
           lo_column TYPE REF TO cl_salv_column_table,
           lv_msg    TYPE string.

 ENDCLASS.

***********************************************
*Class Implementation
***********************************************
 CLASS lc_salv IMPLEMENTATION.
*** Fetch the data from tables
   METHOD fetch_data.
     SELECT a~aufnr b~auart b~objnr AS abjnr
            c~aufpl c~aplzl c~vornr c~arbid c~objnr c~steus
            d~matnr
     FROM afko AS a
     INNER JOIN aufk AS b
       ON a~aufnr = b~aufnr
     INNER JOIN afvc AS c
       ON a~aufpl = c~aufpl
     INNER JOIN afpo AS d
       ON d~aufnr = a~aufnr
     INTO CORRESPONDING FIELDS OF TABLE lt_final
     WHERE a~aufnr IN s_aufnr.
     CHECK lt_final IS NOT INITIAL.
     DATA(lv_count) = lines( lt_final ).
     LOOP AT lt_final ASSIGNING FIELD-SYMBOL(<ls_final>).
       CALL FUNCTION 'STATUS_TEXT_EDIT'
         EXPORTING
           objnr            = <ls_final>-objnr
           only_active      = 'X'
           spras            = sy-langu
         IMPORTING
           line             = lv_stext
         EXCEPTIONS
           object_not_found = 1
           OTHERS           = 2.
        IF lv_stext CS 'PNCF' OR lv_stext CS 'REL'.
         <ls_final>-stext = lv_stext.
         CLEAR: lv_stext.
        ENDIF.
     ENDLOOP.
   ENDMETHOD.

*******************************************************************
*Instance Generation is private- cannot create an instance of this class except for the class itself
* Factory design pattern - class itself is responsible for creating instances(objects) of that class
*******************************************************************
   METHOD create_salv_obj.
     cl_salv_table=>factory( IMPORTING r_salv_table = lo_salv
                             CHANGING  t_table      = lt_final ).
     IF sy-subrc IS NOT INITIAL.
       RAISE EXCEPTION TYPE cx_salv_msg.
     ENDIF.
   ENDMETHOD.

*** Display basic toolbar
   METHOD display_toolbar.
     lo_salv->get_functions( )->set_all( if_salv_c_bool_sap=>true ).
   ENDMETHOD.

***  Enable ALV layout change & save
   METHOD set_layout.
*    Local Variables
     lo_salv->get_layout( )->set_key( VALUE #( report = sy-cprog ) ).
     lo_salv->get_layout( )->set_save_restriction( if_salv_c_layout=>restrict_none ).
*  IF id_variant IS NOT INITIAL.
*    lo_salv->get_layout( )->set_initial_layout( id_variant ).
*  ENDIF.
"Set save restriction
"Check authority to change display variants.
*AUTHORITY-CHECK OBJECT 'Z_VARIANT1' ID 'ACTVT' FIELD '*' ##AUTH_OBJ_OK.

     IF sy-subrc = 0.
       " yes, allow user and global display variants
       lo_salv->get_layout( )->set_save_restriction( if_salv_c_layout=>restrict_none ).
     ELSE.
       lo_salv->get_layout( )->set_save_restriction( if_salv_c_layout=>restrict_user_dependant ).
     ENDIF.
   ENDMETHOD.                                    "set_layout

*******************************************
* Setting column attributes
* Key, Multi-Row o/p, Labels, invisible, Hotspot, Sorting, total and sub-totals
*******************************************
   METHOD set_column_attr.
*Multiple Rows selection SALV output
     lo_salv->get_selections( )->set_selection_mode( if_salv_c_selection_mode=>row_column ).
*Set ALV columns to optimized
     lo_salv->get_columns( )->set_optimize( ).
     lo_salv->get_columns( )->set_key_fixation( abap_true ).
*Chnaging column label, Setting Key, Htstspot and Tooltip
     lo_column ?= lo_salv->get_columns( )->get_column( 'AUFNR' ).
     IF lo_column IS BOUND.
       lo_column->set_short_text( 'Prod Ord' ).
       lo_column->set_medium_text( 'Prod Order' ).
       lo_column->set_long_text(   'Production Order' ).
       lo_column->set_key( abap_true ).
*Set Hotspot
       lo_column->set_cell_type( if_salv_c_cell_type=>hotspot ).
*Set tooltip
       lo_column->set_tooltip( 'PRODUCTION ORDER' ).
     ENDIF.
*Hide column
     lo_column ?= lo_salv->get_columns( )->get_column( 'ABJNR' ).
     lo_column->set_visible( if_salv_c_bool_sap=>false ).
*Sorting and subtotals
     lo_salv->get_sorts( )->add_sort( 'AUFNR' ).
     lo_salv->get_sorts( )->add_sort( 'MATNR' ).
     lo_salv->get_sorts( )->add_sort( 'AUFPL' ).
     lo_salv->get_sorts( )->add_sort( 'APLZL' ).
     IF sy-subrc IS NOT INITIAL.
       RAISE EXCEPTION TYPE cx_salv_not_found.
     ENDIF.

   ENDMETHOD.

*** Event handler - double click
   METHOD event_handle_click.
     DATA: lv_msg TYPE string.
     READ TABLE lt_final INTO DATA(ls_final) INDEX row.
     
    IF sy-subrc IS INITIAL.
       CASE column.
         WHEN 'MATNR'.
           SET PARAMETER ID 'MAT' FIELD ls_final-matnr.
           CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN.
       ENDCASE.
     ENDIF.
   
ENDMETHOD.

* Event handler - link click
   METHOD event_handle_link.
     DATA: lv_msg TYPE string.
     READ TABLE lt_final INTO DATA(ls_final) INDEX row.

     IF sy-subrc IS INITIAL.
       CASE column.
         WHEN 'AUFNR'.
           SET PARAMETER ID 'ANR' FIELD ls_final-aufnr.
           CALL TRANSACTION 'CO03' AND SKIP FIRST SCREEN.
       ENDCASE.
     ENDIF.
   ENDMETHOD.

*** Set event handler
   METHOD set_events.
     DATA(r_event)  = lo_salv->get_event( ).
     SET HANDLER me->event_handle_click FOR r_event.
     SET HANDLER me->event_handle_link FOR r_event.
   ENDMETHOD.

** Set Top-of-list
   METHOD create_header_obj.
***   Header object
     DATA(lo_header) = NEW cl_salv_form_layout_grid( ).
     DATA: lv_title   TYPE c LENGTH 150,
           lv_string  TYPE c LENGTH 150,
           lo_h_label TYPE REF TO cl_salv_form_label,
           lo_h_text  TYPE REF TO cl_salv_form_text.
     DATA(lv_count) = lines( lt_final ).

     "Line 2
     lv_title = 'No of Records:'.
     lv_string = ': Entries counts as per selection is'.
     lo_h_label = lo_header->create_label( row = 2 column = 1 ).
     lo_h_label->set_text( lv_title ).
     lo_h_text = lo_header->create_text( row = 2 column = 2 ).
     lo_h_text->set_text( lv_string ).
     lo_h_text = lo_header->create_text( row = 2 column = 3 ).
     lo_h_text->set_text( lv_count ).

     "Line 1
     lv_title = 'Report:'.
     lv_string = ': New Report for testing Order'.
     lo_h_label = lo_header->create_label( row = 1 column = 1 ).
     lo_h_label->set_text( lv_title ).
     lo_h_text = lo_header->create_text( row = 1 column = 2 ).
     lo_h_text->set_text( lv_string ).
     lo_salv->set_top_of_list( lo_header ).
   ENDMETHOD.

*Display output
   METHOD display_output.
     lo_salv->display( ).
   ENDMETHOD.
 ENDCLASS.

 START-OF-SELECTION.
   DATA(lo_obj) = NEW lc_salv( ).
   TRY.
       lo_obj->fetch_data( ).             "Fetches data
       lo_obj->create_salv_obj( ).     "Creates SALV Object
       lo_obj->display_toolbar( ).     "Display basic toolbar
       lo_obj->set_layout( ).              "Change & saveSave Layout
       lo_obj->set_column_attr( ).     "Column attributes
       lo_obj->set_events( ).              "Event handling
       lo_obj->create_header_obj( ).  "Create Top-OF-List
       lo_obj->display_output( ).        "Display Output in ALV format
     CATCH cx_static_check INTO DATA(lx_msg).
       MESSAGE lx_msg->get_text( ) TYPE 'E'.
   ENDTRY.


The output of this report will be as follows:



Comments

Popular posts from this blog

Preparing the ABAP developement environment for SAP BTP

Introducing RESTful Application Programming