ALV Grid
https://www.saptechpro.com/post/SALV_DEMO_METADATA
1. Build field catalog and set the field DO_SUM of field catalog to ‘X’ for the field for
which you want calculate the totals.
2. Pass field catalog to function module ‘REUSE_ALV_GRID_DISPLAY’.
TYPE-POOLS: slis. " SLIS contains all the ALV data types
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.
wa_fieldcat-fieldname = 'CONNID'.
wa_fieldcat-fieldname = 'FLDATE'.
wa_fieldcat-seltext_m = 'Date'.
wa_fieldcat-fieldname = 'BOOKID'.
wa_fieldcat-fieldname = 'PASSNAME'.
wa_fieldcat-fieldname = 'LOCCURAM'.
wa_fieldcat-seltext_m = 'Price'.
wa_fieldcat-do_sum = 'X'.
wa_fieldcat-fieldname = 'LOCCURKEY'.
wa_fieldcat-seltext_m = 'Currency'.
APPEND wa_fieldcat TO it_fieldcat.
*Pass data and field catalog to ALV function module to display ALV list
EXPORTING
it_fieldcat = it_fieldcat
TABLES
t_outtab = it_sbook
EXCEPTIONS
program_error = 1
OTHERS = 2.
Output
Subtotal in ABAP ALV
To calculate subtotal in ALV, build sort catalog and set the field SUBTOT of sort catalog to
‘X’.
TYPE-POOLS: slis. " SLIS contains all the ALV data types
*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
DATA: it_sbook TYPE TABLE OF sbook.
DATA: it_fieldcat TYPE slis_t_fieldcat_alv,
wa_fieldcat TYPE slis_fieldcat_alv,
it_sort TYPE slis_t_sortinfo_alv,
wa_sort TYPE slis_sortinfo_alv..
*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.
wa_fieldcat-fieldname = 'CONNID'.
wa_fieldcat-seltext_m = 'Con. No.'.
APPEND wa_fieldcat TO it_fieldcat.
wa_fieldcat-fieldname = 'FLDATE'.
wa_fieldcat-seltext_m = 'Date'.
APPEND wa_fieldcat TO it_fieldcat.
wa_fieldcat-fieldname = 'BOOKID'.
wa_fieldcat-seltext_m = 'Book. ID'.
APPEND wa_fieldcat TO it_fieldcat.
wa_fieldcat-fieldname = 'PASSNAME'.
wa_fieldcat-seltext_m = 'Passenger Name'.
APPEND wa_fieldcat TO it_fieldcat.
wa_fieldcat-fieldname = 'LOCCURAM'.
wa_fieldcat-seltext_m = 'Price'.
wa_fieldcat-do_sum = 'X'.
APPEND wa_fieldcat TO it_fieldcat.
wa_fieldcat-fieldname = 'LOCCURKEY'.
wa_fieldcat-seltext_m = 'Currency'.
APPEND wa_fieldcat TO it_fieldcat.
*Build sort catalog
wa_sort-spos = 1.
wa_sort-fieldname = 'CARRID'.
wa_sort-up = 'X'.
wa_sort-subtot = 'X'.
APPEND wa_sort TO it_sort.
*Pass data and field catalog to ALV function module to display ALV list
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
it_fieldcat = it_fieldcat
it_sort = it_sort
TABLES
t_outtab = it_sbook
EXCEPTIONS
program_error = 1
OTHERS = 2.
Output
If you want to calculate subtotals for more fields, just add a record in sort field catalog for
each field.
wa_sort-spos = 2.
wa_sort-fieldname = 'CONNID'.
wa_sort-up = 'X'.
wa_sort-subtot = 'X'.
APPEND wa_sort TO it_sort.
Output
Sub Total Text in ABAP ALV Grid
By
Prakash
07/24/2015
6572
[adsenseyu2]
Hello everyone, In the ALV ABAP Tutorials, we will learn how to add sub total
text in ABAP ALV Grid.
Step-by-Step Procedure
REPORT Z_ALV_SUBTOTAL_TEXT.
*-----------------------------------------------------
--------*
* www.saplearners.com
*
*-----------------------------------------------------
--------*
*-----------------------------------------------------
--------*
TABLES: ekko.
*-- Type pool declaration
TYPE-POOLS: slis.
END OF lty_ekpo.
START-OF-SELECTION.
FROM ekpo
ENDIF.
PERFORM f_field_catalog.
*--- Layout
PERFORM f_build_layout.
PERFORM f_populate_sort.
PERFORM f_get_event.
END-OF-SELECTION.
*&----------------------------------------------------
-----------------*
*&----------------------------------------------------
-----------------*
*-----------------------------------------------------
-----------------*
* No Parameter
*-----------------------------------------------------
-----------------*
FORM f_field_catalog .
lwa_fcat-col_pos = 1. "Column
lwa_fcat-col_pos = 2. "Column
CLEAR:lwa_fcat.
lwa_fcat-col_pos = 3. "Column
CLEAR:lwa_fcat.
lwa_fcat-col_pos = 3. "Column
lwa_fcat-no_out = 'X'.
lwa_fcat-tech = 'X'.
CLEAR:lwa_fcat.
lwa_fcat-col_pos = 4. "Column
CLEAR:lwa_fcat.
lwa_fcat-col_pos = 4. "Column
lwa_fcat-fieldname = 'MATNR'. "Field Name
lwa_fcat-no_out = 'X'.
lwa_fcat-tech = 'X'.
CLEAR:lwa_fcat.
lwa_fcat-col_pos = 5. "Column
CLEAR:lwa_fcat.
*&----------------------------------------------------
-----------------*
*-----------------------------------------------------
-----------------*
* No Parameter
*-----------------------------------------------------
-----------------*
FORM f_build_layout.
CLEAR wa_layout.
*&----------------------------------------------------
-----------------*
FORM f_populate_sort .
* Sort on plant
wa_sort-spos = 1.
wa_sort-fieldname = 'WERKS'.
wa_sort-tabname = 'I_EKPO'.
wa_sort-up = 'X'.
wa_sort-subtot = 'X'.
CLEAR wa_sort.
* Sort on material
wa_sort-spos = 2.
wa_sort-fieldname = 'MATNR'.
wa_sort-tabname = 'I_EKPO'.
wa_sort-up = 'X'.
wa_sort-subtot = 'X'.
APPEND wa_sort TO lt_sort .
CLEAR wa_sort.
*&----------------------------------------------------
-----------------*
*&----------------------------------------------------
-----------------*
FORM f_get_event.
IMPORTING
et_events = lt_events
EXCEPTIONS
list_type_wrong = 0
OTHERS = 0.
* Subtotal
IF sy-subrc = 0.
lwa_event-form = 'SUBTOTAL_TEXT'.
ENDIF.
*&----------------------------------------------------
-----------------*
*&----------------------------------------------------
-----------------*
FORM f_alv_report_display .
* ALV report
EXPORTING
i_callback_program = sy-repid
is_layout = wa_layout
it_fieldcat = lt_fieldcat
it_sort = lt_sort
it_events = lt_events
TABLES
t_outtab = lt_ekpo
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
ENDIF.
*&----------------------------------------------------
-----------------*
*& Form subtotal_text
*&----------------------------------------------------
-----------------*
*-----------------------------------------------------
-----------------*
IF p_subtot_text-criteria = 'MATNR'.
p_subtot_text-display_text_for_subtotal =
'Material Level Sub-Total'.
ENDIF.
IF p_subtot_text-criteria = 'WERKS'.
p_subtot_text-display_text_for_subtotal = 'Plant
Level Sub-Total'.
ENDIF.
ENDFORM. "subtotal_text
Output :
[adsenseyu1]
Selection Texts:
P_AGGR Aggregation
P_AMOUNT Number of Data Records
P_DISP Global Display Settings
P_FCAT Column Information
P_FILT Filter
P_FULL Output as Fullscreen Grid
P_GRID Output as Grid
P_LIST Output as List
P_SORT Sort
*&---------------------------------------------------------------------*
*& Report SALV_DEMO_METADATA
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
*-----------------------------------------------------------------------
*... This report demonstrates the setting of metadata for ALV OM
* - cl_salv_table (Fullscreen Grid, Fullscreen List, Grid)
* the following steps have to be performed. Please go to the corresponding
* markers in the coding.
* If the table is empty, please create data for the demo by running report
* BCALV_GENERATE_ALV_T_T2
* �1 For a simple call, all you need is a table and a model class object
* �2 Just select your data in some table
* �3 Provide the data table to the model class' factory method,
* get the model class object and display the table
* �3a if you want to display the data in your own Dynpro, you have to give a
container to ALV
* �4 Change the default settings of the metadata
* �5 Offer the default set of ALV generic functions
*-----------------------------------------------------------------------
type-pools: col,
icon,
sym.
begin of gc_s_display,
list type i value 1,
fullscreen type i value 2,
grid type i value 3,
end of gc_s_display.
*----------------------------------------------------------------------*
* SELECTION-SCREEN - for demonstration purposes only *
*----------------------------------------------------------------------*
selection-screen begin of block gen with frame.
parameters:
p_amount type i default 30.
selection-screen end of block gen.
*----------------------------------------------------------------------*
* START-OF-SELECTION *
*----------------------------------------------------------------------*
start-of-selection.
gs_test-amount = p_amount.
gs_test-repid = sy-repid.
case gc_true.
when p_list.
gs_test-display = gc_s_display-list.
when p_full.
gs_test-display = gc_s_display-fullscreen.
when p_grid.
gs_test-display = gc_s_display-grid.
endcase.
perform select_data.
*----------------------------------------------------------------------*
* END-OF-SELECTION *
*----------------------------------------------------------------------*
end-of-selection.
case gs_test-display.
when gc_s_display-fullscreen.
perform display_fullscreen.
when gc_s_display-grid.
perform display_grid.
when gc_s_display-list.
perform display_list.
endcase.
*&---------------------------------------------------------------------*
*& Form select_data
*&---------------------------------------------------------------------*
* �2 to display the data, you first have to select it in some table
*----------------------------------------------------------------------*
form select_data.
if gs_test-amount > 0.
select * from alv_t_t2 into corresponding fields of table gt_outtab
up to gs_test-amount rows. "#EC *
endif.
*&---------------------------------------------------------------------*
*& Form display_fullscreen
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form display_fullscreen .
perform display.
*&---------------------------------------------------------------------*
*& Form display_list
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form display_list .
perform display.
endform. "display_grid
*&---------------------------------------------------------------------*
*& Module d0100_pbo OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module d0100_pbo output.
perform d0100_pbo.
endmodule. " d0100_pbo OUTPUT
*&---------------------------------------------------------------------*
*& Module d0100_pai INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module d0100_pai input.
perform d0100_pai.
endmodule. " d0100_pai INPUT
*&---------------------------------------------------------------------*
*& Form d0100_pbo
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form d0100_pbo .
perform display.
endif.
*&---------------------------------------------------------------------*
*& Form d0100_pai
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form d0100_pai .
case g_okcode.
when 'BACK' or 'EXIT' or 'CANC'.
set screen 0.
leave screen.
endcase.
*&--------------------------------------------------------------------*
*& Form set_display_settings
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
form set_display_settings.
data:
ls_display type ref to cl_salv_display_settings.
ls_display = gr_table->get_display_settings( ).
try.
ls_display->set_list_header( text-002 ).
ls_display->set_vertical_lines( abap_false ).
ls_display->set_horizontal_lines( abap_false ).
ls_display->set_striped_pattern( abap_true ).
ls_display->set_list_header_size(
cl_salv_display_settings=>c_header_size_small ).
ls_display->set_suppress_empty_data( abap_true ).
catch cx_no_check.
endtry.
endform. "set_display_settings
*&--------------------------------------------------------------------*
*& Form set_columns
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
form set_columns.
data:
lr_columns type ref to cl_salv_columns, "global columns settings
lr_column type ref to cl_salv_column_table. "individual column setting
lr_columns = gr_table->get_columns( ).
lr_columns->set_optimize( abap_true ).
try.
lr_column ?= lr_columns->get_column( 'MANDT' ). "get a
single column
lr_column->set_technical( abap_true ). "Client column is a
technical column
catch cx_salv_not_found. "#EC NO_HANDLER
endtry.
try.
lr_column ?= lr_columns->get_column( 'SEATSOCC' ).
lr_column->set_visible( abap_false ).
catch cx_salv_not_found. "#EC NO_HANDLER
endtry.
try.
lr_column ?= lr_columns->get_column( 'PRICE' ).
ls_color-col = col_group.
ls_color-int = 1.
lr_column->set_color( ls_color ).
catch cx_salv_not_found. "#EC NO_HANDLER
endtry.
endform. "set_columns
*&--------------------------------------------------------------------*
*& Form set_sort
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
form set_sort.
data:
lr_sorts type ref to cl_salv_sorts. "sort information
lr_sorts = gr_table->get_sorts( ).
try.
lr_sorts->add_sort(
columnname = 'CARRID'
position = 1
sequence = if_salv_c_sort=>sort_up ).
catch cx_salv_not_found cx_salv_existing cx_salv_data_error."#EC
NO_HANDLER
endtry.
try.
lr_sorts->add_sort(
columnname = 'CONNID'
position = 1
subtotal = abap_true "applies only if some aggregations are
turned on
sequence = if_salv_c_sort=>sort_up ).
catch cx_salv_not_found cx_salv_existing cx_salv_data_error."#EC
NO_HANDLER
endtry.
endform. "set_sort
*&--------------------------------------------------------------------*
*& Form set_aggregations
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
form set_aggregations.
data:
lr_aggregations type ref to cl_salv_aggregations.
lr_aggregations = gr_table->get_aggregations( ).
lr_aggregations->clear( ).
try.
lr_aggregations->add_aggregation( columnname = 'PAYMENTSUM' ).
catch cx_salv_not_found cx_salv_data_error cx_salv_existing."#EC
NO_HANDLER
endtry.
endform. "set_aggregations
*&--------------------------------------------------------------------*
*& Form set_filter
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
form set_filter.
data:
lr_filters type ref to cl_salv_filters.
lr_filters = gr_table->get_filters( ).
lr_filters->clear( ).
try.
lr_filters->add_filter(
columnname = 'CURRENCY'
low = 'USD' ).
catch cx_salv_not_found cx_salv_data_error cx_salv_existing."#EC
NO_HANDLER
endtry.
endform. "set_filter
*&--------------------------------------------------------------------*
*& Form set_metadata
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
form set_metadata .
*�4 depending on the choices made in the lower frame of the selection
screen,
* metadata default settings are changed
if p_disp eq abap_true.
* metadate for global display settings
perform set_display_settings.
endif.
if p_fcat eq abap_true.
* metadata for columns
perform set_columns.
endif.
if p_sort eq abap_true.
* sort by company and flight number
perform set_sort.
endif.
if p_aggr eq abap_true.
* aggregate the paymentsum
perform set_aggregations.
endif.
if p_filt eq abap_true.
* filter by currency
perform set_filter.
endif.
*&---------------------------------------------------------------------*
*& Form display
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form display .
lr_functions = gr_table->get_functions( ).
lr_functions->set_default( ).
To add a total or sub-total to an ALV report, we create a field catalog for the report (see Field
Catalog in ALV), then we set the field DO_SUM of field catalog for the field that we want to
calculate.
Example Program – Adding Total in ALV
*&———————————————————————*
*& Report Z01_DEMO_ALV_FIELDCATALOG
*&
*&———————————————————————*
*& Programmer: Vy Khang Ly
*& SAP FREE ABAP TUTORIALS
*& http://sapforbeginner.com
*&———————————————————————*
REPORT Z01_DEMO_ALV_FIELDCATALOG.
TYPE-POOLS: slis. ” SLIS contains all the ALV data types
*&———————————————————————*
*& Data Declaration
*&———————————————————————*
DATA: lt_sflight TYPE TABLE OF sflight.
DATA: lt_fieldcat TYPE slis_t_fieldcat_alv,
ls_fieldcat TYPE slis_fieldcat_alv.
DATA: g_repid TYPE sy-repid.
*&———————————————————————*
*& START-OF-SELECTION
*&———————————————————————*
START-OF-SELECTION.
g_repid = sy-repid.
*&—– Fetch data from the database —–*
SELECT *
FROM sflight
INTO TABLE lt_sflight.
*&—– Field Catalog —–*
ls_fieldcat-fieldname = ‘CARRID’.
ls_fieldcat-seltext_m = ‘Airline’.
APPEND ls_fieldcat TO lt_fieldcat.
ls_fieldcat-fieldname = ‘CONNID’.
ls_fieldcat-seltext_m = ‘FLight Number’.
APPEND ls_fieldcat TO lt_fieldcat.
ls_fieldcat-fieldname = ‘FLDATE’.
ls_fieldcat-seltext_m = ‘FLight Date’.
APPEND ls_fieldcat TO lt_fieldcat.
*&———————————————————————*
*& Report Z01_DEMO_ALV_FIELDCATALOG
*&
*&———————————————————————*
*& Programmer: Vy Khang Ly
*& SAP FREE ABAP TUTORIALS
*& http://sapforbeginner.com
*&———————————————————————*
REPORT Z01_DEMO_ALV_FIELDCATALOG.
*&———————————————————————*
*& Data Declaration
*&———————————————————————*
DATA: lt_sflight TYPE TABLE OF sflight.
DATA: lt_fieldcat TYPE slis_t_fieldcat_alv,
ls_fieldcat TYPE slis_fieldcat_alv,
lt_sort TYPE slis_t_sortinfo_alv,
ls_sort TYPE slis_sortinfo_alv.
DATA: g_repid TYPE sy-repid.
*&———————————————————————*
*& START-OF-SELECTION
*&———————————————————————*
START-OF-SELECTION.
g_repid = sy-repid.
*&—– Fetch data from the database —–*
SELECT *
FROM sflight
INTO TABLE lt_sflight.
*&—– Field Catalog —–*
ls_fieldcat-fieldname = ‘CARRID’. “Fieldname in the data table
ls_fieldcat-seltext_m = ‘Airline’. “Column description in the output
APPEND ls_fieldcat TO lt_fieldcat.
ls_fieldcat-fieldname = ‘CONNID’.
ls_fieldcat-seltext_m = ‘FLight Number’.
APPEND ls_fieldcat TO lt_fieldcat.
ls_fieldcat-fieldname = ‘FLDATE’.
ls_fieldcat-seltext_m = ‘FLight Date’.
APPEND ls_fieldcat TO lt_fieldcat.