Tuesday, March 28, 2017

TOP_OF_PAGE in ALV Using CL_GUI_ALV_GRID

This Blog explains how to trigger the TOP_OF_PAGE event in an ALV Report using CL_GUI_ALV_GRID class. I developed a small report and implemented the TOP_OF_PAGE event of CL_GUI_ALV_GRID class, TOP_OF_PAGE event uses the object of class CL_DD_DOCUMENT. In this class there are methods ADD_TEXT, ADD_PICTURE, and ADD_GAP which are useful to to show the content in the TOP_OF_PAGE.

One important thing is to split the screen into two parts using the splitter container and then use the first part to TOP_OF_PAGE and the second one to show the Grid data. Here is the Simple Report with these steps:

Steps. 1. Create the Screen 100 (call screen 100 in start-of-selection event), and place the Custom Control in the Screen and Name it as CONTROL. 

2.Using the PBO Module of screen 100 set the PF-status and Title Bar.

MODULE STATUS_0100 OUTPUT.
  SET PF-STATUS ‘STATUS’.
  SET TITLEBAR ‘TITLE’.

  IF G_CUSTOM_CONTAINER IS INITIAL.
    PERFORM CREATE_AND_INIT_ALV.
  ENDIF.
ENDMODULE.                 ” STATUS_0100  OUTPUT 


3.Split the Container and Assign the first part to TOP_OF_PAGE and second part to ALV GRID.

DATA: 
  G_GRID TYPE REF TO CL_GUI_ALV_GRID.

* Create TOP-Document 
  CREATE OBJECT O_DYNDOC_ID 
    EXPORTING 
      STYLE = ‘ALV_GRID’.

  CREATE OBJECT G_CUSTOM_CONTAINER 
    EXPORTING 
      CONTAINER_NAME = G_CONTAINER.     

* Create Splitter for custom_container
  CREATE OBJECT O_SPLITTER 
    EXPORTING 
      PARENT      = G_CUSTOM_CONTAINER 
      ROWS         = 2 
      COLUMNS   = 1.

* Assigning Part 1 for TOP_OF_PAGE     
  CALL METHOD O_SPLITTER->GET_CONTAINER     
    EXPORTING     
      ROW = 1     
      COLUMN = 1     
    RECEIVING    
      CONTAINER = O_PARENT_TOP. 

* Assigning the Part 2 to GRID    
  CALL METHOD O_SPLITTER->GET_CONTAINER     
    EXPORTING     
      ROW = 2     
      COLUMN = 1     
    RECEIVING     
      CONTAINER = O_PARENT_GRID.   

* Set height for Top of page   
  CALL METHOD O_SPLITTER->SET_ROW_HEIGHT     
    EXPORTING     
      ID = 1     
      HEIGHT = 5.   
  
  CREATE OBJECT G_GRID 
    EXPORTING I_PARENT = O_PARENT_GRID.

4.Have a local class inside the report to Handle the TOP_OF_PAGE event.
Create the Event Handler Object and Set the handler to trigger the TOP_OF_PAGE event. 


  CREATE OBJECT G_GRID
    EXPORTING I_PARENT = O_PARENT_GRID.

DATA:
  G_HANDLER TYPE REF TO LCL_EVENT_HANDLER.
  CREATE OBJECT G_HANDLER.
  SET HANDLER G_HANDLER->TOP_OF_PAGE FOR G_GRID.

  CLASS LCL_EVENT_HANDLER DEFINITION .
    PUBLIC SECTION .

      METHODS: 

*     Event Handler for Top of page 
      TOP_OF_PAGE FOR EVENT TOP_OF_PAGE OF CL_GUI_ALV_GRID        
        IMPORTING E_DYNDOC_ID.
  ENDCLASS.             “lcl_event_handler 

  CLASS LCL_EVENT_HANDLER IMPLEMENTATION.
    METHOD TOP_OF_PAGE.
*  Top-of-page event 
    PERFORM EVENT_TOP_OF_PAGE 
      USING 
        O_DYNDOC_ID. 
    ENDMETHOD.                            “top_of_page 
  ENDCLASS.       “LCL_EVENT_HANDLER 


5. Use of methods ADD_TEXTADD_PICTUREADD_GAP, etc. ADD_TEXT is used to add the text and also you can specify the color,font size,font type. So many friends asked in the forum I want to place the text right aligned instead of left this also can be done with the combination of ADD_TEXT and ADD_GAP, but this is not possible with the ALV FM’s. ADD_PICTURE is used to Add the Logo in the Top of page.Incase of ALV Grid(using FM) the Logo always on Right side.But here you can place where you want. ADD_GAP is used to add the Gap,It can take the paramter width, with that parameter you can maintain the gap between two texts. NEW_LINE is add the New line where ever required. 

DATA : 
  DL_TEXT(255) TYPE C.  “Text   
  
CALL METHOD DG_DYNDOC_ID->ADD_TEXT     
  EXPORTING    
    TEXT = ‘Flight Details’     
    SAP_STYLE = CL_DD_AREA=>HEADING     
    SAP_FONTSIZE = CL_DD_AREA=>LARGE     
    SAP_COLOR = CL_DD_AREA=>LIST_HEADING_INT.     

