Anda di halaman 1dari 41

Display Subtotal and Total in ABAP

ALV Grid
https://www.saptechpro.com/post/SALV_DEMO_METADATA

November 14, 2012 ABAP Tutorial 7

Use below steps to calculate totals in ABAP ALV.

1. Build field catalog and set the field DO_SUM of field catalog to ‘X’ for the field for
which you want calculate the totals.
2. Pass field catalog to function module ‘REUSE_ALV_GRID_DISPLAY’.

TYPE-POOLS: slis. " SLIS contains all the ALV data types

*&---------------------------------------------------------------------*

*& Data Declaration

*&---------------------------------------------------------------------*

DATA: it_sbook TYPE TABLE OF sbook.

DATA: it_fieldcat TYPE slis_t_fieldcat_alv,

wa_fieldcat TYPE slis_fieldcat_alv.

*&---------------------------------------------------------------------*

*& START-OF-SELECTION

*&---------------------------------------------------------------------*

START-OF-SELECTION.

*Fetch data from the database

SELECT * FROM sbook INTO TABLE it_sbook.

*Build field catalog

wa_fieldcat-fieldname = 'CARRID'. " Fieldname in the data table


wa_fieldcat-seltext_m = 'Airline'. " Column description in the output

APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = 'CONNID'.

wa_fieldcat-seltext_m = 'Con. No.'.

APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = 'FLDATE'.

wa_fieldcat-seltext_m = 'Date'.

APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = 'BOOKID'.

wa_fieldcat-seltext_m = 'Book. ID'.

APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = 'PASSNAME'.

wa_fieldcat-seltext_m = 'Passenger Name'.

APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = 'LOCCURAM'.

wa_fieldcat-seltext_m = 'Price'.

*Calculate Total for Price

wa_fieldcat-do_sum = 'X'.

APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = 'LOCCURKEY'.

wa_fieldcat-seltext_m = 'Currency'.
APPEND wa_fieldcat TO it_fieldcat.

*Pass data and field catalog to ALV function module to display ALV list

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

it_fieldcat = it_fieldcat

TABLES

t_outtab = it_sbook

EXCEPTIONS

program_error = 1

OTHERS = 2.

Output
Subtotal in ABAP ALV
To calculate subtotal in ALV, build sort catalog and set the field SUBTOT of sort catalog to
‘X’.

TYPE-POOLS: slis. " SLIS contains all the ALV data types

*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
DATA: it_sbook TYPE TABLE OF sbook.
DATA: it_fieldcat TYPE slis_t_fieldcat_alv,
wa_fieldcat TYPE slis_fieldcat_alv,
it_sort TYPE slis_t_sortinfo_alv,
wa_sort TYPE slis_sortinfo_alv..
*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.

*Fetch data from the database


SELECT * FROM sbook INTO TABLE it_sbook.

*Build field catalog


wa_fieldcat-fieldname = 'CARRID'. " Fieldname in the data table
wa_fieldcat-seltext_m = 'Airline'. " Column description in the output
APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = 'CONNID'.
wa_fieldcat-seltext_m = 'Con. No.'.
APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = 'FLDATE'.
wa_fieldcat-seltext_m = 'Date'.
APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = 'BOOKID'.
wa_fieldcat-seltext_m = 'Book. ID'.
APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = 'PASSNAME'.
wa_fieldcat-seltext_m = 'Passenger Name'.
APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = 'LOCCURAM'.
wa_fieldcat-seltext_m = 'Price'.
wa_fieldcat-do_sum = 'X'.
APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = 'LOCCURKEY'.
wa_fieldcat-seltext_m = 'Currency'.
APPEND wa_fieldcat TO it_fieldcat.
*Build sort catalog
wa_sort-spos = 1.
wa_sort-fieldname = 'CARRID'.
wa_sort-up = 'X'.
wa_sort-subtot = 'X'.
APPEND wa_sort TO it_sort.

