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

sum in grid

Former Member
0 Likes
723

Hi. I have a question. How to make such thing in grid:


record 1
record 2
...
record n
SUM 1
record n+1
...
record m
SUM 2
SUM 3 = SUM 1 + SUM 2

It's sth like this: http://www.erpgenie.com/sap/abap/controls/alvgri3.jpg

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
580

Hi,

This example shows the use of the method GET_SUBTOTALS.

report zkiran_0006.

tables: mara.

type-pools: slis, icon.

  • Internal Tables

data: begin of ialv occurs 0,

matnr type mara-matnr,

maktx type makt-maktx,

brgew type mara-brgew,

end of ialv .

data: alv_container type ref to cl_gui_custom_container,

alv_grid type ref to cl_gui_alv_grid,

ok_code like sy-ucomm,

layout type lvc_s_layo,

fieldcat type lvc_t_fcat.

select-options: s_matnr for mara-matnr.

start-of-selection.

perform get_data.

call screen 100.

************************************************************************

  • Module status_0100 OUTPUT

************************************************************************

module status_0100 output.

set pf-status '0100'.

set titlebar '0100'.

data: variant type disvariant.

variant-report = sy-repid.

variant-username = sy-uname.

  • Create Controls

create object alv_container

exporting container_name = 'ALV_CONTAINER'.

create object alv_grid

exporting i_parent = alv_container.

  • Populate Field Catalog

perform get_fieldcatalog.

call method alv_grid->set_table_for_first_display

exporting

is_layout = layout

is_variant = variant

i_save = 'U'

i_structure_name = 'IALV'

changing

it_outtab = ialv[]

it_fieldcatalog = fieldcat[].

endmodule.

************************************************************************

  • Module USER_COMMAND_0100 INPUT

************************************************************************

module user_command_0100 input.

case sy-ucomm.

when 'BACK' or 'CANC'.

perform free_containers.

if sy-subrc = 0.

set screen 0.

leave screen.

else.

leave program.

endif.

when 'EXIT'.

perform free_containers.

leave program.

when 'ENTER'.

data: grandtots type ref to data.

call method alv_grid->get_subtotals

importing

ep_collect00 = grandtots

  • EP_COLLECT01 =

  • EP_COLLECT02 =

  • EP_COLLECT03 =

  • EP_COLLECT04 =

  • EP_COLLECT05 =

  • EP_COLLECT06 =

  • EP_COLLECT07 =

  • EP_COLLECT08 =

  • EP_COLLECT09 =

  • ET_GROUPLEVELS =

.

check sy-subrc = 0.

endcase.

endmodule.

*********************************************************************

  • FORM GET_DATA.

*********************************************************************

form get_data.

select maramatnr marabrgew makt~maktx

into corresponding fields of table ialv

from mara

inner join makt

on maramatnr = maktmatnr

where mara~matnr in s_matnr

and makt~spras = sy-langu.

sort ialv ascending by matnr.

endform.

************************************************************************

  • Form FREE_CONTAINERS

************************************************************************

form free_containers.

if not alv_container is initial.

call method alv_container->free.

clear: alv_container.

free : alv_container.

endif.

endform.

************************************************************************

  • Form Get_Fieldcatalog - Set Up Columns/Headers

************************************************************************

form get_fieldcatalog.

data: ls_fcat type lvc_s_fcat.

data: columnno(3) type n value '0'.

refresh: fieldcat.

clear: ls_fcat.

ls_fcat-reptext = 'Material Number'.

ls_fcat-coltext = 'Material Number'.

ls_fcat-fieldname = 'MATNR'.

ls_fcat-ref_table = 'IALV'.

ls_fcat-outputlen = '18'.

ls_fcat-col_pos = 1.

append ls_fcat to fieldcat.

clear: ls_fcat.

ls_fcat-reptext = 'Material Description'.

ls_fcat-coltext = 'Material Description'.

ls_fcat-fieldname = 'MAKTX'.

ls_fcat-ref_table = 'IALV'.

ls_fcat-outputlen = '40'.

ls_fcat-col_pos = 2.

append ls_fcat to fieldcat.