CALL METHOD DG_DYNDOC_ID->ADD_GAP     
  EXPORTING     
    WIDTH = 200.     

CALL METHOD O_DYNDOC_ID->ADD_PICTURE     
  EXPORTING     
    PICTURE_ID = ‘ENJOYSAP_LOGO’.   

* Add new-line  
CALL METHOD DG_DYNDOC_ID->NEW_LINE.     

CALL METHOD DG_DYNDOC_ID->NEW_LINE.   

CLEAR : DL_TEXT.   


The complete coding :

REPORT  z_oo_alv_top_of_page  MESSAGE-ID zz  .
DATA: it_flight TYPE TABLE OF sflight.

DATA: ok_code LIKE sy-ucomm,
save_ok LIKE sy-ucomm.

DATA:  g_container TYPE scrfname VALUE 'CONTROL',
o_dyndoc_id  TYPE REF TO cl_dd_document,
o_splitter   TYPE REF TO cl_gui_splitter_container,
o_parent_grid TYPE REF TO cl_gui_container,
o_parent_top TYPE REF TO cl_gui_container,
o_html_cntrl TYPE REF TO cl_gui_html_viewer.

*----------------------------------------------------------------------*
*       CLASS LCL_EVENT_HANDLER DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_handler DEFINITION .
  PUBLIC SECTION .
    METHODS:
*Event Handler for Top of page
    top_of_page FOR EVENT top_of_page
           OF cl_gui_alv_grid
           IMPORTING e_dyndoc_id.
ENDCLASS.             "lcl_event_handler DEFINITION
*----------------------------------------------------------------------*
*       CLASS LCL_EVENT_HANDLER IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_handler IMPLEMENTATION.
  METHOD top_of_page.
* Top-of-page event
    PERFORM event_top_of_page USING o_dyndoc_id.

  ENDMETHOD.                            "top_of_page
ENDCLASS.       "LCL_EVENT_HANDLER IMPLEMENTATION

DATA: g_custom_container TYPE REF TO cl_gui_custom_container,
      g_handler TYPE REF TO lcl_event_handler. "handler

START-OF-SELECTION.
  SELECT *
  FROM sflight
  UP TO 20 ROWS
  INTO TABLE it_flight.

END-OF-SELECTION.
  IF NOT it_flight[] IS INITIAL.

    CALL SCREEN 100.

  ELSE.

    MESSAGE i002 WITH 'NO DATA FOR THE SELECTION'(004).

  ENDIF.


*----------------------------------------------------------------------*
*  MODULE STATUS_0100 OUTPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.

  SET PF-STATUS 'STATUS'.

  SET TITLEBAR 'TITLE'.

  IF g_custom_container IS INITIAL.

    PERFORM create_and_init_alv.

  ENDIF.

ENDMODULE.                 " STATUS_0100  OUTPUT


*----------------------------------------------------------------------*
*  MODULE USER_COMMAND_0100 INPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.

  CASE sy-ucomm.

    WHEN 'BACK'.

      LEAVE TO SCREEN 0.

  ENDCASE.

ENDMODULE.                 " USER_COMMAND_0100  INPUT


*&---------------------------------------------------------------------*
*&      Form  CREATE_AND_INIT_ALV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM create_and_init_alv .

  DATA: g_grid TYPE REF TO cl_gui_alv_grid.

  CREATE OBJECT g_custom_container
    EXPORTING
      container_name = g_container.

* Create TOP-Document
  CREATE OBJECT o_dyndoc_id
    EXPORTING
      style = 'ALV_GRID'.

* Create Splitter for custom_container
  CREATE OBJECT o_splitter
    EXPORTING
      parent  = g_custom_container
      rows    = 2
      columns = 1.

  CALL METHOD o_splitter->get_container
    EXPORTING
      row       = 1
      column    = 1
    RECEIVING
      container = o_parent_top.

  CALL METHOD o_splitter->get_container
    EXPORTING
      row       = 2
      column    = 1
    RECEIVING
      container = o_parent_grid.

* Set height for g_parent_html
  CALL METHOD o_splitter->set_row_height
    EXPORTING
      id     = 1
      height = 5.

  CREATE OBJECT g_grid
    EXPORTING
      i_parent = o_parent_grid.

  CREATE OBJECT g_handler.

  SET HANDLER g_handler->top_of_page FOR g_grid.

*Calling the Method for ALV output
  CALL METHOD g_grid->set_table_for_first_display
    EXPORTING
      i_structure_name = 'SFLIGHT'
    CHANGING
      it_outtab        = it_flight[].


  CALL METHOD o_dyndoc_id->initialize_document
    EXPORTING
      background_color = cl_dd_area=>col_textarea.

* Processing events
  CALL METHOD g_grid->list_processing_events
    EXPORTING
      i_event_name = 'TOP_OF_PAGE'
      i_dyndoc_id  = o_dyndoc_id.


ENDFORM.                     "CREATE_AND_INIT_ALV