*Pass data and field catalog to ALV function module to display ALV list
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
it_fieldcat = it_fieldcat
it_sort = it_sort
TABLES
t_outtab = it_sbook
EXCEPTIONS
program_error = 1
OTHERS = 2.

Output
If you want to calculate subtotals for more fields, just add a record in sort field catalog for
each field.

*Build sort catalog


wa_sort-spos = 1.
wa_sort-fieldname = 'CARRID'.
wa_sort-up = 'X'.
wa_sort-subtot = 'X'.
APPEND wa_sort TO it_sort.

wa_sort-spos = 2.
wa_sort-fieldname = 'CONNID'.
wa_sort-up = 'X'.
wa_sort-subtot = 'X'.
APPEND wa_sort TO it_sort.

Output
Sub Total Text in ABAP ALV Grid
By

Prakash

07/24/2015

6572

[adsenseyu2]
Hello everyone, In the ALV ABAP Tutorials, we will learn how to add sub total
text in ABAP ALV Grid.

Step-by-Step Procedure

1. Create an ABAP Program in SE38 transaction.


2. Create and ALV report using the function
module REUSE_ALV_GRID_DISPLAY.
3. Populate the SUBTOTAL_EVENT in IT_EVENTS parameter
in the REUSE_ALV_GRID_DISPLAY.

You can find the full ABAP code below.

REPORT Z_ALV_SUBTOTAL_TEXT.

*-----------------------------------------------------
--------*

* www.saplearners.com
*

*-----------------------------------------------------
--------*

* Sub Total Text


*

*-----------------------------------------------------
--------*

*--- Table declaration

TABLES: ekko.
*-- Type pool declaration

TYPE-POOLS: slis.

*--- Selection screen

SELECT-OPTIONS: s_ebeln FOR ekko-ebeln.

*--- Type declaration

TYPES: BEGIN OF lty_ekpo,

ebeln TYPE char30, " Document no.

ebelp TYPE ebelp, " Item no

matnr TYPE matnr, " Material no

matnr1 TYPE matnr, " Material no

werks TYPE werks_d, " Plant

werks1 TYPE werks_d, " Plant

ntgew TYPE entge, " Net weight


gewe TYPE egewe, " Unit of weight

END OF lty_ekpo.

*--- Internal table declaration

DATA: lt_ekpo TYPE STANDARD TABLE OF


lty_ekpo,

lt_fieldcat TYPE slis_t_fieldcat_alv,

lt_alv_top_of_page TYPE slis_t_listheader,

lt_events TYPE slis_t_event,

lt_sort TYPE slis_t_sortinfo_alv,

i_event TYPE slis_t_event.

*--- Work area declaration

DATA: wa_ekko TYPE lty_ekpo,

wa_layout TYPE slis_layout_alv,

wa_events TYPE slis_alv_event,

wa_sort TYPE slis_sortinfo_alv.


*--- Start-of-selection event

START-OF-SELECTION.

* Select data from ekpo

SELECT ebeln " Doc no

ebelp " Item

matnr " Material

matnr " Material

werks " Plant

werks " Plant

ntgew " Quantity

gewei " Unit

FROM ekpo

INTO TABLE lt_ekpo

WHERE ebeln IN s_ebeln

AND ntgew NE '0.00'.


IF sy-subrc = 0.

SORT lt_ekpo BY ebeln ebelp matnr .

ENDIF.

*--- Field Catalog

PERFORM f_field_catalog.

*--- Layout

PERFORM f_build_layout.

* Perform to populate the sort table.

PERFORM f_populate_sort.

* Perform to populate ALV event

PERFORM f_get_event.

END-OF-SELECTION.

* Perform to display ALV report


PERFORM f_alv_report_display.

*&----------------------------------------------------
-----------------*

*& Form sub_field_catalog

*&----------------------------------------------------
-----------------*

