SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

TOP_OF_PAGE in ALV Using CL_GUI_ALV_GRID



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ALV Grid / ALV Tree / ALV List
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1636

PostPosted: Tue Nov 20, 2007 11:10 am    Post subject: TOP_OF_PAGE in ALV Using CL_GUI_ALV_GRID Reply with quote

This 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 contenet 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 the Steps.

1.Create the Screen 100, and place the Custom Control in the Screen and Name it as CONTROL.
2.Using the PBO Module set the PF-status and Title Bar

Code:
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.

Code:
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.


Code:
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.
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


5. Use of methods ADD_TEXT, ADD_PICTURE, ADD_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.

Code:
DATA : DL_TEXT(255) TYPE C.  "Text
  CALL METHOD DG_DYNDOC_ID->ADD_TEXT
    EXPORTING<BR/>
    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<br/>
  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.


6.Check the sample output of the Report.



7. The complete coding

Code:
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 .
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.
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.

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.
   
CASE SY-UCOMM.
   
WHEN 'BACK'.
   
LEAVE TO SCREEN 0.
   
ENDCASE.
   
ENDMODULE.                 " USER_COMMAND_0100  INPUT


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 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.

* 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


Original: https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/4046
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ALV Grid / ALV Tree / ALV List All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.