REUSE_ALV_LIST_DISPLAY- 18

light11Use of ALV Field Catalog to calculate TOTAL and SORT table for sub total calculation

 

 

 

The previous post REUSE_ALV_LIST_DISPLAY- 17 shows the calculation of total(SUM) of a field by using the field catalog property and also setting up the total text by the layout property. This post shows to calculate the subtotal and total. 


Code Snippet:


———-data declarations——–
TYPE-POOLS: slis.
TYPES: BEGIN OF ty_flight,
box.
INCLUDE STRUCTURE spfli.
TYPES: END OF ty_flight.
DATA: lt_spfli TYPE TABLE OF ty_flight.
DATA: ls_spfli TYPE ty_flight.
DATA: ls_layout TYPE slis_layout_alv.
DATA: lt_fieldcat TYPE slis_t_fieldcat_alv.
DATA: lt_sort TYPE slis_t_sortinfo_alv.    ” declare to calculate the subtotal 
———————————–

START-OF-SELECTION.
PERFORM build_data.
PERFORM build_fieldcatalog.
PERFORM build_layout.
PERFORM build_sort.
PERFORM display_list_alv.

&————————————————-
& Form BUILD_DATA
&————————————————-*
FORM build_data.
SELECT * FROM spfli INTO CORRESPONDING FIELDS OF TABLE lt_spfli UP TO 10 ROWS.
ENDFORM. ” BUILD_DATA

&———————————————————————
& Form BUILD_FIELDCATALOG
&———————————————————————
FORM build_fieldcatalog.
DATA: ls_fieldcat TYPE LINE OF slis_t_fieldcat_alv.
CALL FUNCTION ‘REUSE_ALV_FIELDCATALOG_MERGE’
EXPORTING
i_program_name = sy-cprog
I_INTERNAL_TABNAME =
i_structure_name = ‘SPFLI’
* I_CLIENT_NEVER_DISPLAY = ‘X’
* I_INCLNAME =
* I_BYPASSING_BUFFER =
* I_BUFFER_ACTIVE =
CHANGING
ct_fieldcat = lt_fieldcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.

LOOP AT lt_fieldcat INTO ls_fieldcat.
IF ls_fieldcat-fieldname = ‘FLTYPE’ OR ls_fieldcat-fieldname = ‘PERIOD’ .
ls_fieldcat-no_out = ‘X’.
MODIFY lt_fieldcat FROM ls_fieldcat TRANSPORTING no_out.
ENDIF.

” set the field catalog property for the total/SUM
IF ls_fieldcat-fieldname = ‘DISTANCE’.
ls_fieldcat-do_sum = ‘X’.
MODIFY lt_fieldcat FROM ls_fieldcat TRANSPORTING do_sum.
ENDIF.
IF ls_fieldcat-fieldname = ‘CARRID’.
ls_fieldcat-outputlen = ‘7’.
MODIFY lt_fieldcat FROM ls_fieldcat TRANSPORTING outputlen.
ENDIF.
ENDLOOP.

ENDFORM. ” BUILD_FIELDCATALOG

&————————————————–
& Form BUILD_LAYOUT
&————————————————–
FORM build_layout.
ls_layout-zebra = ‘X’.
ls_layout-box_fieldname = ‘BOX’.
ls_layout-detail_popup = ‘X’.
ls_layout-detail_titlebar = ‘Detailed Flight Info’.
ls_layout-f2code = ‘&ETA’.
ls_layout-window_titlebar = ‘Flight Info’.
ls_layout-totals_text = ‘Total’.
ENDFORM. ” BUILD_LAYOUT
&————————————————–
& Form BUILD_SORT
&————————————————–
FORM build_sort.
DATA: ls_sort TYPE slis_sortinfo_alv.
ls_sort-fieldname = ‘CARRID’.     ” subtotal to be performed on CARRID field
ls_sort-spos = 1.
ls_sort-up = ‘X’.                                 ” Ascending sort for the field CARRID
” ls_sort-down = ‘X’.
ls_sort-subtot = ‘X’.                         ” set to calculate sub total
APPEND ls_sort TO lt_sort.
ENDFORM. ” BUILD_SORT
&—————————————————
& Form DISPLAY_LIST_ALV
&—————————————————-*
FORM display_list_alv.
CALL FUNCTION ‘REUSE_ALV_LIST_DISPLAY’
EXPORTING
i_callback_program = sy-cprog
” i_structure_name = ‘SPFLI’
is_layout = ls_layout
it_fieldcat = lt_fieldcat
it_sort = lt_sort     ” pass the sort field to the ALV 
TABLES
t_outtab = lt_spfli
EXCEPTIONS
program_error = 1
OTHERS = 2.
CASE sy-subrc.
WHEN 1.
MESSAGE ‘Program Error’ TYPE ‘I’.
WHEN OTHERS.
ENDCASE.
ENDFORM. ” DISPLAY_LIST_ALV