* Build Field Catalog

*-----------------------------------------------------
-----------------*

* No Parameter

*-----------------------------------------------------
-----------------*

FORM f_field_catalog .

DATA: lwa_fcat TYPE slis_fieldcat_alv.

* Build Field Catalog

lwa_fcat-col_pos = 1. "Column

lwa_fcat-fieldname = 'EBELN'. "Field Name


lwa_fcat-tabname = 'LT_EKPO'. "Internal
Table Name

lwa_fcat-seltext_l = 'Doc. No'. "Field Text

APPEND lwa_fcat TO lt_fieldcat.

lwa_fcat-col_pos = 2. "Column

lwa_fcat-fieldname = 'EBELP'. "Field Name

lwa_fcat-tabname = 'LT_EKPO'. "Internal


Table Name

lwa_fcat-seltext_l = 'Item No'. "Field Text

APPEND lwa_fcat TO lt_fieldcat.

CLEAR:lwa_fcat.

lwa_fcat-col_pos = 3. "Column

lwa_fcat-fieldname = 'WERKS1'. "Field Name

lwa_fcat-tabname = 'LT_EKPO'. "Internal


Table Name

lwa_fcat-seltext_l = 'Plant'. "Field Text

APPEND lwa_fcat TO lt_fieldcat.

CLEAR:lwa_fcat.
lwa_fcat-col_pos = 3. "Column

lwa_fcat-fieldname = 'WERKS'. "Field Name

lwa_fcat-tabname = 'LT_EKPO'. "Internal


Table Name

lwa_fcat-no_out = 'X'.

lwa_fcat-tech = 'X'.

lwa_fcat-seltext_l = 'Plant'. "Field Text

APPEND lwa_fcat TO lt_fieldcat.

CLEAR:lwa_fcat.

lwa_fcat-col_pos = 4. "Column

lwa_fcat-fieldname = 'MATNR1'. "Field Name

lwa_fcat-tabname = 'LT_EKPO'. "Internal


Table Name

lwa_fcat-seltext_l = 'Material'. "Field Text

APPEND lwa_fcat TO lt_fieldcat.

CLEAR:lwa_fcat.

lwa_fcat-col_pos = 4. "Column
lwa_fcat-fieldname = 'MATNR'. "Field Name

lwa_fcat-tabname = 'LT_EKPO'. "Internal


Table Name

lwa_fcat-no_out = 'X'.

lwa_fcat-tech = 'X'.

lwa_fcat-seltext_l = 'Material'. "Field Text

APPEND lwa_fcat TO lt_fieldcat.

CLEAR:lwa_fcat.

lwa_fcat-col_pos = 5. "Column

lwa_fcat-fieldname = 'NTGEW'. "Field Name

lwa_fcat-tabname = 'LT_EKPO'. "Internal


Table Name

lwa_fcat-seltext_l = 'Quantity'. "Field Text

lwa_fcat-do_sum = 'X'. "Sum

APPEND lwa_fcat TO lt_fieldcat.

CLEAR:lwa_fcat.

ENDFORM. " sub_field_catalog


*&----------------------------------------------------
-----------------*

*& Form f_populate_layout

*&----------------------------------------------------
-----------------*

* Populate ALV layout

*-----------------------------------------------------
-----------------*

* No Parameter

*-----------------------------------------------------
-----------------*

FORM f_build_layout.

CLEAR wa_layout.

wa_layout-colwidth_optimize = 'X'." Optimization of


Col width

ENDFORM. " f_populate_layout

*&----------------------------------------------------
-----------------*

*& Form f_populate_sort


*&----------------------------------------------------
-----------------*

FORM f_populate_sort .

* Sort on plant

wa_sort-spos = 1.

wa_sort-fieldname = 'WERKS'.

wa_sort-tabname = 'I_EKPO'.

wa_sort-up = 'X'.

wa_sort-subtot = 'X'.