clear: ls_fcat.

ls_fcat-reptext = 'Material Description'.

ls_fcat-coltext = 'Material Description'.

ls_fcat-fieldname = 'BRGEW'.

ls_fcat-ref_table = 'IALV'.

ls_fcat-do_sum = 'X'.

ls_fcat-just = 'R'.

ls_fcat-datatype = 'QUAN'.

ls_fcat-outputlen = '15'.

ls_fcat-col_pos = 2.

append ls_fcat to fieldcat.

endform.

Refer

https://forums.sdn.sap.com/click.jspa?searchID=4359508&messageID=1793643

https://forums.sdn.sap.com/click.jspa?searchID=4359508&messageID=1116661

Regards

Hi. I have a question. How to make such thing in grid:


record 1
record 2
...
record n
SUM 1
record n+1
...
record m
SUM 2
SUM 3 = SUM 1 + SUM 2

It's sth like this: http://www.erpgenie.com/sap/abap/controls/alvgri3.jpg

3 REPLIES 3
Read only

Former Member
0 Likes
580

I depends on what version of ALV you are using?

Are you using FM REUSE_ALV_LIST_DISPLAY?

Are you using class CL_GUI_ALV_GRID?

Are you using simplified ALV SALV class cl_salv_table?

There are different ways to do it based on what versin of ALV you are using:

1) REUSE_ALV_LIST_DISPLAY

Set up a sort table similar to the way that you set up a field catalog.

This identifies the fields that you want to sort and subtotal by:

Data: lt_slis_sort TYPE slis_t_sortinfo_alv WITH HEADER LINE.

data: lt_slis_fcat type slis_t_fieldcat_alv WITH HEADER LINE.

Load the appropriate fields and then call the FM:

lt_slis_sort-spos = 1. " Sort position

lt_slis_sort-fieldname = 'FIELD1'. " Name of field to sort on

lt_slis_sort-'GT-OUTTAB'. "internal table with ALV grid data

lt_slis_sort-up = 'X'. " X = sort ascending, blank otherwise

lt_slis_sort-down = ' '. " X = sort descending, blank otherwise

lt_slis_sort-subtot = ' X'. " X = subtotal on breaks of this field

etc -- see type pool SLIS for more info on available fields

Append lt_slis_sort.

For each field to be summed up when subtotaled are to display, set the do_sum flag in the field catalog:

lt_slis_fcat-do_sum = 'X'.

      call function 'REUSE_ALV_GRID_DISPLAY'
        exporting
          i_bypassing_buffer       = gs_test-bypassing_buffer
          i_buffer_active          = gs_test-buffer_active
          i_callback_program       = g_repid
          i_callback_pf_status_set = 'F01_ALV_EVENT_PF_STATUS_SET'
          i_callback_user_command  = 'F01_ALV_EVENT_USER_COMMAND'
          is_layout                = ls_slis_layo
          it_fieldcat              = lt_slis_fcat
          it_sort                  = lt_slis_sort
          it_filter                = lt_slis_filt
          i_default                = gs_test-vari_default
          i_save                   = gs_test-vari_save
          is_variant               = ls_vari
          is_print                 = ls_slis_prnt
        tables
          t_outtab                 = gt_outtab
        exceptions
          program_error            = 1
          others                   = 2.

In SE84 Information System, you can look up sample programs BALV*

For example, programs BALVST02 and BALVST02_GRID have sort code. You could adapt this to your needs.

-


The technique is similar for CL_GUI_ALV_GRID, but the type are different.

The sort table type for CL_GUI_ALV_GRID is LVC_T_SORT, the basic field names are the same.

The field catalog type is LVC_T_FCAT, and you would still use do_sum.

Pass the sort table in your method call:

    CALL METHOD alv_list_ref->set_table_for_first_display
      EXPORTING
        is_layout            = i_layout
        it_toolbar_excluding = i_toolbar_excl
      CHANGING
        it_outtab            = i_alv_grid_tab
        it_fieldcatalog      = i_fieldcat
        it_sort                 = sort_table.

-


If using SALV, it is a bit different, and there are methods to use:

