Top of page and end of page in OOALV with factory methods

Display top of page, end of page using ALV factory methods, top of list in ALV factory methods

+ -

The below example explains you how to add top of page(top of list) and end of page to ALV report with factory method.

Requirement:Display purchase order details for a range of purchase orders, display totals and subtotals for a purchase order number .Display top of page(top of list) and end of list.

Requirement Analysis: For the above requirement, we need to get data from EKPO(purchase order item table), display subtotals for each purchase order number(EBELN field)(one purchase order can have multiple items) and display totals of all purchase orders at the bottom.Display top of page(top of list) and end of list

Step 1: Data deceleration, get data and get factory instance.

REPORT ZSAPN_ALV_FACTORY_TOTALS.
TABLES: EKKO.
SELECT-OPTIONS : S_EBELN FOR EKKO-EBELN. "po number input
TYPES: BEGIN OF TY_EKPO, "user defined types
        EBELN TYPE EKPO-EBELN,
        EBELP TYPE EKPO-EBELP,
        MATNR TYPE EKPO-MATNR,
        BUKRS TYPE EKPO-BUKRS,
        MENGE TYPE EKPO-MENGE,
      END OF TY_EKPO.
DATA : IT_EKPO TYPE TABLE OF TY_EKPO, "internal table
       WA_EKPO TYPE TY_EKPO. "work area

START-OF-SELECTION.
  SELECT EBELN EBELP MATNR BUKRS MENGE FROM EKPO INTO TABLE IT_EKPO WHERE EBELN IN S_EBELN. "get po data
  DATA : LR_ALV TYPE REF TO CL_SALV_TABLE. "alv referance

*TRY.
  CALL METHOD CL_SALV_TABLE=>FACTORY "load factory instance
*  EXPORTING
*    LIST_DISPLAY   = IF_SALV_C_BOOL_SAP=>FALSE
*    R_CONTAINER    =
*    CONTAINER_NAME =
    IMPORTING
      R_SALV_TABLE = LR_ALV
    CHANGING
      T_TABLE      = IT_EKPO.
* CATCH CX_SALV_MSG .
*ENDTRY.

Step 2: Add totals

To add totals we need to use GET_AGGREGATIONS, once we get aggregations instance, we need to add aggregation by passing column name and aggregation type to method ADD_AGGREGATION.

  DATA: LO_AGGRS TYPE REF TO CL_SALV_AGGREGATIONS.
  LO_AGGRS = LR_ALV->GET_AGGREGATIONS( ). "get aggregations
*   Add TOTAL for COLUMN NETWR
  TRY.
      CALL METHOD LO_AGGRS->ADD_AGGREGATION "add aggregation
        EXPORTING
          COLUMNNAME  = 'MENGE' "aggregation column name
          AGGREGATION = IF_SALV_C_AGGREGATION=>TOTAL. "aggregation type

    CATCH CX_SALV_DATA_ERROR .                          "#EC NO_HANDLER
    CATCH CX_SALV_NOT_FOUND .                           "#EC NO_HANDLER
    CATCH CX_SALV_EXISTING .                            "#EC NO_HANDLER
  ENDTRY.

Step 3: Add subtotals

To add subtotals, we need to add sort to the columns and then we have to use SET_SUBTOTAL method to display subtotals.
  DATA : LR_SORT TYPE REF TO CL_SALV_SORTS. "ALV sorts
  CALL METHOD LR_ALV->GET_SORTS "get sorts
    RECEIVING
      VALUE = LR_SORT.
*TRY.
  DATA : LR_SORT_COLUMN TYPE REF TO CL_SALV_SORT. "column sort
  CALL METHOD LR_SORT->ADD_SORT "add column sort
    EXPORTING
      COLUMNNAME = 'EBELN' "sort column always keyfield
*     POSITION   =
*     SEQUENCE   = IF_SALV_C_SORT=>SORT_UP
*     SUBTOTAL   = IF_SALV_C_BOOL_SAP=>FALSE
*     GROUP      = IF_SALV_C_SORT=>GROUP_NONE
*     OBLIGATORY = IF_SALV_C_BOOL_SAP=>FALSE
    RECEIVING
      VALUE      = LR_SORT_COLUMN.

*  TRY.
  CALL METHOD LR_SORT_COLUMN->SET_SUBTOTAL "add subtotal
    EXPORTING
      VALUE = IF_SALV_C_BOOL_SAP=>TRUE.
*   CATCH CX_SALV_DATA_ERROR .
*  ENDTRY.

Step 4: Add top of page(top of list) and end of list.

To add top of page and end of list we need to create a layout gird and insert that grid into ALV using method SET_TOP_OF_LIST.