APPEND wa_sort TO lt_sort .

CLEAR wa_sort.

* Sort on material

wa_sort-spos = 2.

wa_sort-fieldname = 'MATNR'.

wa_sort-tabname = 'I_EKPO'.

wa_sort-up = 'X'.

wa_sort-subtot = 'X'.
APPEND wa_sort TO lt_sort .

CLEAR wa_sort.

ENDFORM. " f_populate_sort

*&----------------------------------------------------
-----------------*

*& Form f_get_event

*&----------------------------------------------------
-----------------*

FORM f_get_event.

DATA: lwa_event TYPE slis_alv_event.

CALL FUNCTION 'REUSE_ALV_EVENTS_GET'

IMPORTING

et_events = lt_events

EXCEPTIONS

list_type_wrong = 0

OTHERS = 0.
* Subtotal

READ TABLE lt_events INTO lwa_event

WITH KEY name =


slis_ev_subtotal_text.

IF sy-subrc = 0.

lwa_event-form = 'SUBTOTAL_TEXT'.

MODIFY lt_events FROM lwa_event INDEX sy-tabix.

ENDIF.

ENDFORM. " f_get_event

*&----------------------------------------------------
-----------------*

*& Form f_alv_report_display

*&----------------------------------------------------
-----------------*

FORM f_alv_report_display .
* ALV report

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = sy-repid

is_layout = wa_layout

it_fieldcat = lt_fieldcat

it_sort = lt_sort

it_events = lt_events

TABLES

t_outtab = lt_ekpo

EXCEPTIONS

program_error = 1

OTHERS = 2.

IF sy-subrc <> 0.

ENDIF.

ENDFORM. " f_alv_report_display

*&----------------------------------------------------
-----------------*
*& Form subtotal_text

*&----------------------------------------------------
-----------------*

* Build subtotal text

*-----------------------------------------------------
-----------------*

FORM subtotal_text CHANGING

p_total TYPE any

p_subtot_text TYPE slis_subtot_text.

* Material level sub total

IF p_subtot_text-criteria = 'MATNR'.

p_subtot_text-display_text_for_subtotal =
'Material Level Sub-Total'.

ENDIF.

* Plant level sub total

IF p_subtot_text-criteria = 'WERKS'.

p_subtot_text-display_text_for_subtotal = 'Plant
Level Sub-Total'.
ENDIF.

ENDFORM. "subtotal_text
Output :

[adsenseyu1]

SALV: Total, Subtotal, Sort, Filter


Text Elements:
001 Select the Meta Data to be Changed
002 My New Heading
B01 Display Selections
B02 Set Rows
B03 Set Columns
B04 Current Cell
I02 Line
I03 Column
I04 Master
I05 Slave
I06 Single Click
I07 Double Click

Selection Texts:
P_AGGR Aggregation
P_AMOUNT Number of Data Records
P_DISP Global Display Settings
P_FCAT Column Information
P_FILT Filter
P_FULL Output as Fullscreen Grid
P_GRID Output as Grid
P_LIST Output as List
P_SORT Sort

*&---------------------------------------------------------------------*
*& Report SALV_DEMO_METADATA
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

report salv_demo_metadata no standard page heading.

*-----------------------------------------------------------------------
*... This report demonstrates the setting of metadata for ALV OM
* - cl_salv_table (Fullscreen Grid, Fullscreen List, Grid)
* the following steps have to be performed. Please go to the corresponding
* markers in the coding.
* If the table is empty, please create data for the demo by running report
* BCALV_GENERATE_ALV_T_T2
* �1 For a simple call, all you need is a table and a model class object
* �2 Just select your data in some table
* �3 Provide the data table to the model class' factory method,
* get the model class object and display the table
* �3a if you want to display the data in your own Dynpro, you have to give a
container to ALV
* �4 Change the default settings of the metadata
* �5 Offer the default set of ALV generic functions
*-----------------------------------------------------------------------