CONSTANTS: gc_true        TYPE sap_bool VALUE 'X',
           gc_false       TYPE sap_bool VALUE ' '.

  DATA: lr_sorts            TYPE REF TO cl_salv_sorts.
  DATA: lr_agg              TYPE REF TO cl_salv_aggregations.

  TRY.
      lr_sorts = gr_table->get_sorts( ).
      lr_sorts->add_sort( columnname = 'FIELD1'     subtotal = gc_true ).
      lr_sorts->add_sort( columnname = 'FIELD2'    subtotal = gc_true ).

*etc, as many sort field as you need.  leave out the
*subtotal parameter if no subtotal on that sort.

    CATCH cx_salv_existing.                             "#EC NO_HANDLER
    CATCH cx_salv_not_found.                            "#EC NO_HANDLER
    CATCH cx_salv_data_error.                           "#EC NO_HANDLER
  ENDTRY.

*To set the fields to sum, do the following
  TRY.
      lr_agg = gr_table->get_aggregations( ).
      lr_agg->add_aggregation( 'SUM_FIELD1' ).
      lr_agg->add_aggregation( 'SUM_FIELD2' ).

*etc, as many sum fields as you need.  

    CATCH cx_salv_existing.                             "#EC NO_HANDLER
    CATCH cx_salv_not_found.                            "#EC NO_HANDLER
    CATCH cx_salv_data_error.                           "#EC NO_HANDLER
  ENDTRY.

good luck

brian

Message was edited by: Brian Sammond, fixed typos

Brian Sammond

Message was edited by:

Brian Sammond

Read only

Former Member
0 Likes
580

Hi friend here is the program for totalling as your screen shot shows ..

this is the fields defiend in the field catalog

  "fieldcatalog-do_sum      = 'X'.

" After executeing    do   sum  by every field   using the  ICON  sigma 
" with  small  sigma  .... so that it  will give your output ..

REPORT  zdemo_alvgrid                 .

TABLES:     ekko.

type-pools: slis.                                 "ALV Declarations
*Data Declaration
*----------------
TYPES: BEGIN OF t_ekko,
  ebeln TYPE ekpo-ebeln,
  ebelp TYPE ekpo-ebelp,
  statu TYPE ekpo-statu,
  aedat TYPE ekpo-aedat,
  matnr TYPE ekpo-matnr,
  menge TYPE ekpo-menge,
  meins TYPE ekpo-meins,
  netpr TYPE ekpo-netpr,
  peinh TYPE ekpo-peinh,
 END OF t_ekko.

DATA: it_ekko TYPE STANDARD TABLE OF t_ekko INITIAL SIZE 0,
      wa_ekko TYPE t_ekko.

*ALV data declarations
data: fieldcatalog type slis_t_fieldcat_alv with header line,
      gd_tab_group type slis_t_sp_group_alv,
      gd_layout    type slis_layout_alv,
      gd_repid     like sy-repid.


************************************************************************
*Start-of-selection.
START-OF-SELECTION.

perform data_retrieval.
perform build_fieldcatalog.
perform build_layout.
perform display_alv_report.


*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
*       Build Fieldcatalog for ALV Report
*----------------------------------------------------------------------*
form build_fieldcatalog.

* There are a number of ways to create a fieldcat.
* For the purpose of this example i will build the fieldcatalog manualy
* by populating the internal table fields individually and then
* appending the rows. This method can be the most time consuming but can
* also allow you  more control of the final product.

* Beware though, you need to ensure that all fields required are
* populated. When using some of functionality available via ALV, such as
* total. You may need to provide more information than if you were
* simply displaying the result
*               I.e. Field type may be required in-order for
*                    the 'TOTAL' function to work.

  fieldcatalog-fieldname   = 'EBELN'.
  fieldcatalog-seltext_m   = 'Purchase Order'.
  fieldcatalog-col_pos     = 0.
  fieldcatalog-outputlen   = 10.
  fieldcatalog-emphasize   = 'X'.
  fieldcatalog-key         = 'X'.
  fieldcatalog-do_sum      = 'X'.
