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: 

totas and sub totals in ALV grid display

Former Member
0 Kudos

Hi all,

how can I get the total for an integer field in internal table. I am using the following code, but unable to display the total. Can anyone give the reason for the same.

Thank you,

Usha

REPORT ZTEST_SUB .

TABLES : vbak.

TYPE-POOLS: slis.

SELECT-OPTIONS : s_vkorg FOR vbak-vkorg,

s_kunnr FOR vbak-kunnr,

s_vbeln FOR vbak-vbeln.

SELECTION-SCREEN : SKIP,

BEGIN OF LINE,

COMMENT 5(27) v_1 FOR FIELD p_max.

PARAMETERS p_max(2) TYPE n DEFAULT '2' OBLIGATORY.

SELECTION-SCREEN END OF LINE.

DATA: ls_fieldcat TYPE slis_fieldcat_alv,

lt_fieldcat TYPE slis_t_fieldcat_alv,

lt_sort TYPE slis_t_sortinfo_alv,

ls_sort TYPE slis_sortinfo_alv,

ls_layout TYPE slis_layout_alv.

DATA: BEGIN OF gt_vbak OCCURS 0,

vkorg LIKE vbak-vkorg,

kunnr LIKE vbak-kunnr,

vbeln LIKE vbak-vbeln,

test type I,

END OF gt_vbak.

START-OF-SELECTION.

PERFORM f_read_data.

loop at gt_vbak.

gt_vbak-test = 1.

modify gt_vbak.

endloop.

PERFORM f_display_data.

FORM f_read_data.

SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_vbak

FROM vbak UP TO p_max ROWS WHERE

kunnr IN s_kunnr AND

vbeln IN s_vbeln AND

vkorg IN s_vkorg.

ENDFORM.

FORM f_display_data.

DEFINE m_fieldcat.

add 1 to ls_fieldcat-col_pos.

ls_fieldcat-fieldname = &1.

ls_fieldcat-ref_tabname = 'VBAK'.

ls_fieldcat-ref_FIELDname = &2.

ls_fieldcat-do_sum = &3.

ls_fieldcat-cfieldname = &4.

append ls_fieldcat to lt_fieldcat.

END-OF-DEFINITION.

DEFINE m_sort.

add 1 to ls_sort-spos.

ls_sort-fieldname = &1.

ls_sort-up = 'X'.

ls_sort-subtot = &2.

ls_sort-group = '*'.

append ls_sort to lt_sort.

END-OF-DEFINITION.

DATA: ls_fieldcat TYPE slis_fieldcat_alv,

lt_fieldcat TYPE slis_t_fieldcat_alv,

lt_sort TYPE slis_t_sortinfo_alv,

ls_sort TYPE slis_sortinfo_alv,

ls_layout TYPE slis_layout_alv.

m_fieldcat 'VKORG' 'VKORG' '' ''.

m_fieldcat 'KUNNR' '' '' ''.

m_fieldcat 'VBELN' '' '' ''.

m_fieldcat 'TEST' '' 'X' ''.

  • m_sort 'VKORG' ''.

  • m_sort 'KUNNR' ''.

  • m_sort 'VBELN' ''.

m_sort 'TEST' 'X'.

ls_layout-cell_merge = 'X'.

LS_LAYOUT-WINDOW_TITLEBAR = 'test window'.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

IS_LAYOUT = ls_layout

IT_FIELDCAT = lt_fieldcat

IT_SORT = lt_sort

  • IMPORTING

  • E_EXIT_CAUSED_BY_CALLER =

  • ES_EXIT_CAUSED_BY_USER =

TABLES

T_OUTTAB = gt_vbak

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.

4 REPLIES 4

Former Member

Former Member
0 Kudos

hi

REPORT ZTEST_SUB .

TABLES : vbak.

TYPE-POOLS: slis.

SELECT-OPTIONS : s_vkorg FOR vbak-vkorg,

s_kunnr FOR vbak-kunnr,

s_vbeln FOR vbak-vbeln.

SELECTION-SCREEN : SKIP,

BEGIN OF LINE,

COMMENT 5(27) v_1 FOR FIELD p_max.

PARAMETERS p_max(2) TYPE n DEFAULT '2' OBLIGATORY.

SELECTION-SCREEN END OF LINE.

DATA: ls_fieldcat TYPE slis_fieldcat_alv,

lt_fieldcat TYPE slis_t_fieldcat_alv,

