Register Login

SubTotal in ALV GRID

Updated May 18, 2018

The code below produces alv display with 2 columns: field1 & field2.

field1 is character based & the int. table passed to the alv is sorted on this field.

field2 is numeric, whose subtotals are desired.

so while populating the gt_sort table the option gs_sort-subtot is set for field1 bcoz the subtotals are to be based on the groups existing in this column.whereas the same option is NOT set for field2, even though the subtotal appears in that column.i have marked these 2 statements in the code for your convenience.

execute the code below & you will surely understand what i said above.

bye!!


report zalv10.


type-pools: slis.

data: g_repid like sy-repid,
gs_print type slis_print_alv,
gt_list_top_of_page type slis_t_listheader,
gt_events type slis_t_event,
gt_sort type slis_t_sortinfo_alv,
gs_layout type slis_layout_alv,
gt_fieldcat type slis_t_fieldcat_alv,
fieldcat_ln like line of gt_fieldcat,
col_pos type i.

data: begin of itab,
field1(5) type c,
field2(5) type c,
field3(5) type p decimals 2,
end of itab.

data: begin of itab1 occurs 0.
include structure itab.
data: end of itab1.

data: begin of itab_fieldcat occurs 0.
include structure itab.
data: end of itab_fieldcat.

* Print Parameters
parameters:
p_print as checkbox default ' ', "PRINT IMMEDIATE
p_nosinf as checkbox default 'X', "NO SELECTION INFO
p_nocove as checkbox default ' ', "NO COVER PAGE
p_nonewp as checkbox default ' ', "NO NEW PAGE
p_nolinf as checkbox default 'X', "NO PRINT LIST INFO
p_reserv type i. "NO OF FOOTER LINE

initialization.
g_repid = sy-repid.
perform print_build using gs_print. "Print PARAMETERS

start-of-selection.
* TEST DATA
move 'TEST1' to itab1-field1.
move 'TEST1' to itab1-field2.
move '10.00' to itab1-field3.
append itab1.

move 'TEST2' to itab1-field1.
move 'TEST2' to itab1-field2.
move '20.00' to itab1-field3.
append itab1.

do 50 times.
append itab1.
enddo.

end-of-selection.

perform build.
perform eventtab_build changing gt_events.
perform comment_build changing gt_list_top_of_page.
perform call_alv.

form build.
* DATA FIELD CATALOG
* Explain Field Description to ALV
data: fieldcat_in type slis_fieldcat_alv.

clear fieldcat_in.
fieldcat_ln-fieldname = 'FIELD1'.
fieldcat_ln-tabname = 'ITAB1'.
*FIELDCAT_LN-NO_OUT = 'X'. "FIELD NOT DISPLAY, CHOOSE FROM LAYOUT
fieldcat_ln-key = ' '. "SUBTOTAL KEY
fieldcat_ln-no_out = ' '.
fieldcat_ln-seltext_l = 'HEAD1'.
append fieldcat_ln to gt_fieldcat.

clear fieldcat_in.
fieldcat_ln-fieldname = 'FIELD2'.
fieldcat_ln-tabname = 'ITAB1'.
fieldcat_ln-no_out = 'X'.
fieldcat_ln-seltext_l = 'HEAD2'.
append fieldcat_ln to gt_fieldcat.

clear fieldcat_in.
fieldcat_ln-fieldname = 'FIELD3'.
fieldcat_ln-tabname = 'ITAB1'.
fieldcat_ln-ref_fieldname = 'MENGE'. "<- REF FIELD IN THE DICTIONNARY
fieldcat_ln-ref_tabname = 'MSEG'. "<- REF TABLE IN THE DICTIONNARY
fieldcat_ln-no_out = ' '.
fieldcat_ln-do_sum = 'X'. "SUM UPON DISPLAY
append fieldcat_ln to gt_fieldcat.

* DATA SORTING AND SUBTOTAL
data: gs_sort type slis_sortinfo_alv.

clear gs_sort.
gs_sort-fieldname = 'FIELD1'.
gs_sort-spos = 1.
gs_sort-up = 'X'.
gs_sort-subtot = 'X'. ****CRUCIAL STATEMENT*****
append gs_sort to gt_sort.