type-pools: col,
icon,
sym.

types: begin of g_type_s_test,


amount type i,
repid type syrepid,
display type i,
dynamic type sap_bool,
end of g_type_s_test.

constants: gc_true type sap_bool value 'X',

begin of gc_s_display,
list type i value 1,
fullscreen type i value 2,
grid type i value 3,
end of gc_s_display.

data: gs_test type g_type_s_test.

* �1 to use ALV OM, minimum is a table...


data: gt_outtab type standard table of alv_t_t2.
* ... and a model class object
data: gr_table type ref to cl_salv_table.
* if you want to display the data table on your own Dynpro, you also need a
container
data: gr_container type ref to cl_gui_custom_container.
data: g_okcode type syucomm.

*----------------------------------------------------------------------*
* SELECTION-SCREEN - for demonstration purposes only *
*----------------------------------------------------------------------*
selection-screen begin of block gen with frame.
parameters:
p_amount type i default 30.
selection-screen end of block gen.

selection-screen begin of block dsp with frame.


parameters:
p_full radiobutton group dsp,
p_list radiobutton group dsp,
p_grid radiobutton group dsp.
selection-screen end of block dsp.

selection-screen begin of block sel with frame title text-001.


parameters:
p_disp as checkbox,
p_fcat as checkbox,
p_sort as checkbox,
p_aggr as checkbox,
p_filt as checkbox.
selection-screen end of block sel.

*----------------------------------------------------------------------*
* START-OF-SELECTION *
*----------------------------------------------------------------------*
start-of-selection.
gs_test-amount = p_amount.
gs_test-repid = sy-repid.
case gc_true.
when p_list.
gs_test-display = gc_s_display-list.
when p_full.
gs_test-display = gc_s_display-fullscreen.
when p_grid.
gs_test-display = gc_s_display-grid.
endcase.
perform select_data.

*----------------------------------------------------------------------*
* END-OF-SELECTION *
*----------------------------------------------------------------------*
end-of-selection.
case gs_test-display.
when gc_s_display-fullscreen.
perform display_fullscreen.

when gc_s_display-grid.
perform display_grid.

when gc_s_display-list.
perform display_list.
endcase.

*&---------------------------------------------------------------------*
*& Form select_data
*&---------------------------------------------------------------------*
* �2 to display the data, you first have to select it in some table
*----------------------------------------------------------------------*
form select_data.

if gs_test-amount > 0.
select * from alv_t_t2 into corresponding fields of table gt_outtab
up to gs_test-amount rows. "#EC *
endif.

endform. " select_data

*&---------------------------------------------------------------------*
*& Form display_fullscreen
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form display_fullscreen .

*�3 create an ALV table - default is grid display ...


try.
cl_salv_table=>factory(
importing
r_salv_table = gr_table
changing
t_table = gt_outtab ).
catch cx_salv_msg.
endtry.

perform display.

endform. " display_fullscreen

*&---------------------------------------------------------------------*
*& Form display_list
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form display_list .

*�3 create an ALV table for list display ...


try.
cl_salv_table=>factory(
exporting
list_display = abap_true
importing
r_salv_table = gr_table
changing
t_table = gt_outtab ).
catch cx_salv_msg.
endtry.

perform display.

endform. " display_list


*&--------------------------------------------------------------------*
*& Form display_grid
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
form display_grid.

call screen 100.

endform. "display_grid

*&---------------------------------------------------------------------*
*& Module d0100_pbo OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module d0100_pbo output.
perform d0100_pbo.
endmodule. " d0100_pbo OUTPUT

*&---------------------------------------------------------------------*
*& Module d0100_pai INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module d0100_pai input.
perform d0100_pai.
endmodule. " d0100_pai INPUT

*&---------------------------------------------------------------------*
*& Form d0100_pbo
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form d0100_pbo .

set pf-status 'D0100'.

