03-12-2009 10:56 AM
Hi all!
I need to change the way that SAP uses to calculate the totals (and subtotals) at ALV grid using OO.
I've gone half the way: I can change the subtotals and totals for first show. But if user changes the sort, or applies a filter,... totals and subtotals are updated by SAP...
What is the event that I can register to so that I can call my code to change the formula for totals and subtotals!?
Can anyone help?!
Best regards,
Carlos Constantino.
08-03-2015 12:58 PM
Hi,
This code model is used to calculate the custom subtotals in ALV OO.
data lo_grid TYPE REF TO cl_gui_alv_grid.
data refr_co(1) type c.
class lcl_event_receiver definition.
public section.
methods handle_after_refresh
for event after_refresh of cl_gui_alv_grid.
endclass.
class lcl_event_receiver implementation.
method handle_after_refresh.
if refr_co is initial.
perform calc_total.
endif.
endmethod.
endclass.
data event_receiver type ref to lcl_event_receiver.
.....
create object event_receiver.
set handler event_receiver->handle_after_refresh for lo_grid.
call method lo_grid->register_edit_event
exporting
i_event_id = cl_gui_alv_grid=>mc_evt_modified.
perform calc_total.
endif.
.........
form calc_total.
* get subtotal fileds
data it_fieldcatalog type lvc_t_fcat with header line.
call method lo_grid->get_frontend_fieldcatalog
importing
et_fieldcatalog = it_fieldcatalog[].
read table it_fieldcatalog with key fieldname = 'FIELD_PERCENT'
do_sum = 'X'.
if sy-subrc ne 0.
return.
endif.
read table it_fieldcatalog with key fieldname = 'FIELD_VAL'
do_sum = 'X'.
if sy-subrc ne 0.
return.
endif.
read table it_fieldcatalog with key fieldname = 'FIELD_DIV'
do_sum = 'X'.
if sy-subrc ne 0.
return.
endif.
* get sub totals for total, first subtotal, second subtotal
data: it_00 type ref to data,
it_01 type ref to data,
it_02 type ref to data,
inc type i.
call method lo_grid->get_subtotals
importing
ep_collect00 = it_00
ep_collect01 = it_01
ep_collect02 = it_02.
field-symbols: <ft_tab> type any table,
<fs_tab> type any,
<ff_percent> type any,
<ff_val> type any,
<ff_div> type any.
do 3 times.
inc = sy-index - 1.
case inc.
when 0.
assign it_00->* to <ft_tab>.
when 1.
assign it_01->* to <ft_tab>.
when 2.
assign it_02->* to <ft_tab>.
endcase.
* modifying subtotals
loop at <ft_tab> assigning <fs_tab>.
assign component 'FIELD_PERCENT' of structure <fs_tab>
to <ff_percent>.
assign component 'FIELD_VAL' of structure <fs_tab>
to <ff_val>.
assign component 'FIELD_DIV' of structure <fs_tab>
to <ff_div>.
if <ff_val> ne 0 and <ff_div> ne 0.
<ff_percent> = ( ( <ff_val> / <ff_div> ) - 1 ) * 100.
else.
<ff_percent> = 0.
endif.
endloop.
enddo.
refr_co = 'X'.
call method lo_grid->refresh_table_display
exporting
i_soft_refresh = 'X'
exceptions
finished = 1
others = 2.
clear refr_co.
endform. " calc_total
03-12-2009 11:08 AM
hi,
Sort functions should be blocked when using Subtotals in ALV grid. When this subtotals are calculated manually i.e. in the loop of the internal table we cant use Sort.
I dnt think there is other option to it.
Blocking can be done using a EXCLUDE table n pass it to ALV.
03-12-2009 11:09 AM
03-12-2009 11:17 AM
Ok, you are telling me that there is no way to solve my problem. Only avoid it.
I'll make the protections (so that the totals don't need to bee recalculated) but this solution Isn't the solution... this is only a workaround and if anyone have a hint on how to acomplish it I wold apreciate it a lot!!!
08-03-2015 12:58 PM
Hi,
This code model is used to calculate the custom subtotals in ALV OO.
data lo_grid TYPE REF TO cl_gui_alv_grid.
data refr_co(1) type c.
class lcl_event_receiver definition.
public section.
methods handle_after_refresh
for event after_refresh of cl_gui_alv_grid.
endclass.
class lcl_event_receiver implementation.
method handle_after_refresh.
if refr_co is initial.
perform calc_total.
endif.
endmethod.
endclass.
data event_receiver type ref to lcl_event_receiver.
.....
create object event_receiver.
set handler event_receiver->handle_after_refresh for lo_grid.
call method lo_grid->register_edit_event
exporting
i_event_id = cl_gui_alv_grid=>mc_evt_modified.
perform calc_total.
endif.
.........
form calc_total.
* get subtotal fileds
data it_fieldcatalog type lvc_t_fcat with header line.
call method lo_grid->get_frontend_fieldcatalog
importing
et_fieldcatalog = it_fieldcatalog[].
read table it_fieldcatalog with key fieldname = 'FIELD_PERCENT'
do_sum = 'X'.
if sy-subrc ne 0.
return.
endif.
read table it_fieldcatalog with key fieldname = 'FIELD_VAL'
do_sum = 'X'.
if sy-subrc ne 0.
return.
endif.
read table it_fieldcatalog with key fieldname = 'FIELD_DIV'
do_sum = 'X'.
if sy-subrc ne 0.
return.
endif.
* get sub totals for total, first subtotal, second subtotal
data: it_00 type ref to data,
it_01 type ref to data,
it_02 type ref to data,
inc type i.
call method lo_grid->get_subtotals
importing
ep_collect00 = it_00
ep_collect01 = it_01
ep_collect02 = it_02.
field-symbols: <ft_tab> type any table,
<fs_tab> type any,
<ff_percent> type any,
<ff_val> type any,
<ff_div> type any.
do 3 times.
inc = sy-index - 1.
case inc.
when 0.
assign it_00->* to <ft_tab>.
when 1.
assign it_01->* to <ft_tab>.
when 2.
assign it_02->* to <ft_tab>.
endcase.
* modifying subtotals
loop at <ft_tab> assigning <fs_tab>.
assign component 'FIELD_PERCENT' of structure <fs_tab>
to <ff_percent>.
assign component 'FIELD_VAL' of structure <fs_tab>
to <ff_val>.
assign component 'FIELD_DIV' of structure <fs_tab>
to <ff_div>.
if <ff_val> ne 0 and <ff_div> ne 0.
<ff_percent> = ( ( <ff_val> / <ff_div> ) - 1 ) * 100.
else.
<ff_percent> = 0.
endif.
endloop.
enddo.
refr_co = 'X'.
call method lo_grid->refresh_table_display
exporting
i_soft_refresh = 'X'
exceptions
finished = 1
others = 2.
clear refr_co.
endform. " calc_total
10-09-2015 5:53 PM
Hi!
Although it's been a long time and I'm not working in that customer anymore, your sugestion appears to be a solution.
I haven't marked as a correct response because now I don't have time to test, or access that customer anymore...
Even so, thank U.