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: 

Automatic Sum At the Bottom Or At the Top of an ALV Grid

Former Member
0 Kudos

Hi!

Suppose I have an internal table Gt_itab such as

Sales Doc Gross Value Discount Net Value Cost of Goods Profit Profit Margin

[--


] [
] [

] [

] [

] [

] [
--


]

S1 120 20 100 70 30 %30

.

.

.

I use ALV Grid to display that internal table.

My Questions:

1) How can I get the sum of the Columns (Gross Value,Net Value,Cost of Goods,Profit) and Also Calculate

the Profit Margin for the bottom line automaticly by the ABAP code w/o a user ?

2) It is possible to show the Summation Line at the top of the Grid ?

3) Is it possible to show the list provided that it is Sorted Descending for the Profit Column ?

Kind Regards.

Erkan VAROL

1 ACCEPTED SOLUTION

Former Member
0 Kudos

check this report

BCALV_TEST_HTML_HEADER

4 REPLIES 4

faisal_altaf2
Active Contributor
0 Kudos

Hi, Koleksiyon

The following sample code will help you and you will be able to get the answers of all you question from that example.

REPORT  zfsl_alv_test.

TYPES: BEGIN OF t_it,
  mname(10),
  amount1 TYPE p,
  amount2 TYPE p,
END OF t_it.

*&---------------------------------------------------------------------*
*TABLES : it1_sum.
*&---------------------------------------------------------------------*
TYPE-POOLS : slis.
*&---------------------------------------------------------------------*

*&---------------------------------------------------------------------*
*VARIABLES
DATA : check(1),
       rep_id TYPE sy-repid.
*&---------------------------------------------------------------------*
*INTERNAL TABLE TYPE OF t_name
DATA: it1_sum TYPE STANDARD TABLE OF t_it WITH HEADER LINE,
      wa_it1_sum TYPE t_it,
      it2_sum TYPE STANDARD TABLE OF t_it WITH HEADER LINE,
      wa_it2_sum TYPE t_it.
*&---------------------------------------------------------------------*
*List Header
DATA : it_listheader TYPE STANDARD TABLE OF slis_listheader WITH HEADER LINE,
       wa_listheader TYPE slis_listheader.
*&---------------------------------------------------------------------*
*Event Raising to Display Heading.
DATA: event TYPE slis_t_event,
      event_str TYPE slis_alv_event.        "Event String.
*&---------------------------------------------------------------------*
*FIELD CATALOG
DATA : it_field TYPE slis_t_fieldcat_alv,
       wa_field TYPE slis_fieldcat_alv.
*&---------------------------------------------------------------------*
*SORTING
DATA : it_sort TYPE slis_t_sortinfo_alv,
       wa_sort TYPE slis_sortinfo_alv.
*&---------------------------------------------------------------------*
*Layout
DATA : wa_layout TYPE slis_layout_alv.
*&---------------------------------------------------------------------*
INITIALIZATION.
  check = 'X'.
  rep_id = sy-repid.
*&---------------------------------------------------------------------*

START-OF-SELECTION.

  wa_it1_sum-mname = 'BBB'.
  wa_it1_sum-amount1 = '-500'.
  wa_it1_sum-amount2 = '-200'.
  APPEND wa_it1_sum  TO it1_sum.
  wa_it1_sum-mname = 'BBB'.
  wa_it1_sum-amount1 = 100.
  wa_it1_sum-amount2 = 200.
  APPEND wa_it1_sum  TO it1_sum.
  wa_it1_sum-mname = 'CCC'.
  wa_it1_sum-amount1 = 500.
  wa_it1_sum-amount2 = 10000.
  APPEND wa_it1_sum  TO it1_sum.
  wa_it1_sum-mname = 'CCC'.
  wa_it1_sum-amount1 = 105000.
  wa_it1_sum-amount2 = 20500.
  APPEND wa_it1_sum  TO it1_sum.
  wa_it1_sum-mname = 'AAA'.
  wa_it1_sum-amount1 = 21000.
  wa_it1_sum-amount2 = 22000.
  APPEND wa_it1_sum  TO it1_sum.
  wa_it1_sum-mname = 'BBB'.
  wa_it1_sum-amount1 = 5500.
  wa_it1_sum-amount2 = 2200.
  APPEND wa_it1_sum  TO it1_sum.

*&---------------------------------------------------------------------*
*          Heading
*&---------------------------------------------------------------------*
  wa_listheader-typ = 'H'.
  wa_listheader-info = 'ALV TEST Top-of-Page'.
  APPEND wa_listheader TO it_listheader.

*&---------------------------------------------------------------------*
*          FIELD CATALOG
*&---------------------------------------------------------------------*
  wa_field-col_pos = 1 .
  wa_field-tabname = 'IT1_SUM'.
  wa_field-fieldname = 'MNAME'.
  wa_field-seltext_m = 'Material Number'.