if gr_container is not bound.


*�3a for displaying the data in your own Dynpro, create a container ...
create object gr_container
exporting
container_name = 'CONTAINER'.

*�3 create an ALV table ...


try.
cl_salv_table=>factory(
exporting r_container = gr_container
importing
r_salv_table = gr_table
changing
t_table = gt_outtab ).
catch cx_salv_msg.
endtry.

perform display.

endif.

endform. " d0100_pbo

*&---------------------------------------------------------------------*
*& Form d0100_pai
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form d0100_pai .

case g_okcode.
when 'BACK' or 'EXIT' or 'CANC'.
set screen 0.
leave screen.
endcase.

endform. " d0100_pai

*&--------------------------------------------------------------------*
*& Form set_display_settings
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
form set_display_settings.

data:
ls_display type ref to cl_salv_display_settings.

ls_display = gr_table->get_display_settings( ).

try.
ls_display->set_list_header( text-002 ).
ls_display->set_vertical_lines( abap_false ).
ls_display->set_horizontal_lines( abap_false ).
ls_display->set_striped_pattern( abap_true ).
ls_display->set_list_header_size(
cl_salv_display_settings=>c_header_size_small ).
ls_display->set_suppress_empty_data( abap_true ).
catch cx_no_check.
endtry.

endform. "set_display_settings

*&--------------------------------------------------------------------*
*& Form set_columns
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
form set_columns.

data:
lr_columns type ref to cl_salv_columns, "global columns settings
lr_column type ref to cl_salv_column_table. "individual column setting

lr_columns = gr_table->get_columns( ).

lr_columns->set_optimize( abap_true ).

try.
lr_column ?= lr_columns->get_column( 'MANDT' ). "get a
single column
lr_column->set_technical( abap_true ). "Client column is a
technical column
catch cx_salv_not_found. "#EC NO_HANDLER
endtry.

try.
lr_column ?= lr_columns->get_column( 'SEATSOCC' ).
lr_column->set_visible( abap_false ).
catch cx_salv_not_found. "#EC NO_HANDLER
endtry.

* change the PRICE color color


data:
ls_color type lvc_s_colo.

try.
lr_column ?= lr_columns->get_column( 'PRICE' ).

ls_color-col = col_group.
ls_color-int = 1.

lr_column->set_color( ls_color ).
catch cx_salv_not_found. "#EC NO_HANDLER
endtry.

endform. "set_columns

*&--------------------------------------------------------------------*
*& Form set_sort
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
form set_sort.

data:
lr_sorts type ref to cl_salv_sorts. "sort information
lr_sorts = gr_table->get_sorts( ).

lr_sorts->clear( ). "remove all existing sort


setings

try.
lr_sorts->add_sort(
columnname = 'CARRID'
position = 1
sequence = if_salv_c_sort=>sort_up ).
catch cx_salv_not_found cx_salv_existing cx_salv_data_error."#EC
NO_HANDLER
endtry.
try.
lr_sorts->add_sort(
columnname = 'CONNID'
position = 1
subtotal = abap_true "applies only if some aggregations are
turned on
sequence = if_salv_c_sort=>sort_up ).
catch cx_salv_not_found cx_salv_existing cx_salv_data_error."#EC
NO_HANDLER
endtry.

endform. "set_sort

*&--------------------------------------------------------------------*
*& Form set_aggregations
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
form set_aggregations.

data:
lr_aggregations type ref to cl_salv_aggregations.

lr_aggregations = gr_table->get_aggregations( ).

lr_aggregations->clear( ).
try.
lr_aggregations->add_aggregation( columnname = 'PAYMENTSUM' ).
catch cx_salv_not_found cx_salv_data_error cx_salv_existing."#EC
NO_HANDLER
endtry.

endform. "set_aggregations

*&--------------------------------------------------------------------*
*& Form set_filter
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
form set_filter.

data:
lr_filters type ref to cl_salv_filters.