*  fieldcatalog-no_zero     = 'X'.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'EBELP'.
  fieldcatalog-seltext_m   = 'PO Item'.
  fieldcatalog-col_pos     = 1.
  fieldcatalog-do_sum      = 'X'.

  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'STATU'.
  fieldcatalog-seltext_m   = 'Status'.
  fieldcatalog-col_pos     = 2.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'AEDAT'.
  fieldcatalog-seltext_m   = 'Item change date'.
  fieldcatalog-col_pos     = 3.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'MATNR'.
  fieldcatalog-seltext_m   = 'Material Number'.
  fieldcatalog-col_pos     = 4.
  fieldcatalog-do_sum      = 'X'.

  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'MENGE'.
  fieldcatalog-seltext_m   = 'PO quantity'.
  fieldcatalog-col_pos     = 5.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'MEINS'.
  fieldcatalog-seltext_m   = 'Order Unit'.
  fieldcatalog-col_pos     = 6.
  fieldcatalog-do_sum      = 'X'.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'NETPR'.
  fieldcatalog-seltext_m   = 'Net Price'.
  fieldcatalog-col_pos     = 7.
  fieldcatalog-outputlen   = 15.
  fieldcatalog-do_sum      = 'X'.        "Display column total
  fieldcatalog-datatype     = 'CURR'.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.

  fieldcatalog-fieldname   = 'PEINH'.
  fieldcatalog-seltext_m   = 'Price Unit'.
  fieldcatalog-col_pos     = 8.
  append fieldcatalog to fieldcatalog.
  clear  fieldcatalog.
endform.                    " BUILD_FIELDCATALOG


*&---------------------------------------------------------------------*
*&      Form  BUILD_LAYOUT
*&---------------------------------------------------------------------*
*       Build layout for ALV grid report
*----------------------------------------------------------------------*
form build_layout.
  gd_layout-no_input          = 'X'.
  gd_layout-colwidth_optimize = 'X'.
  gd_layout-totals_text       = 'Totals'(201).
*  gd_layout-totals_only        = 'X'.
*  gd_layout-f2code            = 'DISP'.  "Sets fcode for when double
*                                         "click(press f2)
*  gd_layout-zebra             = 'X'.
*  gd_layout-group_change_edit = 'X'.
*  gd_layout-header_text       = 'helllllo'.
endform.                    " BUILD_LAYOUT


*&---------------------------------------------------------------------*
*&      Form  DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
*       Display report using ALV grid
*----------------------------------------------------------------------*
form display_alv_report.
  gd_repid = sy-repid.
  call function 'REUSE_ALV_GRID_DISPLAY'
       exporting
            i_callback_program      = gd_repid
*            i_callback_top_of_page   = 'TOP-OF-PAGE'  "see FORM
*            i_callback_user_command = 'USER_COMMAND'
*            i_grid_title           = outtext
            is_layout               = gd_layout
            it_fieldcat             = fieldcatalog[]
*            it_special_groups       = gd_tabgroup
*            IT_EVENTS                = GT_XEVENTS
            i_save                  = 'X'
*            is_variant              = z_template

       tables
            t_outtab                = it_ekko
       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.
endform.                    " DISPLAY_ALV_REPORT


*&---------------------------------------------------------------------*
*&      Form  DATA_RETRIEVAL
*&---------------------------------------------------------------------*
*       Retrieve data form EKPO table and populate itab it_ekko
*----------------------------------------------------------------------*
form data_retrieval.

select ebeln ebelp statu aedat matnr menge meins netpr peinh
 up to 10 rows
  from ekpo
  into table it_ekko.
endform.                    " DATA_RETRIEVAL

reward points if it is usefull ...

Girish

Read only

Former Member
0 Likes
581

Hi,

This example shows the use of the method GET_SUBTOTALS.

report zkiran_0006.

tables: mara.

type-pools: slis, icon.

  • Internal Tables

data: begin of ialv occurs 0,

matnr type mara-matnr,

maktx type makt-maktx,

brgew type mara-brgew,

end of ialv .

data: alv_container type ref to cl_gui_custom_container,

