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: 

Numeric field total in ALV

Former Member
0 Kudos

Hi Experts,

I have one numeric field in ALV report, i want the subtotal of that numeric field, how i can do that.

IF i am using do_sum that is not working, it is giving interger fields total not numeric fields total?

Urgent,

useful reward points.....

4 REPLIES 4

former_member181995
Active Contributor
0 Kudos

Use Type p decimals xx insted of numeric.

Amit.

Former Member
0 Kudos

What I generally do in case I want sub-totals is that after say n sublines, when I want to insert my sub-total line, i calculate the sub-total till that point and then insert a 'TOTALS' line in the internal table to be displayed.

Eg >

data : sub_tot like itab-num_fld1.

loop at itab into ls_itab.

on change of...... (or at new...)

ls_disp-col_text = 'TOTAL'.

ls_disp-num_fld1 = sub_tot.

ls_disp-color = 'C310'. " YELLOW ,

" color(4) TYPE c,

append ls_disp to display_itab.

clear sub_tot.

endon.

sub_tot = sub_tot + ls_itab-num_fld1.

endloop.

While filing the layout, pass

ls_layout-info_fieldname = 'COLOR'.

this will not only display sub-totals properly, but in diff color too , to differentiate that line from the rest of them.

Do let me knw if this helped.

Pranjal.

Former Member
0 Kudos

Hi,

In ur case u have to use the following logic

isort type standard table of lvc_s_sort.

wa_fieldcat-col_pos = 1.

wa_fieldcat-fieldname = 'MATNR'.

wa_fieldcat-tabname = 'IFINAL'.

wa_fieldcat-coltext = text-007.

wa_fieldcat-do_sum = 'X'.

APPEND wa_fieldcat TO ifieldcat.

clear wa_fieldcat.

now u have to pass this table isort to the

FM or class used for ALV

wa_sort-spos = '1'.

wa_sort-fieldname = 'MATNR'.

wa_sort-up = abap_true.

wa_sort-subtot = abap_true.

APPEND wa_sort to isort.

clear wa_sort.

Hope this helps.

Regds,

Seema

Former Member
0 Kudos

Hi

i am sending a sample code for alv subtotals.you go through it.

REPORT z_alv_subtotal.

&----


*& Table declaration

&----


TABLES: EKKO.

&----


*& Type pool declaration

&----


TYPE-POOLS: SLIS. " Type pool for ALV

&----


*& Selection screen

&----


SELECT-OPTIONS: S_EBELN FOR EKKO-EBELN.

&----


*& Type declaration

&----


  • Type declaration for internal table to store EKPO data

TYPES: BEGIN OF X_DATA,

EBELN TYPE CHAR30, " Document no.

EBELP TYPE EBELP, " Item no

MATNR TYPE MATNR, " Material no

MATNR1 TYPE MATNR, " Material no

WERKS TYPE WERKS_D, " Plant

WERKS1 TYPE WERKS_D, " Plant

NTGEW TYPE ENTGE, " Net weight

GEWE TYPE EGEWE, " Unit of weight

END OF X_DATA.

&----


*& Internal table declaration

&----


DATA:

  • Internal table to store EKPO data

I_EKPO TYPE STANDARD TABLE OF X_DATA INITIAL SIZE 0,

  • Internal table for storing field catalog information

I_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,

  • Internal table for Top of Page info. in ALV Display

I_ALV_TOP_OF_PAGE TYPE SLIS_T_LISTHEADER,

  • Internal table for ALV Display events

I_EVENTS TYPE SLIS_T_EVENT,

  • Internal table for storing ALV sort information

I_SORT TYPE SLIS_T_SORTINFO_ALV,

I_EVENT TYPE SLIS_T_EVENT.

&----


*& Work area declaration

&----


DATA:

WA_EKKO TYPE X_DATA,

WA_LAYOUT TYPE SLIS_LAYOUT_ALV,

WA_EVENTS TYPE SLIS_ALV_EVENT,

WA_SORT TYPE SLIS_SORTINFO_ALV.

&----


*& Constant declaration

&----


CONSTANTS:

C_HEADER TYPE CHAR1

VALUE 'H', "Header in ALV

C_ITEM TYPE CHAR1

VALUE 'S'.

&----


*& Start-of-selection event

&----


START-OF-SELECTION.

  • Select data from ekpo

SELECT EBELN " Doc no

EBELP " Item

MATNR " Material

MATNR " Material

WERKS " Plant

WERKS " Plant

NTGEW " Quantity

GEWEI " Unit

FROM EKPO

INTO TABLE I_EKPO

WHERE EBELN IN S_EBELN

AND NTGEW NE '0.00'.

IF SY-SUBRC = 0.

SORT I_EKPO BY EBELN EBELP MATNR .

ENDIF.

  • To build the Page header

PERFORM SUB_BUILD_HEADER.

  • To prepare field catalog