lr_filters = gr_table->get_filters( ).

lr_filters->clear( ).

try.
lr_filters->add_filter(
columnname = 'CURRENCY'
low = 'USD' ).
catch cx_salv_not_found cx_salv_data_error cx_salv_existing."#EC
NO_HANDLER
endtry.

endform. "set_filter

*&--------------------------------------------------------------------*
*& Form set_metadata
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
form set_metadata .
*�4 depending on the choices made in the lower frame of the selection
screen,
* metadata default settings are changed

if p_disp eq abap_true.
* metadate for global display settings
perform set_display_settings.
endif.

if p_fcat eq abap_true.
* metadata for columns
perform set_columns.
endif.

if p_sort eq abap_true.
* sort by company and flight number
perform set_sort.
endif.

if p_aggr eq abap_true.
* aggregate the paymentsum
perform set_aggregations.
endif.

if p_filt eq abap_true.
* filter by currency
perform set_filter.
endif.

endform. " set_metadata

*&---------------------------------------------------------------------*
*& Form display
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form display .

*�4 change the metadata default settings


perform set_metadata.
*�5 offer the default set of ALV generic funtions
data: lr_functions type ref to cl_salv_functions_list.

lr_functions = gr_table->get_functions( ).
lr_functions->set_default( ).

*... and display the table


gr_table->display( ).

endform. " display

Generating total and subtotal


(including currency wise total) in
ALV report
posted in ABAP ALV, ABAP Tutorials by vylykhang

To add a total or sub-total to an ALV report, we create a field catalog for the report (see Field
Catalog in ALV), then we set the field DO_SUM of field catalog for the field that we want to
calculate.
Example Program – Adding Total in ALV

*&———————————————————————*
*& Report Z01_DEMO_ALV_FIELDCATALOG
*&
*&———————————————————————*
*& Programmer: Vy Khang Ly
*& SAP FREE ABAP TUTORIALS
*& http://sapforbeginner.com
*&———————————————————————*
REPORT Z01_DEMO_ALV_FIELDCATALOG.
TYPE-POOLS: slis. ” SLIS contains all the ALV data types

*&———————————————————————*
*& Data Declaration
*&———————————————————————*
DATA: lt_sflight TYPE TABLE OF sflight.
DATA: lt_fieldcat TYPE slis_t_fieldcat_alv,
ls_fieldcat TYPE slis_fieldcat_alv.
DATA: g_repid TYPE sy-repid.

*&———————————————————————*
*& START-OF-SELECTION
*&———————————————————————*
START-OF-SELECTION.
g_repid = sy-repid.
*&—– Fetch data from the database —–*
SELECT *
FROM sflight
INTO TABLE lt_sflight.
*&—– Field Catalog —–*
ls_fieldcat-fieldname = ‘CARRID’.
ls_fieldcat-seltext_m = ‘Airline’.
APPEND ls_fieldcat TO lt_fieldcat.
ls_fieldcat-fieldname = ‘CONNID’.
ls_fieldcat-seltext_m = ‘FLight Number’.
APPEND ls_fieldcat TO lt_fieldcat.

ls_fieldcat-fieldname = ‘FLDATE’.
ls_fieldcat-seltext_m = ‘FLight Date’.
APPEND ls_fieldcat TO lt_fieldcat.

*Calculating total of bookings.


ls_fieldcat-fieldname = ‘PAYMENTSUM’.
ls_fieldcat-seltext_m = ‘Total of current bookings’.
ls_fieldcat-do_sum = ‘X’.
APPEND ls_fieldcat TO lt_fieldcat.
ls_fieldcat-fieldname = ‘CURRENCY’.
ls_fieldcat-seltext_m = ‘Currency’.
APPEND ls_fieldcat TO lt_fieldcat.

CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’


EXPORTING
it_fieldcat = lt_fieldcat
TABLES
t_outtab = lt_sflight.