alv_grid type ref to cl_gui_alv_grid,

ok_code like sy-ucomm,

layout type lvc_s_layo,

fieldcat type lvc_t_fcat.

select-options: s_matnr for mara-matnr.

start-of-selection.

perform get_data.

call screen 100.

************************************************************************

  • Module status_0100 OUTPUT

************************************************************************

module status_0100 output.

set pf-status '0100'.

set titlebar '0100'.

data: variant type disvariant.

variant-report = sy-repid.

variant-username = sy-uname.

  • Create Controls

create object alv_container

exporting container_name = 'ALV_CONTAINER'.

create object alv_grid

exporting i_parent = alv_container.

  • Populate Field Catalog

perform get_fieldcatalog.

call method alv_grid->set_table_for_first_display

exporting

is_layout = layout

is_variant = variant

i_save = 'U'

i_structure_name = 'IALV'

changing

it_outtab = ialv[]

it_fieldcatalog = fieldcat[].

endmodule.

************************************************************************

  • Module USER_COMMAND_0100 INPUT

************************************************************************

module user_command_0100 input.

case sy-ucomm.

when 'BACK' or 'CANC'.

perform free_containers.

if sy-subrc = 0.

set screen 0.

leave screen.

else.

leave program.

endif.

when 'EXIT'.

perform free_containers.

leave program.

when 'ENTER'.

data: grandtots type ref to data.

call method alv_grid->get_subtotals

importing

ep_collect00 = grandtots

  • EP_COLLECT01 =

  • EP_COLLECT02 =

  • EP_COLLECT03 =

  • EP_COLLECT04 =

  • EP_COLLECT05 =

  • EP_COLLECT06 =

  • EP_COLLECT07 =

  • EP_COLLECT08 =

  • EP_COLLECT09 =

  • ET_GROUPLEVELS =

.

check sy-subrc = 0.

endcase.

endmodule.

*********************************************************************

  • FORM GET_DATA.

*********************************************************************

form get_data.

select maramatnr marabrgew makt~maktx

into corresponding fields of table ialv

from mara

inner join makt

on maramatnr = maktmatnr

where mara~matnr in s_matnr

and makt~spras = sy-langu.

sort ialv ascending by matnr.

endform.

************************************************************************

  • Form FREE_CONTAINERS

************************************************************************

form free_containers.

if not alv_container is initial.

call method alv_container->free.

clear: alv_container.

free : alv_container.

endif.

endform.

************************************************************************

  • Form Get_Fieldcatalog - Set Up Columns/Headers

************************************************************************

form get_fieldcatalog.

data: ls_fcat type lvc_s_fcat.

data: columnno(3) type n value '0'.

refresh: fieldcat.

clear: ls_fcat.

ls_fcat-reptext = 'Material Number'.

ls_fcat-coltext = 'Material Number'.

ls_fcat-fieldname = 'MATNR'.

ls_fcat-ref_table = 'IALV'.

ls_fcat-outputlen = '18'.

ls_fcat-col_pos = 1.

append ls_fcat to fieldcat.

clear: ls_fcat.

ls_fcat-reptext = 'Material Description'.

ls_fcat-coltext = 'Material Description'.

ls_fcat-fieldname = 'MAKTX'.

ls_fcat-ref_table = 'IALV'.

ls_fcat-outputlen = '40'.

ls_fcat-col_pos = 2.

append ls_fcat to fieldcat.

clear: ls_fcat.

ls_fcat-reptext = 'Material Description'.

ls_fcat-coltext = 'Material Description'.

ls_fcat-fieldname = 'BRGEW'.

ls_fcat-ref_table = 'IALV'.

ls_fcat-do_sum = 'X'.

ls_fcat-just = 'R'.

ls_fcat-datatype = 'QUAN'.

ls_fcat-outputlen = '15'.

ls_fcat-col_pos = 2.

append ls_fcat to fieldcat.

endform.

Refer

https://forums.sdn.sap.com/click.jspa?searchID=4359508&messageID=1793643

https://forums.sdn.sap.com/click.jspa?searchID=4359508&messageID=1116661

Regards