clear gs_sort.
gs_sort-fieldname = 'FIELD2'.
gs_sort-spos = 2.
gs_sort-up = 'X'.
*GS_SORT-SUBTOT = 'X'. ***THIS SHOULD NOT BE UNCOMENTED**
append gs_sort to gt_sort.

endform.

form call_alv.
* ABAP List Viewer
call function 'REUSE_ALV_LIST_DISPLAY'
exporting
* I_INTERFACE_CHECK = ' '
* I_BYPASSING_BUFFER =
* I_BUFFER_ACTIVE = ' '
i_callback_program = g_repid
* I_CALLBACK_PF_STATUS_SET = ' '
* I_CALLBACK_USER_COMMAND = ' '
i_structure_name = 'ITAB1'
is_layout = gs_layout
it_fieldcat = gt_fieldcat[]
* IT_EXCLUDING =
* IT_SPECIAL_GROUPS =
it_sort = gt_sort[]
* IT_FILTER =
* IS_SEL_HIDE =
* I_DEFAULT = 'X'
* I_SAVE = ' '
* IS_VARIANT =
it_events = gt_events[]
* IT_EVENT_EXIT =
is_print = gs_print
* IS_REPREP_ID =
* I_SCREEN_START_COLUMN = 0
* I_SCREEN_START_LINE = 0
* I_SCREEN_END_COLUMN = 0
* I_SCREEN_END_LINE = 0
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER =
* ES_EXIT_CAUSED_BY_USER =
tables
t_outtab = itab1
exceptions
program_error = 1
others = 2.
endform.

* HEADER FORM
form eventtab_build changing lt_events type slis_t_event.
constants:
gc_formname_top_of_page type slis_formname value 'TOP_OF_PAGE'.
*GC_FORMNAME_END_OF_PAGE TYPE SLIS_FORMNAME VALUE 'END_OF_PAGE'.

data: ls_event type slis_alv_event.

call function 'REUSE_ALV_EVENTS_GET'
exporting
i_list_type = 0
importing
et_events = lt_events.

read table lt_events with key name = slis_ev_top_of_page
into ls_event.
if sy-subrc = 0.
move gc_formname_top_of_page to ls_event-form.
append ls_event to lt_events.
endif.

* define END_OF_PAGE event
* READ TABLE LT_EVENTS WITH KEY NAME = SLIS_EV_END_OF_PAGE
* INTO LS_EVENT.
* IF SY-SUBRC = 0.
* MOVE GC_FORMNAME_END_OF_PAGE TO LS_EVENT-FORM.
* APPEND LS_EVENT TO LT_EVENTS.
* ENDIF.
endform.

form comment_build changing gt_top_of_page type slis_t_listheader.
data: gs_line type slis_listheader.

clear gs_line.
gs_line-typ = 'H'.
gs_line-info = 'HEADER 1'.
append gs_line to gt_top_of_page.

clear gs_line.
gs_line-typ = 'S'.
gs_line-key = 'STATUS 1'.
gs_line-info = 'INFO 1'.
append gs_line to gt_top_of_page.
gs_line-key = 'STATUS 2'.
gs_line-info = 'INFO 2'.
append gs_line to gt_top_of_page.

* CLEAR GS_LINE.
* GS_LINE-TYP = 'A'.
*
* GS_LINE-INFO = 'ACTION'.
* APPEND GS_LINE TO GT_TOP_OF_PAGE.

endform.

form top_of_page.
call function 'REUSE_ALV_COMMENTARY_WRITE'
exporting
it_list_commentary = gt_list_top_of_page.
write: sy-datum, 'Page No', sy-pagno left-justified.
endform.

form end_of_page.
write at (sy-linsz) sy-pagno centered.
endform.

* PRINT SETTINGS
form print_build using ls_print type slis_print_alv.
ls_print-print = p_print. "PRINT IMMEDIATE
ls_print-no_print_selinfos = p_nosinf. "NO SELECTION INFO
ls_print-no_coverpage = p_nocove. "NO COVER PAGE
ls_print-no_new_page = p_nonewp.
ls_print-no_print_listinfos = p_nolinf. "NO PRINT LIST INFO
ls_print-reserve_lines = p_reserv.

endform.


×