Program Output:

6.jpg


Code Snippet: In case we need descending sub total on the field CARRID


———-data declarations——–
TYPE-POOLS: slis.
TYPES: BEGIN OF ty_flight,
box.
INCLUDE STRUCTURE spfli.
TYPES: END OF ty_flight.
DATA: lt_spfli TYPE TABLE OF ty_flight.
DATA: ls_spfli TYPE ty_flight.
DATA: ls_layout TYPE slis_layout_alv.
DATA: lt_fieldcat TYPE slis_t_fieldcat_alv.
DATA: lt_sort TYPE slis_t_sortinfo_alv.
———————————–

START-OF-SELECTION.
PERFORM build_data.
PERFORM build_fieldcatalog.
PERFORM build_layout.
PERFORM build_sort.
PERFORM display_list_alv.

&————————————————-
& Form BUILD_DATA
&————————————————-*
FORM build_data.
SELECT * FROM spfli INTO CORRESPONDING FIELDS OF TABLE lt_spfli UP TO 10 ROWS.
ENDFORM. ” BUILD_DATA

&———————————————————————
& Form BUILD_FIELDCATALOG
&———————————————————————
FORM build_fieldcatalog.
DATA: ls_fieldcat TYPE LINE OF slis_t_fieldcat_alv.
CALL FUNCTION ‘REUSE_ALV_FIELDCATALOG_MERGE’
EXPORTING
i_program_name = sy-cprog
I_INTERNAL_TABNAME =
i_structure_name = ‘SPFLI’
* I_CLIENT_NEVER_DISPLAY = ‘X’
* I_INCLNAME =
* I_BYPASSING_BUFFER =
* I_BUFFER_ACTIVE =
CHANGING
ct_fieldcat = lt_fieldcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.

LOOP AT lt_fieldcat INTO ls_fieldcat.
IF ls_fieldcat-fieldname = ‘FLTYPE’ OR ls_fieldcat-fieldname = ‘PERIOD’ .
ls_fieldcat-no_out = ‘X’.
MODIFY lt_fieldcat FROM ls_fieldcat TRANSPORTING no_out.
ENDIF.
IF ls_fieldcat-fieldname = ‘DISTANCE’.
ls_fieldcat-do_sum = ‘X’.
MODIFY lt_fieldcat FROM ls_fieldcat TRANSPORTING do_sum.
ENDIF.
IF ls_fieldcat-fieldname = ‘CARRID’.
ls_fieldcat-outputlen = ‘7’.
MODIFY lt_fieldcat FROM ls_fieldcat TRANSPORTING outputlen.
ENDIF.
ENDLOOP.

ENDFORM. ” BUILD_FIELDCATALOG

&————————————————–
& Form BUILD_LAYOUT
&————————————————–
FORM build_layout.
ls_layout-zebra = ‘X’.
ls_layout-box_fieldname = ‘BOX’.
ls_layout-detail_popup = ‘X’.
ls_layout-detail_titlebar = ‘Detailed Flight Info’.
ls_layout-f2code = ‘&ETA’.
ls_layout-window_titlebar = ‘Flight Info’.
ls_layout-totals_text = ‘Total’.
ENDFORM. ” BUILD_LAYOUT
&————————————————–
& Form BUILD_SORT
&————————————————–
FORM build_sort.
DATA: ls_sort TYPE slis_sortinfo_alv.
ls_sort-fieldname = ‘CARRID’.
ls_sort-spos = 1.
“ls_sort-up = ‘X’.
ls_sort-down = ‘X’.     ” Set down sort for descending sort
ls_sort-subtot = ‘X’.
APPEND ls_sort TO lt_sort.
ENDFORM. ” BUILD_SORT
&—————————————————
& Form DISPLAY_LIST_ALV
&—————————————————-*
FORM display_list_alv.
CALL FUNCTION ‘REUSE_ALV_LIST_DISPLAY’
EXPORTING
i_callback_program = sy-cprog
” i_structure_name = ‘SPFLI’
is_layout = ls_layout
it_fieldcat = lt_fieldcat
it_sort = lt_sort
TABLES
t_outtab = lt_spfli
EXCEPTIONS
program_error = 1
OTHERS = 2.
CASE sy-subrc.
WHEN 1.
MESSAGE ‘Program Error’ TYPE ‘I’.
WHEN OTHERS.
ENDCASE.
ENDFORM. ” DISPLAY_LIST_ALV


Program Output:

7.jpg


 

Advertisements

One thought on “REUSE_ALV_LIST_DISPLAY- 18

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s