PERFORM SUB_FIELD_CATALOG.

  • Perform to populate the layout structure

PERFORM SUB_POPULATE_LAYOUT.

  • Perform to populate the sort table.

PERFORM SUB_POPULATE_SORT.

  • Perform to populate ALV event

PERFORM SUB_GET_EVENT.

END-OF-SELECTION.

  • Perform to display ALV report

PERFORM SUB_ALV_REPORT_DISPLAY.

&----


*& Form sub_build_header

&----


  • To build the header

----


  • No Parameter

----


FORM SUB_BUILD_HEADER .

  • Local data declaration

DATA: L_SYSTEM TYPE CHAR10 , "System id

L_R_LINE TYPE SLIS_LISTHEADER, "Hold list header

L_DATE TYPE CHAR10, "Date

L_TIME TYPE CHAR10, "Time

L_SUCCESS_RECORDS TYPE I, "No of success records

L_TITLE(300) TYPE C. " Title

  • Title Display

L_R_LINE-TYP = C_HEADER. " header

L_TITLE = 'Test report'(001).

L_R_LINE-INFO = L_TITLE.

APPEND L_R_LINE TO I_ALV_TOP_OF_PAGE.

CLEAR L_R_LINE.

  • Run date Display

CLEAR L_DATE.

L_R_LINE-TYP = C_ITEM. " Item

WRITE: SY-DATUM TO L_DATE MM/DD/YYYY.

L_R_LINE-KEY = 'Run Date :'(002).

L_R_LINE-INFO = L_DATE.

APPEND L_R_LINE TO I_ALV_TOP_OF_PAGE.

CLEAR: L_R_LINE,

L_DATE.ENDFORM. " sub_build_header

&----


*& Form sub_field_catalog

&----


  • Build Field Catalog

----


  • No Parameter

----


FORM SUB_FIELD_CATALOG .

  • Build Field Catalog

PERFORM SUB_FILL_ALV_FIELD_CATALOG USING:

'01' '01' 'EBELN' 'I_EKPO' 'L' 'Doc No'(003) ' ' ' ' ' ' ' ',

'01' '02' 'EBELP' 'I_EKPO' 'L' 'Item No'(004) 'X' 'X' ' ' ' ',

'01' '03' 'MATNR' 'I_EKPO' 'L' 'Material No'(005) 'X' 'X' ' ' ' ',

'01' '03' 'MATNR1' 'I_EKPO' 'L' 'Material No'(005) ' ' ' ' ' ' ' ',

'01' '04' 'WERKS' 'I_EKPO' 'L' 'Plant'(006) 'X' 'X' ' ' ' ',

'01' '04' 'WERKS1' 'I_EKPO' 'L' 'Plant'(006) ' ' ' ' ' ' ' ',

'01' '05' 'NTGEW' 'I_EKPO' 'R' 'Net Weight'(007) ' ' ' ' 'GEWE' 'I_EKPO'

.

ENDFORM. " sub_field_catalog

&----


*& Form sub_fill_alv_field_catalog

&----


*& For building Field Catalog

&----


*& p_rowpos Row position

*& p_colpos Col position

*& p_fldnam Fldname

*& p_tabnam Tabname

*& p_justif Justification

*& p_seltext Seltext

*& p_out no out

*& p_tech Technical field

*& p_qfield Quantity field

*& p_qtab Quantity table

&----


FORM SUB_FILL_ALV_FIELD_CATALOG USING P_ROWPOS TYPE SYCUROW

P_COLPOS TYPE SYCUCOL

P_FLDNAM TYPE FIELDNAME

P_TABNAM TYPE TABNAME

P_JUSTIF TYPE CHAR1

P_SELTEXT TYPE DD03P-SCRTEXT_L

P_OUT TYPE CHAR1

P_TECH TYPE CHAR1

P_QFIELD TYPE SLIS_FIELDNAME

P_QTAB TYPE SLIS_TABNAME.

  • Local declaration for field catalog

DATA: WA_LFL_FCAT TYPE SLIS_FIELDCAT_ALV.

WA_LFL_FCAT-ROW_POS = P_ROWPOS. "Row

WA_LFL_FCAT-COL_POS = P_COLPOS. "Column

WA_LFL_FCAT-FIELDNAME = P_FLDNAM. "Field Name

WA_LFL_FCAT-TABNAME = P_TABNAM. "Internal Table Name

WA_LFL_FCAT-JUST = P_JUSTIF. "Screen Justified

WA_LFL_FCAT-SELTEXT_L = P_SELTEXT. "Field Text

WA_LFL_FCAT-NO_OUT = P_OUT. "No output

WA_LFL_FCAT-TECH = P_TECH. "Technical field

WA_LFL_FCAT-QFIELDNAME = P_QFIELD. "Quantity unit

WA_LFL_FCAT-QTABNAME = P_QTAB . "Quantity table

IF P_FLDNAM = 'NTGEW'.

