Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Can we modify a sub-total in ALV

Former Member
0 Kudos

Hi All,

I have a requirement for ALV where following are my values

for eg-

CVAL PVAL DIFF PERCENTAGE

0.000 587.000 0.000 100.0-

0.000 463.000 0.000 100.0-

0.000 181.000 0.000 100.0-

Subtotal

0.000 1,231.000 0.000 300.0-

Now my calculation for percentage is

((CVAL - (PVAL + DIFF)) / (PVAL + DIFF) ) * 100

So my percentage here (-100) is correct for individual rows

but my ALV GRID subtotal says (-300) which is incorrect.

Any idea how to go for this

Regards,

DNP

1 ACCEPTED SOLUTION

naimesh_patel
Active Contributor

Yes it is possible.

First, you need to get the ALV object from the ALV function module. You can use the FM GET_GLOBALS_FROM_SLVC_FULLSCR to get the ALV object.

Second, you need to Get the Subtotal from that ALV object using the GET_SUBTOTALS method. Get the table ep_collect01.

Third, you need to modify the subtotal. For this you need to take help of the field-symbols.

Fourth (and last) you need to referesh the ALV display by calling method REFRESH_TABLE_DISPLAY.

You can check out this blog for more information: [Classical ALV: Change Subtotal.|http://help-abap.blogspot.com/2008/09/classical-alv-change-subtotal.html]

Regards,

Naimesh Patel

11 REPLIES 11

Former Member
0 Kudos

1.calculate percentages manually for the subtotals and append the record at the end. or

2.use subtot = 'A'. which will calculate the average of the percentages.but there will still variance.

naimesh_patel
Active Contributor

Yes it is possible.

First, you need to get the ALV object from the ALV function module. You can use the FM GET_GLOBALS_FROM_SLVC_FULLSCR to get the ALV object.

Second, you need to Get the Subtotal from that ALV object using the GET_SUBTOTALS method. Get the table ep_collect01.

Third, you need to modify the subtotal. For this you need to take help of the field-symbols.

Fourth (and last) you need to referesh the ALV display by calling method REFRESH_TABLE_DISPLAY.

You can check out this blog for more information: [Classical ALV: Change Subtotal.|http://help-abap.blogspot.com/2008/09/classical-alv-change-subtotal.html]

Regards,

Naimesh Patel

0 Kudos

Hi Naimesh,

Your reply was indeed valuable and I was able to change sub-total. But, Now I guess for totals I'll have to use the FM LVC_TOTALS_GET.

Thanks for your inputs.

Regards,

DNP

0 Kudos

Yes you are right, as GET_TOTAL is private and it doesn't have anything in implementation.

Regards,

Naimesh Patel

0 Kudos

Hi Naimesh,

I tried using the fn module LVC_TOTALS_GET but couldn't get the totals. Can you help me with that. This is how I used the fn

CALL FUNCTION 'LVC_TOTALS_GET'

EXPORTING

IT_SORT = LT_SORT

IS_LAYOUT = f_grid_layout

IT_FILTER_INDEX = fil_ind

TABLES

IT_DATA = GT_REPORT

ET_COLLECT00 = <lt_ct00>

ET_COLLECT01 = <lt_ct01>

ET_COLLECT02 = <lt_ct02>

ET_COLLECT03 = <lt_ct03>

ET_COLLECT04 = <lt_ct04>

ET_COLLECT05 = <lt_ct05>

ET_COLLECT06 = <lt_ct06>

ET_COLLECT07 = <lt_ct07>

ET_COLLECT08 = <lt_ct08>

ET_COLLECT09 = <lt_ct09>

CHANGING

CS_TOTAL_OPTIONS = ZTOTAL

CT_FIELDCAT = f_fieldcat

CT_GROUPLEVELS = ZGRP_LEVEL

C_CALCULATE_TOTALS = CAL_TOT.

  • C_SUMLEVEL =

Regards,

DNP

0 Kudos

Hi,

I was able to achieve it by using get_subtotals method.

Regards,

DNP

0 Kudos

Hi DNP.

How did you change the row totals in ALV using the Method get_subtotals??

Any help will be grateful. Greetings...

0 Kudos

HI.

I am successful in changing the sub-total in ALV. However, when i click the list output or i try to download the data to excel, the changes i made was not reflected. Please let me know what I need to do to achieve my requirements. Thanks.

Former Member
0 Kudos

Totals can be obtained by filling the table IT_SORT

wa_sort-spos = 1.

wa_sort-fieldname = <FIELD NAME>

wa_sort-SUBTOT = 'X'. "subtotals any totals column by this field

gd_sortcat-tabname

APPEND wa_sort TO it_sortcat.

If we want to perform the totals :

Use this

IT_FIELDCAT-DO_SUM = 'X'.

former_member188685
Active Contributor
0 Kudos

yes it possible to do that..

just check this sample code..

REPORT ZTEST_ALV_TEXT .
type-pools : slis.
 
types : begin of itab_t,
ebeln like ekpo-ebeln,
lifnr like ekko-lifnr,
ekorg like ekko-ekorg,
ekgrp like ekko-ekgrp,
werks like ekpo-werks,
ebelp like ekpo-ebelp,
matnr like ekpo-matnr,
menge like ekpo-menge,
netpr like ekpo-netpr,
d, "Dummy field to fire the Subtotal text event
end of itab_t.
 
data: itab type table of itab_t.
data: tab type itab_t.
data : itab1 like eket occurs 0 with header line.
 
data: t_fcat type slis_t_fieldcat_alv,
it_sort type slis_t_sortinfo_alv,
t_events type slis_t_event,
listhead type slis_t_listheader,
ls_layout type slis_layout_alv.
 
start-of-selection.
 
select a~ebeln
a~lifnr
a~ekorg
a~ekgrp
b~werks
b~ebelp
b~matnr
b~menge
b~netpr
up to 100 rows
into corresponding fields of table itab
from ekko as a inner join ekpo as b
on a~ebeln = b~ebeln.
 
end-of-selection.
 
perform fill_fcat using t_fcat.
 
perform fill_event using t_events.
 
perform fill_layout.
 
perform display.
 
&--------------------------------------------------------------------
*& Form fill_fcat
&--------------------------------------------------------------------
 
form fill_fcat using p_t_fcat type slis_t_fieldcat_alv.
 
data : lfcat type slis_fieldcat_alv,
 
colpos type i value '0'.
 
data : ls_sort type slis_sortinfo_alv.
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'EBELN'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'EBELN'.
lfcat-ref_tabname = 'EKKO'.
lfcat-hotspot = 'X'.
append lfcat to p_t_fcat.
clear lfcat.
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'LIFNR'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'LIFNR'.
lfcat-ref_tabname = 'EKKO'.
append lfcat to p_t_fcat.
clear lfcat.
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'EKORG'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'EKORG'.
lfcat-ref_tabname = 'EKKO'.
append lfcat to p_t_fcat.
clear lfcat.
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'EKGRP'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'EKGRP'.
lfcat-ref_tabname = 'EKKO'.
append lfcat to p_t_fcat.
clear lfcat.
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'WERKS'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'WERKS'.
lfcat-ref_tabname = 'EKPO'.
append lfcat to p_t_fcat.
clear lfcat.
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'EBELP'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'EBELP'.
lfcat-ref_tabname = 'EKPO'.
append lfcat to p_t_fcat.
clear lfcat.
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'MATNR'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'MATNR'.
lfcat-ref_tabname = 'EKPO'.
append lfcat to p_t_fcat.
clear lfcat.
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'MENGE'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'MENGE'.
lfcat-ref_tabname = 'EKPO'.
lfcat-do_sum = 'X'.
append lfcat to p_t_fcat.
clear lfcat.
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'NETPR'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'NETPR'.
lfcat-ref_tabname = 'EKPO'.
lfcat-do_sum = 'X'.
append lfcat to p_t_fcat.
clear lfcat.
 
 
colpos = colpos + 1.
lfcat-col_pos = colpos.
lfcat-fieldname = 'D'.
lfcat-tabname = 'ITAB'.
lfcat-ref_fieldname = 'EBELN'.
lfcat-ref_tabname = 'EKKO'.
lfcat-no_out = 'X'.
append lfcat to p_t_fcat.
clear lfcat.
"this is important for getting the subtotal 
ls_sort-spos = 1.
ls_sort-fieldname = 'EBELN'.
ls_sort-tabname = 'ITAB'.
ls_sort-up = 'X'.
ls_sort-group = 'UL'.
 
 
append ls_sort to it_sort.
clear ls_sort.
 
ls_sort-spos = 2.
ls_sort-fieldname = 'D'.
ls_sort-tabname = 'ITAB'.
ls_sort-up = 'X'.
ls_sort-group = 'UL'.
ls_sort-subtot = 'X'.
 
append ls_sort to it_sort.
endform. " fill_fcat
 
&--------------------------------------------------------------------
*& Form fill_event
&--------------------------------------------------------------------
form fill_event using p_t_events type slis_t_event.
 
data : ls_event type slis_alv_event.
 
call function 'REUSE_ALV_EVENTS_GET'
exporting
i_list_type = 0
importing
et_events = p_t_events
 
EXCEPTIONS 
LIST_TYPE_WRONG = 1 
OTHERS = 2 
.
if sy-subrc ne  0.
 
 
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO 
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. 
endif.
 
read table p_t_events with key name = slis_ev_top_of_page
into ls_event.
if sy-subrc = 0.
move 'TOP_OF_PAGE' to ls_event-form.
append ls_event to p_t_events.
endif.
 
read table p_t_events with key name = SLIS_EV_SUBTOTAL_TEXT
into ls_event.
if sy-subrc = 0.
move 'SUBTOTAL' to ls_event-form.
append ls_event to p_t_events.
endif.
 
endform. " FILL_EVENT
FORM SUBTOTAL USING I_LISTHEAD STRUCTURE tab
I_SUBTOTAL TYPE SLIS_SUBTOT_TEXT.
 "here you can modify the subtotal line....
*criteria type slis_fieldname,
 
keyword like dd03p-reptext, 
criteria_text(255) type c, 
max_len like dd03p-outputlen, 
display_text_for_subtotal(255) type c, 
if I_SUBTOTAL-criteria = 'D'.
I_SUBTOTAL-display_text_for_subtotal = 'Sub total'.
 
endif.
 
ENDFORM.
&--------------------------------------------------------------------
*& Form TOP_OF_PAGE
&--------------------------------------------------------------------

form top_of_page.
 
data : s_listhead type slis_listheader.
 
clear s_listhead.
s_listhead-typ = 'H'.
s_listhead-info = 'SIMPLE REPORT'.
append s_listhead to listhead.
 
s_listhead-typ = 'S'.
s_listhead-key = 'EBELN'.
s_listhead-info = 'ALV'.
append s_listhead to listhead.
 
call function 'REUSE_ALV_COMMENTARY_WRITE'
exporting
it_list_commentary = listhead
i_logo = 'ENJOYSAP_LOGO'
 
 
I_END_OF_LIST_GRID = 
.
endform. "TOP_OF_PAGE
 
&---------------------------------------------------------------------
*& Form fill_layout
&---------------------------------------------------------------------
form fill_layout .
 
ls_layout-zebra = 'X'.
ls_layout-detail_popup = 'X'.
ls_layout-key_hotspot = 'X'.
ls_layout-window_titlebar = 'Dharma'.
ls_layout-detail_titlebar = 'Jasti'.
ls_layout-totals_text = 'GRAND TOTAL'.
ls_layout-subtotals_text = 'SUB'.
 
endform. " fill_layout

form display .
call function 'REUSE_ALV_LIST_DISPLAY'
exporting
i_callback_program = sy-repid
is_layout = ls_layout
it_fieldcat = t_fcat
it_sort = it_sort[]
it_events = t_events
tables
t_outtab = itab
 
EXCEPTIONS 
PROGRAM_ERROR = 1 
OTHERS = 2 
.
if sy-subrc ne 0.
 
MESSAGE ID SY-MSGI D TYPE SY-MSGTY NUMBER SY-MSGNO 
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. 
endif.
endform. " display

0 Kudos

HI. Rahul.

Thanks for the idea. But i do used the modify line. Please see below:

CHECK sender = gobj_alv_200.

CHECK no_iter IS INITIAL.

CALL METHOD gobj_alv_200->get_subtotals

IMPORTING

ep_collect00 = ref_total00

ep_collect01 = ref_total01

ep_collect02 = ref_total02

ep_collect03 = ref_total03

ep_collect04 = ref_total04

ep_collect05 = ref_total05

ep_collect06 = ref_total06

ep_collect07 = ref_total07

ep_collect08 = ref_total08

ep_collect09 = ref_total09

et_grouplevels = lt_levels.

  • ASSIGN ref_total01->* TO <total>.

DO 3 TIMES.

CASE sy-index.

WHEN 01.

ASSIGN ref_total00->* TO <total>.

WHEN 02.

ASSIGN ref_total01->* TO <total>.

WHEN 03.

ASSIGN ref_total02->* TO <total>.

ENDCASE.

LOOP AT <total> INTO record.

CLEAR: record-zperbc, record-zperba.

IF NOT record-zbudyr IS INITIAL.

record-zperbc = ( record-zdiffbc / record-zbudyr ) * 100.

ENDIF.

IF NOT record-zlastyr IS INITIAL.

record-zperba = ( record-zdiffba / record-zlastyr ) * 100.

ENDIF.

MODIFY <total> FROM record. "from <ff_field>.

ENDLOOP.

ENDDO.

no_iter = 'X'.

CALL METHOD gobj_alv_200->refresh_table_display

EXPORTING

i_soft_refresh = 'X'.

CLEAR no_iter.

Please let me know how i will be able to save my changes. Thanks.