At the end of report, we will see the Total is calculated.


Example Program – Adding Sub-total in ALV
To calculate subtotal in ALV, build sort catalog and set the field SUBTOT of sort catalog to ‘X’.

*&———————————————————————*
*& Report Z01_DEMO_ALV_FIELDCATALOG
*&
*&———————————————————————*
*& Programmer: Vy Khang Ly
*& SAP FREE ABAP TUTORIALS
*& http://sapforbeginner.com
*&———————————————————————*
REPORT Z01_DEMO_ALV_FIELDCATALOG.

TYPE-POOLS: slis. ” SLIS contains all the ALV data types

*&———————————————————————*
*& Data Declaration
*&———————————————————————*
DATA: lt_sflight TYPE TABLE OF sflight.
DATA: lt_fieldcat TYPE slis_t_fieldcat_alv,
ls_fieldcat TYPE slis_fieldcat_alv,
lt_sort TYPE slis_t_sortinfo_alv,
ls_sort TYPE slis_sortinfo_alv.
DATA: g_repid TYPE sy-repid.
*&———————————————————————*
*& START-OF-SELECTION
*&———————————————————————*
START-OF-SELECTION.
g_repid = sy-repid.
*&—– Fetch data from the database —–*
SELECT *
FROM sflight
INTO TABLE lt_sflight.
*&—– Field Catalog —–*
ls_fieldcat-fieldname = ‘CARRID’. “Fieldname in the data table
ls_fieldcat-seltext_m = ‘Airline’. “Column description in the output
APPEND ls_fieldcat TO lt_fieldcat.
ls_fieldcat-fieldname = ‘CONNID’.
ls_fieldcat-seltext_m = ‘FLight Number’.
APPEND ls_fieldcat TO lt_fieldcat.

ls_fieldcat-fieldname = ‘FLDATE’.
ls_fieldcat-seltext_m = ‘FLight Date’.
APPEND ls_fieldcat TO lt_fieldcat.

*Calculating total of bookings.


ls_fieldcat-fieldname = ‘PAYMENTSUM’.
ls_fieldcat-seltext_m = ‘Total of current bookings’.
ls_fieldcat-do_sum = ‘X’.
APPEND ls_fieldcat TO lt_fieldcat.
ls_fieldcat-fieldname = ‘CURRENCY’.
ls_fieldcat-seltext_m = ‘Currency’.
APPEND ls_fieldcat TO lt_fieldcat.

*Build a sort catalog to generating sub-total


ls_sort-spos = 1.
ls_sort-fieldname = ‘FLDATE’.
ls_sort-up = ‘X’.
ls_sort-subtot = ‘X’.
APPEND ls_sort TO lt_sort.
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’
EXPORTING
it_fieldcat = lt_fieldcat
it_sort = lt_sort
TABLES
t_outtab = lt_sflight.
At the end of report, we will see the Total is calculated. And sorting by each day, there are subtotals
of bookings by date.
** Normally, there is enough for adding total of any numeric field. But in case of Currency and
Quantity, there are determined or sorted by different types of currency and quantity. As the
picture above, the total of bookings added without identifying the dependent field. There is an
additional code adding to field catalog in this case:
*Calculating total of bookings.
ls_fieldcat-fieldname = ‘PAYMENTSUM’.
ls_fieldcat-seltext_m = ‘Total of current bookings’.
ls_fieldcat-do_sum = ‘X’.
ls_fieldcat-cfieldname = ‘CURRENCY’.
APPEND ls_fieldcat TO lt_fieldcat.
ls_fieldcat-fieldname = ‘CURRENCY’.
ls_fieldcat-seltext_m = ‘Currency’.
APPEND ls_fieldcat TO lt_fieldcat.

And we have another result:


Now we have a more relevant ALV Report. Applying this rule same on the Quantity fields (Kg vs
Lb).

Anda mungkin juga menyukai