*&---------------------------------------------------------------------*
*&      Form  EVENT_TOP_OF_PAGE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->DG_DYNDOC_ID  text
*----------------------------------------------------------------------*
FORM event_top_of_page USING   dg_dyndoc_id TYPE REF TO
cl_dd_document.

  DATA : dl_text(255) TYPE c.  "Text
  CALL METHOD dg_dyndoc_id->add_text
    EXPORTING
      text         = 'Flight Details'
      sap_style    = cl_dd_area=>heading
      sap_fontsize = cl_dd_area=>large
      sap_color    = cl_dd_area=>list_heading_int.

  CALL METHOD dg_dyndoc_id->add_gap
    EXPORTING
      width = 200.

  CALL METHOD o_dyndoc_id->add_picture
    EXPORTING
      picture_id = 'ENJOYSAP_LOGO'.

* Add new-line
  CALL METHOD dg_dyndoc_id->new_line.

  CALL METHOD dg_dyndoc_id->new_line.


  CLEAR : dl_text.

* program ID
  dl_text = 'Program Name :'.

  CALL METHOD dg_dyndoc_id->add_gap.

  CALL METHOD o_dyndoc_id->add_text
    EXPORTING
      text         = dl_text
      sap_emphasis = cl_dd_area=>heading
      sap_color    = cl_dd_area=>list_heading_int.

  CLEAR dl_text.

  dl_text = sy-repid.

  CALL METHOD o_dyndoc_id->add_text
    EXPORTING
      text         = dl_text
      sap_emphasis = cl_dd_area=>heading
      sap_color    = cl_dd_area=>list_negative_inv.

* Add new-line
  CALL METHOD dg_dyndoc_id->new_line.


  CLEAR : dl_text.


  CLEAR : dl_text.

* program ID
  dl_text = 'User Name :'.

  CALL METHOD dg_dyndoc_id->add_gap.

  CALL METHOD o_dyndoc_id->add_text
    EXPORTING
      text         = dl_text
      sap_emphasis = cl_dd_area=>heading
      sap_color    = cl_dd_area=>list_heading_int.

  CLEAR dl_text.

  dl_text = sy-uname.

  CALL METHOD o_dyndoc_id->add_text
    EXPORTING
      text         = dl_text
      sap_emphasis = cl_dd_area=>heading
      sap_color    = cl_dd_area=>list_negative_inv.

* Add new-line
  CALL METHOD dg_dyndoc_id->new_line.


  CLEAR : dl_text.

* Run Date
  dl_text = 'Run Date :'.

  CALL METHOD dg_dyndoc_id->add_gap.

  CALL METHOD o_dyndoc_id->add_text
    EXPORTING
      text         = dl_text
      sap_emphasis = cl_dd_area=>heading
      sap_color    = cl_dd_area=>list_heading_int.

  CLEAR dl_text.

* Move date
  WRITE sy-datum TO dl_text.

  CALL METHOD o_dyndoc_id->add_text
    EXPORTING
      text         = dl_text
      sap_emphasis = cl_dd_area=>heading
      sap_color    = cl_dd_area=>list_negative_inv.

* Add new-line
  CALL METHOD dg_dyndoc_id->new_line.

  CLEAR : dl_text.

*Time
  dl_text = 'Time :'.

  CALL METHOD dg_dyndoc_id->add_gap.

  CALL METHOD o_dyndoc_id->add_text
    EXPORTING
      text         = dl_text
      sap_emphasis = cl_dd_area=>heading
      sap_color    = cl_dd_area=>list_heading_int.

  CLEAR dl_text.

* Move time
  WRITE sy-uzeit TO dl_text.

  CALL METHOD o_dyndoc_id->add_text
    EXPORTING
      text         = dl_text
      sap_emphasis = cl_dd_area=>heading
      sap_color    = cl_dd_area=>list_negative_inv.

* Add new-line
  CALL METHOD dg_dyndoc_id->new_line.


  PERFORM display.

ENDFORM.                    " EVENT_TOP_OF_PAGE

*&---------------------------------------------------------------------*
*&      Form  DISPLAY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM display.

* Creating html control
  IF o_html_cntrl IS INITIAL.
    CREATE OBJECT o_html_cntrl
      EXPORTING
        parent = o_parent_top.
  ENDIF.
  CALL METHOD o_dyndoc_id->merge_document.
  o_dyndoc_id->html_control = o_html_cntrl.
* Display document
  CALL METHOD o_dyndoc_id->display_document
    EXPORTING
      reuse_control      = 'X'
      parent             = o_parent_top
    EXCEPTIONS
      html_display_error = 1.
  IF sy-subrc NE 0.
    MESSAGE i999 WITH 'Error in displaying top-of-page'(036).
  ENDIF.
ENDFORM.                    " display



image 

No comments:

Post a Comment

SAP giới thiệu mã hỗ trợ AI trong ngôn ngữ ABAP riêng của mình

SAP đã ra mắt một loạt tính năng mã hỗ trợ AI trong môi trường phát triển ứng dụng dựa trên đám mây của mình, đồng thời tham gia vào danh sá...