lt_sort TYPE slis_t_sortinfo_alv,

ls_sort TYPE slis_sortinfo_alv,

ls_layout TYPE slis_layout_alv.

DATA: BEGIN OF gt_vbak OCCURS 0,

vkorg LIKE vbak-vkorg,

kunnr LIKE vbak-kunnr,

vbeln LIKE vbak-vbeln,

test type I,

END OF gt_vbak.

START-OF-SELECTION.

PERFORM f_read_data.

loop at gt_vbak.

gt_vbak-test = 1.

modify gt_vbak.

endloop.

PERFORM f_display_data.

FORM f_read_data.

SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_vbak

FROM vbak UP TO p_max ROWS WHERE

kunnr IN s_kunnr AND

vbeln IN s_vbeln AND

vkorg IN s_vkorg.

ENDFORM.

FORM f_display_data.

DEFINE m_fieldcat.

add 1 to ls_fieldcat-col_pos.

ls_fieldcat-fieldname = &1.

ls_fieldcat-ref_tabname = 'VBAK'.

ls_fieldcat-ref_FIELDname = &2.

ls_fieldcat-do_sum = &3.

ls_fieldcat-cfieldname = &4.

append ls_fieldcat to lt_fieldcat.

END-OF-DEFINITION.

DEFINE m_sort.

add 1 to ls_sort-spos.

ls_sort-fieldname = &1.

ls_sort-up = 'X'.

ls_sort-subtot = &2.

ls_sort-group = '*'.

append ls_sort to lt_sort.

END-OF-DEFINITION.

DATA: ls_fieldcat TYPE slis_fieldcat_alv,

lt_fieldcat TYPE slis_t_fieldcat_alv,

lt_sort TYPE slis_t_sortinfo_alv,

ls_sort TYPE slis_sortinfo_alv,

ls_layout TYPE slis_layout_alv.

m_fieldcat 'VKORG' 'VKORG' '' ''.

m_fieldcat 'KUNNR' '' '' ''.

m_fieldcat 'VBELN' '' '' ''.

m_fieldcat 'TEST' '' 'X' ''.

  • m_sort 'VKORG' ''.

  • m_sort 'KUNNR' ''.

<b> m_sort 'VBELN' 'X'.</b>

*m_sort 'TEST' 'X'.

ls_layout-cell_merge = 'X'.

LS_LAYOUT-WINDOW_TITLEBAR = 'test window'.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

IS_LAYOUT = ls_layout

IT_FIELDCAT = lt_fieldcat

IT_SORT = lt_sort

  • IMPORTING

  • E_EXIT_CAUSED_BY_CALLER =

  • ES_EXIT_CAUSED_BY_USER =

TABLES

T_OUTTAB = gt_vbak

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.

..

Message was edited by: Ashok Parupalli

Former Member
0 Kudos

Hello Usha,

Using the sort Table u can do it.

Use this example

REFRESH gt_sort. CLEAR gs_sort.

  • 1

gs_sort-spos = 1. gs_sort-fieldname = 'VBELN'.

gs_sort-up = 'X'. gs_sort-down = ' '.

gs_sort-subtot = 'X'. gs_sort-expa = ' '.

APPEND gs_sort TO gt_sort.

If u want give the Grand total

While builting the fieldcat for that field

use L_FIELDCAT-DO_SUM = 'X'.

If useful reward points.

Regards,

Vasanth

Former Member
0 Kudos

Hi Usha,

In order to do the sub-total the main total has to be calculated and then the sub-total process can be performed.

In the main fieldcat mark this field

wa_line_fieldcat-fieldname = 'VALUE'.

wa_line_fieldcat-tabname = 'I_OUTPUT'.

wa_line_fieldcat-reptext = text-021.

<b>wa_line_fieldcat-do_sum = 'X'.</b>

APPEND wa_line_fieldcat TO i_fieldcat.

this will do the main total part.

Sub-total:

you will have to create an internal table sort

CLEAR struct_sort.

struct_sort-spos = 1.

struct_sort-fieldname = 'VALUE'.

struct_sort-up = 'X'.

<b>struct_sort-subtot = 'X'.</b>

APPEND struct_sort TO i_sort.

Now pass these two internal tables to the display

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

........

IT_SORT = i_sort

TABLES

t_outtab = I_OUTPUT

Hope this solves ur issue.

Reward if helpful.

Regards,

Tushar