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: 

ALV OO - Custom calculation at total and subtotals

Former Member
0 Kudos

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.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

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

5 REPLIES 5

agnihotro_sinha2
Active Contributor
0 Kudos

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.

Former Member
0 Kudos

do one thing.

Remove the Sort and Filter buttons from the ALV display.

Below link will help you to remove the buttons from ALV.

0 Kudos

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!!!

Former Member
0 Kudos

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

0 Kudos

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.