WA_LFL_FCAT-DO_SUM = 'X'.

ENDIF.

APPEND WA_LFL_FCAT TO I_FIELDCAT.

CLEAR WA_LFL_FCAT.

ENDFORM. " sub_fill_alv_field_catalog

&----


*& Form sub_populate_layout

&----


  • Populate ALV layout

----


  • No Parameter

----


FORM SUB_POPULATE_LAYOUT . CLEAR WA_LAYOUT.

WA_LAYOUT-COLWIDTH_OPTIMIZE = 'X'." Optimization of Col width

ENDFORM. " sub_populate_layout

&----


*& Form sub_populate_sort

&----


  • Populate ALV sort table

----


  • No Parameter

----


FORM SUB_POPULATE_SORT .

  • Sort on material

WA_SORT-SPOS = '01' .

WA_SORT-FIELDNAME = 'MATNR'.

WA_SORT-TABNAME = 'I_EKPO'.

WA_SORT-UP = 'X'.

WA_SORT-SUBTOT = 'X'.

APPEND WA_SORT TO I_SORT .

CLEAR WA_SORT.

  • Sort on plant

WA_SORT-SPOS = '02'.

WA_SORT-FIELDNAME = 'WERKS'.

WA_SORT-TABNAME = 'I_EKPO'.

WA_SORT-UP = 'X'.

WA_SORT-SUBTOT = 'X'.

APPEND WA_SORT TO I_SORT .

CLEAR WA_SORT.

ENDFORM. " sub_populate_sort

&----


*& Form sub_get_event

&----


  • Get ALV grid event and pass the form name to subtotal_text

  • event

----


  • No Parameter

----


FORM SUB_GET_EVENT .

CONSTANTS : C_FORMNAME_SUBTOTAL_TEXT TYPE SLIS_FORMNAME VALUE

'SUBTOTAL_TEXT'.

DATA: L_S_EVENT TYPE SLIS_ALV_EVENT.

CALL FUNCTION 'REUSE_ALV_EVENTS_GET'

EXPORTING

I_LIST_TYPE = 4

IMPORTING

ET_EVENTS = I_EVENT

EXCEPTIONS

LIST_TYPE_WRONG = 0

OTHERS = 0.

  • Subtotal

READ TABLE I_EVENT INTO L_S_EVENT

WITH KEY NAME = SLIS_EV_SUBTOTAL_TEXT.

IF SY-SUBRC = 0.

MOVE C_FORMNAME_SUBTOTAL_TEXT TO L_S_EVENT-FORM.

MODIFY I_EVENT FROM L_S_EVENT INDEX SY-TABIX.

ENDIF.

ENDFORM. " sub_get_event

&----


*& Form sub_alv_report_display

&----


  • For ALV Report Display

----


  • No Parameter

----


FORM SUB_ALV_REPORT_DISPLAY .

DATA: L_REPID TYPE SYREPID .

L_REPID = SY-REPID .

  • This function module for displaying the ALV report

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = L_REPID

I_CALLBACK_TOP_OF_PAGE = 'SUB_ALV_TOP_OF_PAGE'

IS_LAYOUT = WA_LAYOUT

IT_FIELDCAT = I_FIELDCAT

IT_SORT = I_SORT

IT_EVENTS = I_EVENT

I_DEFAULT = 'X'

I_SAVE = 'A'

TABLES

T_OUTTAB = I_EKPO

EXCEPTIONS

PROGRAM_ERROR = 1

OTHERS = 2.

IF SY-SUBRC <> 0.

  • MESSAGE i000 WITH 'Error in ALV report display'(055).

ENDIF.ENDFORM. " sub_alv_report_display

&----


  • FORM sub_alv_top_of_page

----


  • Call ALV top of page

----


  • No parameter

----


FORM SUB_ALV_TOP_OF_PAGE. "#EC CALLED

  • To write header for the ALV

CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'

EXPORTING

IT_LIST_COMMENTARY = I_ALV_TOP_OF_PAGE.

ENDFORM. "alv_top_of_page

&----


*& Form subtotal_text

&----


  • Build subtotal text

----


  • P_total Total

  • p_subtot_text Subtotal text info

----


FORM SUBTOTAL_TEXT CHANGING

P_TOTAL TYPE ANY

P_SUBTOT_TEXT TYPE SLIS_SUBTOT_TEXT.

  • Material level sub total

IF P_SUBTOT_TEXT-CRITERIA = 'MATNR'.

P_SUBTOT_TEXT-DISPLAY_TEXT_FOR_SUBTOTAL

= 'Material level total'(009).

ENDIF.

  • Plant level sub total

IF P_SUBTOT_TEXT-CRITERIA = 'WERKS'.

P_SUBTOT_TEXT-DISPLAY_TEXT_FOR_SUBTOTAL = 'Plant level total'(010).

ENDIF.

ENDFORM. "subtotal_text