Professional Documents
Culture Documents
Developing ALV reports using object oriented factory methods, example ALV
report using factory methods
+ -
REPORT ZSAPN_ALV_MARA_FACTARY.
DATA : IT_MARA TYPE TABLE OF MARA,
WA_MARA TYPE MARA.
DATA : LR_ALV TYPE REF TO CL_SALV_TABLE.
SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
Step2: Call static method FACTORY of class CL_SALV_TABLE, to get table instance with data.
* TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY "get SALV 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_MARA.
* CATCH CX_SALV_MSG .
* ENDTRY.
Learner Questions
ALV report with user defined types using Object Oriented Factory methods
+ -
When ever we use ALV factory methods to display ALV, we don`t need to create any field catalog,
we can directly add our user defined tables instance as it automatically determine fields and
displays.
REPORT ZSAPN_ALV_MARA_FACTARY.
TYPES: BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MBRSH TYPE MARA-MBRSH,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA,
WA_MARA TYPE MARA.
DATA : LR_ALV TYPE REF TO CL_SALV_TABLE.
SELECT MATNR MTART MBRSH MATKL MEINS FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
* TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY "get SALV 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_MARA.
* CATCH CX_SALV_MSG .
* ENDTRY.
LR_ALV->DISPLAY( ). "display grid
Learner Questions
No Questions by learners, be first one to ask ..!!
Insert hotspot into ALV columns using factory methods, example ALV report with
hotspot factory methods
+ -
The below example explains you how to insert hotspot in a column using factory methods.
Step1: Data decelerations, get alv factory instance.
REPORT ZSAPN_ALV_MARA_FACTORY.
TYPES: BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MBRSH TYPE MARA-MBRSH,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA,
WA_MARA TYPE MARA.
DATA : LR_ALV TYPE REF TO CL_SALV_TABLE.
SELECT MATNR MTART MBRSH MATKL MEINS FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
* TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY "get SALV 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_MARA.
* CATCH CX_SALV_MSG .
* ENDTRY.
Learner Questions
No Questions by learners, be first one to ask ..!!
Interactive ALV with factory method using events and event handler methods,
using events in ALV factory methods
+ -
The below example explains you of using events in ALV factory methods, first thing I recommend
you is to go through what are events on Object Oriented ABAP .
*Register events
DATA: LR_EVENTS TYPE REF TO CL_SALV_EVENTS_TABLE.
LR_EVENTS = LR_ALV->GET_EVENT( ). "get event
Register event handler method.
To set functions to ALV, we use SET_SCREEN_STATUS method, to implement this create a menu
in SE41 for this program and add using above method.
In this example I am using standard ALV functions (menu).
REPORT ZSAPN_ALV_MARA_FACTARY.
CLASS LCL_HANDLE_EVENTS DEFINITION DEFERRED. "class definition deffered
TYPES: BEGIN OF TY_MARA, "MARA internal table
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MBRSH TYPE MARA-MBRSH,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA,
WA_MARA TYPE TY_MARA.
DATA : LR_ALV TYPE REF TO CL_SALV_TABLE. "SALV table instance
SELECT MATNR MTART MBRSH MATKL MEINS FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
"fetch data
* TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY "get SALV 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_MARA.
* CATCH CX_SALV_MSG .
* ENDTRY.
*Register events
DATA: LR_EVENTS TYPE REF TO CL_SALV_EVENTS_TABLE.
LR_EVENTS = LR_ALV->GET_EVENT( ). "get event
CREATE OBJECT GR_EVENTS.
SET HANDLER GR_EVENTS->ON_LINE_CLICK FOR LR_EVENTS. "register event handler method
**set standard ALV functions visible
LR_ALV->SET_SCREEN_STATUS(
PFSTATUS = 'SALV_STANDARD'
REPORT = 'SALV_TEST_FUNCTIONS'
SET_FUNCTIONS = LR_ALV->C_FUNCTIONS_ALL ).
Output
Learner Questions
Display totals and subtotals in ALV with factory methods, aggregations in ALV,
sort in ALV
+ -
Most of the times, we may need to display totals and subtotals in ALV reports, in this lesson you will
be able to learn how to display totals and subtotals in ALV with factory methods.
Requirement:Display purchase order details for a range of purchase orders, display totals and
subtotals for a purchase order number .
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.
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.
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
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.
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.
Output
Learner Questions
I want to show a text like "Total" before each Total value (not at the end of the list). Please let
me know how to do it. Urgent.
Thank you for providing an illustration of Aggregation. Please do add one more solution for
my question, How can we set subtotals for multiple columns using factory method? In the
illustration provided, we may add one more column, say NETPR, for displaying Net
Purchasing Value. In that case we need to aggregate subtotal for both the MENGE and
NETPR fields. Please help me modify the code accordingly. Is there any other methods to
achieve aggregation of multiple columns? Please do reply.
Top of page and end of page in
ALV with factory methods
Last Updated: February 21st 2014 by Ashok Kumar Reddy
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
Step1: Data decelerations, 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.
Output
Learner Questions