Create layout grid.

  DATA : LR_HEADER TYPE REF TO CL_SALV_FORM_ELEMENT.
  DATA: LR_GRID_LAYOUT   TYPE REF TO CL_SALV_FORM_LAYOUT_GRID,
        LR_LABEL  TYPE REF TO CL_SALV_FORM_LABEL,
        LR_TEXT   TYPE REF TO CL_SALV_FORM_TEXT,
        L_TEXT    TYPE STRING.
  CREATE OBJECT LR_GRID_LAYOUT.
  L_TEXT = 'Purchase Order Report' .
  LR_GRID_LAYOUT->CREATE_HEADER_INFORMATION(
    ROW    = 1
    COLUMN = 3
    TEXT    = L_TEXT
    TOOLTIP = L_TEXT ).
  LR_GRID_LAYOUT->ADD_ROW( ).
*  LR_GRID_LAYOUT_1 = LR_GRID_LAYOUT->CREATE_GRID(
*                ROW    = 3
*                COLUMN = 1 ).
  LR_LABEL = LR_GRID_LAYOUT->CREATE_LABEL(
    ROW     = 2
    COLUMN  = 1
    TEXT    = 'Number of records found: '
    TOOLTIP = 'Number of records found for your query' ).
  LR_TEXT = LR_GRID_LAYOUT->CREATE_TEXT(
    ROW     = 2
    COLUMN  = 2
    TEXT    = LV_CNT
    TOOLTIP = LV_CNT ).
  LR_LABEL->SET_LABEL_FOR( LR_TEXT ).
  LR_LABEL = LR_GRID_LAYOUT->CREATE_LABEL(
    ROW    = 3
    COLUMN = 1
    TEXT    = 'Date : '
    TOOLTIP = 'Date' ).
  L_TEXT = SY-DATUM.
  LR_TEXT = LR_GRID_LAYOUT->CREATE_TEXT(
    ROW    = 3
    COLUMN = 2
    TEXT    = L_TEXT
    TOOLTIP = L_TEXT ).
  LR_LABEL->SET_LABEL_FOR( LR_TEXT ).
  LR_HEADER = LR_GRID_LAYOUT.

Create top of list.

  CALL METHOD LR_ALV->SET_TOP_OF_LIST
    EXPORTING
      VALUE = LR_HEADER.

Step 5: Add End Of List

End of list can be created using method SET_END_OF_LIST.
  DATA: LR_FOOTER TYPE REF TO CL_SALV_FORM_HEADER_INFO.
  CLEAR L_TEXT.
  L_TEXT = 'End of List as Footer'.
  CREATE OBJECT LR_FOOTER
    EXPORTING
      TEXT    = L_TEXT
      TOOLTIP = L_TEXT.
  LR_ALV->SET_END_OF_LIST( LR_FOOTER ).

Step 6: Display ALV

  LR_ALV->DISPLAY( ). "display ALV

Final code will be

REPORT ZSAPN_ALV_FACTORY_TOPOFLIST.
TABLES: EKKO.
SELECT-OPTIONS : S_EBELN FOR EKKO-EBELN. "po number input
TYPES: BEGIN OF TY_EKPO, "user defined types
        EBELN TYPE EKPO-EBELN,
        EBELP TYPE EKPO-EBELP,
        MATNR TYPE EKPO-MATNR,
        BUKRS TYPE EKPO-BUKRS,
        MENGE TYPE EKPO-MENGE,
      END OF TY_EKPO.
DATA : IT_EKPO TYPE TABLE OF TY_EKPO, "internal table
       WA_EKPO TYPE TY_EKPO. "work area
DATA : LV_CNT TYPE I.

START-OF-SELECTION.
  SELECT EBELN EBELP MATNR BUKRS MENGE FROM EKPO INTO TABLE IT_EKPO WHERE EBELN IN S_EBELN. "get po data
  DESCRIBE TABLE IT_EKPO LINES LV_CNT.
  DATA : LR_ALV TYPE REF TO CL_SALV_TABLE. "alv reference
*TRY.
  CALL METHOD CL_SALV_TABLE=>FACTORY "load factory instance
*  EXPORTING
*    LIST_DISPLAY   = IF_SALV_C_BOOL_SAP=>FALSE
*    R_CONTAINER    =
*    CONTAINER_NAME =
    IMPORTING
      R_SALV_TABLE = LR_ALV
    CHANGING
      T_TABLE      = IT_EKPO.
* CATCH CX_SALV_MSG .
*ENDTRY.

  DATA: LO_AGGRS TYPE REF TO CL_SALV_AGGREGATIONS.
  LO_AGGRS = LR_ALV->GET_AGGREGATIONS( ). "get aggregations
