Application Development and Automation 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: 
Read only

Doing ALV subtotal programatically

Former Member
0 Likes
452

Hi,

I have a requirement where I need to display an ALV with subtotal and totals done programatically(Without a need by application user to select a column and choose Total / Subtotal option provided by ALV manually).

Please direct me to proper help document.

Here is an example

Vendor Site Quantity Value

AA India 12 3,000

AA China 10 2,000

AA Subtotal 22 5,000 -


>Subtotal Row

BB India 12 3,000

BB China 10 2,000

BB Subtotal 22 5,000 -


>Subtotal Row

Total 44 10,000 -


>Total Row

Regards,

Dhana

3 REPLIES 3
Read only

Former Member
0 Likes
409

Welcome to SDNn Dhanasekar,

As per SDN rules, you should search SDN before posting the question

Read only

0 Likes
409

Hi

Change in field catalog field do_sum for all columns you want to sum:

LOOP AT it_fcat INTO wa_fcat.

CASE wa_fcat-fieldname.

WHEN 'BETRW' OR 'BETRW1' OR 'SCTAX'.

wa_fcat-do_sum = 'X'.

MODIFY it_fcat FROM wa_fcat.

ENDCASE.

ENDLOOP.

In layout you should check this option

gs_layout-totals_bef = 'X'.

In sort you should sort by field you want

ls_sort-spos = num .

ls_sort-fieldname = field .

ls_sort-up = up . "A to Z

ls_sort-down = space .

ls_sort-expa = 'X'.

ls_sort-subtot = 'X'.

APPEND ls_sort TO lt_sort .

Best Regards

Yossi

Read only

Former Member
0 Likes
409

Hi Dhanasekar,

Showing subtotal in ALV by programmin claculation is done by through this way:

This is just an example....

REFRESH gt_sort.
  CLEAR wa_sort.
  wa_sort-fieldname = 'fieldname'.<-----that you want to sort and subtotal
  wa_sort-up = 'X'.
  wa_sort-subtot = 'X'.
  wa_sort-tabname = 'IT_OUTPUT'.
  APPEND wa_sort TO gt_sort.
 
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
   EXPORTING
    i_callback_program         = g_repid
    i_callback_top_of_page  = 'TOP_OF_PAGE'
    is_layout                          = gt_layout
    it_fieldcat                         = it_fcat
    it_sort                           = gt_sort[]
    TABLES
      t_outtab                      = it_output.


At last in field catalog pass  
wa_fcat-do_sum    = 'X'

Furthermore i am providing you one live example:

report  zabc message-id zz.

type-pools: slis.
data: begin of itab occurs 0,
       wtgbtr1  type coep-wtgbtr,
       twaer1   type coep-twaer,
    end of itab.

data: ls_fieldcat type slis_fieldcat_alv,
      g_t_fieldcat type slis_t_fieldcat_alv,
      g_s_layout type slis_layout_alv,
      g_s_sort type slis_t_sortinfo_alv,
      gs_sort type slis_sortinfo_alv.

  ls_fieldcat-fieldname = text-022.
  ls_fieldcat-seltext_l = text-023.
  ls_fieldcat-do_sum = ''.
  ls_fieldcat-no_sum = 'x'.
  append ls_fieldcat to g_t_fieldcat.


  clear ls_fieldcat.
  ls_fieldcat-fieldname = text-020.
  ls_fieldcat-seltext_l = text-021.
  ls_fieldcat-do_sum = 'X'.
  ls_fieldcat-no_sum = 'x'.
  append ls_fieldcat to g_t_fieldcat.


gs_sort-fieldname = 'TWAER1'.
gs_sort-spos = 2.
gs_sort-up = 'X'.
gs_sort-subtot = 'X'.
append gs_sort to g_s_sort.


itab-twaer1 = 'USD'.
itab-wtgbtr1 = '200'.
append itab.

itab-twaer1 = 'USD'.
itab-wtgbtr1 = '300'.
append itab.

itab-twaer1 = 'USD'.
itab-wtgbtr1 = '400'.
append itab.

itab-twaer1 = 'USD'.
itab-wtgbtr1 = '500'.
append itab.

itab-twaer1 = 'USD'.
itab-wtgbtr1 = '600'.
append itab.


itab-twaer1 = 'USD'.
itab-wtgbtr1 = '700'.
append itab.

itab-twaer1 = 'PHP'.
itab-wtgbtr1 = '200'.
append itab.


itab-twaer1 = 'PHP'.
itab-wtgbtr1 = '300'.
append itab.

itab-twaer1 = 'PHP'.
itab-wtgbtr1 = '400'.
append itab.

itab-twaer1 = 'PHP'.
itab-wtgbtr1 = '500'.
append itab.

itab-twaer1 = 'PHP'.
itab-wtgbtr1 = '600'.
append itab.


itab-twaer1 = 'PHP'.
itab-wtgbtr1 = '700'.
append itab.

sort itab by twaer1.

call function 'REUSE_ALV_GRID_DISPLAY'
 exporting
*   IS_LAYOUT                         = g_s_layout
   it_fieldcat                       = g_t_fieldcat
   it_sort                           = g_s_sort
  tables
    t_outtab                          = itab
 exceptions
   program_error                     = 1
   others                            = 2
          .
if sy-subrc <> 0.
 message id sy-msgid type sy-msgty number sy-msgno
         with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

May it solves your problem.

Thanks

Deepak Sharma