*ls_fcat-do_sum = 'X'.
  APPEND wa_field TO it_field.
  CLEAR wa_field.

  wa_field-col_pos = 2 .
  wa_field-tabname = 'IT1_SUM'.
  wa_field-fieldname = 'AMOUNT1'.
  wa_field-seltext_m = 'Amount 1'.
  wa_field-do_sum = check.
  wa_field-edit_mask = 'V___________'.  " Use this to place the (-) Minus Signe on the left side
*ls_fcat-do_sum = 'X'.
  APPEND wa_field TO it_field.
  CLEAR wa_field.

  wa_field-col_pos = 3 .
  wa_field-tabname = 'IT1_SUM'.
  wa_field-fieldname = 'AMOUNT1'.
  wa_field-seltext_m = 'Amount 2'.
  wa_field-do_sum = check.
  wa_field-edit_mask = 'V___________'.
*ls_fcat-do_sum = 'X'.
  APPEND wa_field TO it_field.
  CLEAR wa_field.

*&---------------------------------------------------------------------*
*          SORT By MNAME
*&---------------------------------------------------------------------*
  wa_sort-spos = 1.
  wa_sort-fieldname = 'MNAME'.
  wa_sort-tabname = 'IT1_SUM'.
  wa_sort-up = check. " Here Use u2018downu2019 for descending Order
  wa_sort-subtot = check.
  APPEND wa_sort TO it_sort.
  CLEAR wa_sort.

*&---------------------------------------------------------------------*
*          ALV Layout Setting
*&---------------------------------------------------------------------*
  wa_layout-zebra = 'X'.
  wa_layout-colwidth_optimize = 'X'.

*&---------------------------------------------------------------------*
*          Calling function to raise event to display heading and icon above ALV
*&---------------------------------------------------------------------*
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
   EXPORTING
     i_list_type           = 0
   IMPORTING
     et_events             = event[]
* EXCEPTIONS
*   LIST_TYPE_WRONG       = 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.

*&---------------------------------------------------------------------*
*          Calling TOP_OF_PAGE Event
*&---------------------------------------------------------------------*
  READ TABLE event WITH KEY name = 'TOP_OF_PAGE' INTO event_str.
  IF sy-subrc = 0.
    MOVE: 'TOP_OF_PAGE' TO event_str-form.
    APPEND event_str TO event.
  ENDIF.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
   EXPORTING
*   I_INTERFACE_CHECK                 = ' '
*   I_BYPASSING_BUFFER                = ' '
*   I_BUFFER_ACTIVE                   = ' '
     i_callback_program                = sy-cprog
*   I_CALLBACK_PF_STATUS_SET          = ' '
*   I_CALLBACK_USER_COMMAND           = ' '
*   I_CALLBACK_TOP_OF_PAGE            = ' '
*   I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*   I_CALLBACK_HTML_END_OF_LIST       = ' '
*   I_STRUCTURE_NAME                  =
*   I_BACKGROUND_ID                   = ' '
     i_grid_title                      = 'ALV Test'
*   I_GRID_SETTINGS                   =
     is_layout                         = wa_layout
     it_fieldcat                       = it_field
*   IT_EXCLUDING                      =
*   IT_SPECIAL_GROUPS                 =
     it_sort                           = it_sort
*   IT_FILTER                         =
*   IS_SEL_HIDE                       =
     i_default                         = 'X'
     i_save                            = 'X'
*   IS_VARIANT                        =
     it_events                         = event[]
*   IT_EVENT_EXIT                     =
*   IS_PRINT                          =
*   IS_REPREP_ID                      =
*   I_SCREEN_START_COLUMN             = 0
*   I_SCREEN_START_LINE               = 0
*   I_SCREEN_END_COLUMN               = 0
*   I_SCREEN_END_LINE                 = 0
*   I_HTML_HEIGHT_TOP                 = 0
*   I_HTML_HEIGHT_END                 = 0
*   IT_ALV_GRAPHICS                   =
*   IT_HYPERLINK                      =
*   IT_ADD_FIELDCAT                   =
*   IT_EXCEPT_QINFO                   =
*   IR_SALV_FULLSCREEN_ADAPTER        =
* IMPORTING
*   E_EXIT_CAUSED_BY_CALLER           =
*   ES_EXIT_CAUSED_BY_USER            =
    TABLES
      t_outtab                          = it1_sum
* 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.

*&---------------------------------------------------------------------*
*&      Form  top_of_page
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM top_of_page.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary       = it_listheader[]
      i_logo                   = 'ENJOYSAP_LOGO'
*   I_END_OF_LIST_GRID       =
*   I_ALV_FORM               =
            .
ENDFORM.           "top_of_page

Kind Regards,

Faisal

Former Member
0 Kudos

searcg SCN...ull get lots of posts on sum and sort on ALV

Former Member
0 Kudos

hi

in fieldcatalog include this

ls_fcat-do_sum = 'X'.

Former Member
0 Kudos

check this report

BCALV_TEST_HTML_HEADER