*   Add TOTAL for COLUMN NETWR
  TRY.
      CALL METHOD LO_AGGRS->ADD_AGGREGATION "add aggregation
        EXPORTING
          COLUMNNAME  = 'MENGE' "aggregation column name
          AGGREGATION = IF_SALV_C_AGGREGATION=>TOTAL. "aggregation type

    CATCH CX_SALV_DATA_ERROR .                          "#EC NO_HANDLER
    CATCH CX_SALV_NOT_FOUND .                           "#EC NO_HANDLER
    CATCH CX_SALV_EXISTING .                            "#EC NO_HANDLER
  ENDTRY.
*   Bring the total line to top
  DATA : LR_SORT TYPE REF TO CL_SALV_SORTS. "ALV sorts
  CALL METHOD LR_ALV->GET_SORTS "get sorts
    RECEIVING
      VALUE = LR_SORT.
*TRY.
  DATA : LR_SORT_COLUMN TYPE REF TO CL_SALV_SORT. "column sort
  CALL METHOD LR_SORT->ADD_SORT "add column sort
    EXPORTING
      COLUMNNAME = 'EBELN' "sort column always key field
*     POSITION   =
*     SEQUENCE   = IF_SALV_C_SORT=>SORT_UP
*     SUBTOTAL   = IF_SALV_C_BOOL_SAP=>FALSE
*     GROUP      = IF_SALV_C_SORT=>GROUP_NONE
*     OBLIGATORY = IF_SALV_C_BOOL_SAP=>FALSE
    RECEIVING
      VALUE      = LR_SORT_COLUMN.
*  TRY.
  CALL METHOD LR_SORT_COLUMN->SET_SUBTOTAL "add subtotal
    EXPORTING
      VALUE = IF_SALV_C_BOOL_SAP=>TRUE.
*   CATCH CX_SALV_DATA_ERROR .
*  ENDTRY.
* CATCH CX_SALV_NOT_FOUND .
* CATCH CX_SALV_EXISTING .
* CATCH CX_SALV_DATA_ERROR .
*ENDTRY.
***Top of Page
  DATA : LR_HEADER TYPE REF TO CL_SALV_FORM_ELEMENT.
  DATA: LR_GRID_LAYOUT   TYPE REF TO CL_SALV_FORM_LAYOUT_GRID,
        LR_LABEL  TYPE REF TO CL_SALV_FORM_LABEL,
        LR_TEXT   TYPE REF TO CL_SALV_FORM_TEXT,
        L_TEXT    TYPE STRING.
  CREATE OBJECT LR_GRID_LAYOUT.
  L_TEXT = 'Purchase Order Report' .
  LR_GRID_LAYOUT->CREATE_HEADER_INFORMATION( "create header for gird
    ROW    = 1
    COLUMN = 3
    TEXT    = L_TEXT
    TOOLTIP = L_TEXT ).
  LR_GRID_LAYOUT->ADD_ROW( ). "add row
  LR_LABEL = LR_GRID_LAYOUT->CREATE_LABEL(
    ROW     = 2
    COLUMN  = 1
    TEXT    = 'Number of records found: '
    TOOLTIP = 'Number of records found for your query' ).
  LR_TEXT = LR_GRID_LAYOUT->CREATE_TEXT(
    ROW     = 2
    COLUMN  = 2
    TEXT    = LV_CNT
    TOOLTIP = LV_CNT ).
  LR_LABEL->SET_LABEL_FOR( LR_TEXT ).
  LR_LABEL = LR_GRID_LAYOUT->CREATE_LABEL(
    ROW    = 3
    COLUMN = 1
    TEXT    = 'Date : '
    TOOLTIP = 'Date' ).
  L_TEXT = SY-DATUM.
  LR_TEXT = LR_GRID_LAYOUT->CREATE_TEXT(
    ROW    = 3
    COLUMN = 2
    TEXT    = L_TEXT
    TOOLTIP = L_TEXT ).
  LR_LABEL->SET_LABEL_FOR( LR_TEXT ).
  LR_HEADER = LR_GRID_LAYOUT.
  CALL METHOD LR_ALV->SET_TOP_OF_LIST  "set top of list
    EXPORTING
      VALUE = LR_HEADER.
***end of list
  DATA: LR_FOOTER TYPE REF TO CL_SALV_FORM_HEADER_INFO.
  CLEAR L_TEXT.
  L_TEXT = 'End of List as Footer'.
  CREATE OBJECT LR_FOOTER
    EXPORTING
      TEXT    = L_TEXT
      TOOLTIP = L_TEXT.
  LR_ALV->SET_END_OF_LIST( LR_FOOTER ). "set end of list

  LR_ALV->DISPLAY( ). "display ALV

Output

ALV top of list end of list factory methods

Learner Questions


No Questions by learners, be first one to ask ..!!

    Please Sign in to ask a question

    Was this lesson helpful to you? Yes No 15 People out of 15 think this lesson helpful


    Lesson Navigation

    ←Previous Chapter
    Next Lesson → Singleton Pattern on ABAP Objects