Sie sind auf Seite 1von 39

report zbnstest.

************************************************************************
* TABLES AND DATA DECLARATION.
************************************************************************
*TABLES: mara,makt.”,marc.
data syrepid like sy-repid.
data sydatum(10). ” LIKE sy-datum.
data sypagno(3) type n.
* WHEN USING MORE THAN ONE TABLE IN alv WE NEEED TO DECLARE THE TYPE
* GROUP (TYPE-POOLS———>SLIS)
type-pools : slis.
************************************************************************
* INTERNAL TABLE DECLARATION.
************************************************************************
* INTERNAL TABLE TO HOLD THE VALUES FROM THE MARA TABLE
data: begin of t_mara occurs 0,
matnr like mara-matnr,
meins like mara-meins,
mtart like mara-mtart,
matkl like mara-matkl,

end of t_mara.
* INTERNAL TABLE TO HOLD THE CONTENTS FROM THE EKKO TABLE
data : begin of t_marc occurs 0,
matnr like mara-matnr,
werks like marc-werks,
minbe like marc-minbe.
data: end of t_marc.
* INTERNAL TABLE TO HOLD THE VALUES FROM MAKT TABLE.
data : begin of t_makt occurs 0,
matnr like mara-matnr,
maktx like makt-maktx,
spras like makt-spras,
end of t_makt.
* INTERNAL TABLE WHICH ACTUALLY MERGES ALL THE OTHER INTERNAL TABLES.
data: begin of itab1 occurs 0,
matnr like mara-matnr,
meins like mara-meins,
maktx like makt-maktx,
spras like makt-spras,
werks like marc-werks,
minbe like marc-minbe,
end of itab1.
* THE FOLLOWING DECLARATION IS USED FOR DEFINING THE FIELDCAT
* AND THE LAYOUT FOR THE alv.
* HERE AS slis_t_fieldcat_alv IS A INTERNAL TABLE WITHOUT A HEADER LINE
* WE EXPLICITELY DEFINE AN INTERNAL TABLE OF THE SAME STRUCTURE AS THAT
* OF slis_t_fieldcat_alv BUT WITH A HEADER LINE IN THE DEFINITION.
* THIS IS DONE TO MAKE THE CODE SIMPLER.
* OTHERWISE WE MAY HAVE TO DEFINE THE STRUCTURE AS IN THE NORMAL SAP
* PROGRAMS.
* IN THE FIELDCATALOG TABLE WE ACTUALLY PASS THE FIELDS FROM ONE OR
* MORE TABLES AND CREATE A STRUCTURE
* IN THE LAYOUT STRUCTURE WE BASICALLY DEFINE THE FORMATTING OPTIONS
* LIKE DISPLAY IN THE ZEBRA PATTERN ,THE HOTSPOT OPTIONS ETC.
data: fieldcatalog type slis_t_fieldcat_alv with header line,
fieldlayout type slis_layout_alv.
* DECLARING THE EVENTTABLE INTERNL TABLE FOR USING EVENTS LIKE
* TOP-OF-PAGE ETC.
data : eventstab type slis_t_event with header line.
* DECLARING AN INTERNAL TABLE TO HOLD THE DATA FOR THE TOP-OF-PAGE
data : heading type slis_t_listheader with header line.
data : heading1 type slis_t_listheader with header line.
data : heading2 type slis_t_listheader with header line.
data : heading3 type slis_t_listheader with header line.
data : heading4 type slis_t_listheader with header line.
data : heading5 type slis_t_listheader with header line.
data : heading6 type slis_t_listheader with header line.
data : heading7 type slis_t_listheader with header line.
data : heading8 type slis_t_listheader with header line.
* STRUCTURE TO PASS THE COLOR ATTRIBUTES FOR DISPLAY.
data : colorstruct type slis_coltypes.
************************************************************************
* INITIALIZATION. *
************************************************************************
initialization.
syrepid = sy-repid.
sypagno = sy-pagno.
clear fieldcatalog.
************************************************************************
* START-OF-SELECTION. *
************************************************************************
start-of-selection.
* SUBROUTINE TO POPULATE THE COLORSTRUCT
perform fill_colorstruct using colorstruct.
* SUBROUTINE TO POPULATE THE FIELDS OF THE FIELD CATALOGUE
perform populate_fieldcatalog.
* SUBROUTINE TO SELECT DATA FROM VARIOUS TABLES AND POPULATE IT IN THE
* INTERNAL TABLE.
perform selectdata_and_sort.
* SUBROUTINE TO POPULATE THE LAYOUT STRUCTURE.
perform populate_layout using fieldlayout.
* SUBROUTINE TO CALL THE FUNCTION MERGE TO ENSURE PROPER DISPLAY.
perform merge_fieldcatalog.
* SUBROUTINE TO POPULATE THE EVENTSTAB.
perform fill_eventstab tables eventstab.
* SUBROUTINE TO POPULATE THE HEADING TABLES.
perform fill_headingtable tables heading using ‘HEADING’.
perform fill_headingtable tables heading1 using ‘HEADING1′.
perform fill_headingtable tables heading2 using ‘HEADING2′.
perform fill_headingtable tables heading3 using ‘HEADING3′.
perform fill_headingtable tables heading4 using ‘HEADING4′.
perform fill_headingtable tables heading5 using ‘HEADING5′.
perform fill_headingtable tables heading6 using ‘HEADING6′.
perform fill_headingtable tables heading7 using ‘HEADING7′.
perform fill_headingtable tables heading8 using ‘HEADING8′.
* SUBROUTINE TO DISPLAY THE LIST.
perform display_alv_list.
************************************************************************
* FORMS
************************************************************************
* IN THIS SUBROUTINE WE POPULATE THE FIELDCATALOG TABLE WITH THE NAMES
* OF THE TABLE,FIELDNAME,WHETHER IT IS KEY FIELD OR NOT,HEADING AND
* COLUMN JUSTIFICATION.
form populate_fieldcatalog.
perform fill_fields_of_fieldcatalog tables fieldcatalog
using ‘ITAB1′ ‘MATNR’ ‘X’ .
perform fill_fields_of_fieldcatalog tables fieldcatalog
using ‘ITAB1′ ‘MEINS’ ‘ ‘.
perform fill_fields_of_fieldcatalog tables fieldcatalog
using ‘ITAB1′ ‘MAKTX’ ‘ ‘ .
perform fill_fields_of_fieldcatalog tables fieldcatalog
using ‘ITAB1′ ‘MTART’ ‘ ‘ .
perform fill_fields_of_fieldcatalog tables fieldcatalog
using ‘ITAB1′ ‘MATKL’ ‘ ‘ .
perform fill_fields_of_fieldcatalog tables fieldcatalog
using ‘ITAB1′ ‘SPRAS’ ‘ ‘ .
perform fill_fields_of_fieldcatalog tables fieldcatalog
using ‘ITAB1′ ‘WERKS’ ‘ ‘ .
perform fill_fields_of_fieldcatalog tables fieldcatalog
using ‘ITAB1′ ‘MINBE’ ‘ ‘ .
endform. ” POPULATE_FIELDCATALOG
*———————————————————————*
* FORM FILL_FIELDS_OF_FIELDCATALOG *
*———————————————————————*
* …….. *
*———————————————————————*
* –> FIELDCATALOG *
* –> P_TABNAME *
* –> P_FIELDNAME *
* –> P_KEY *
* –> P_KEY *
*———————————————————————*
form fill_fields_of_fieldcatalog tables fieldcatalog
structure fieldcatalog
using p_tabname
p_fieldname
p_key.
* p_no_out.
fieldcatalog-tabname = p_tabname.
fieldcatalog-fieldname = p_fieldname.
fieldcatalog-key = p_key.
fieldcatalog-emphasize = ‘1234′.
*fieldcatalog-no_out = p_no_out.
append fieldcatalog.
endform. ” FILL_FIELDSOFFIELDCATALOG
*———————————————————————*
* FORM POPULATE_LAYOUT *
*———————————————————————*
* …….. *
*———————————————————————*
* –> FIELDLAYOUT *
*———————————————————————*
form populate_layout using fieldlayout type slis_layout_alv.
fieldlayout-f2code = ‘&ETA’ .
fieldlayout-zebra = ‘X’.
* FOR THE WINDOW TITLE.
fieldlayout-window_titlebar = ‘alv with Events’.
fieldlayout-colwidth_optimize = ‘X’.
fieldlayout-no_vline = ‘ ‘.
*fieldlayout-no_input = ‘X’.
fieldlayout-confirmation_prompt = ”.
fieldlayout-key_hotspot = ‘X’.
* This removes the column headings if the flag is set to ‘X’
fieldlayout-no_colhead = ‘ ‘.
*fieldlayout-hotspot_fieldname = ‘MAKTX’.
fieldlayout-detail_popup = ‘X’.
* fieldlayout-coltab_fieldname = ‘X’.
endform. ” POPULATE_LAYOUT
*———————————————————————*
* FORM SELECTDATA_AND_SORT *
*———————————————————————*
* …….. *
*———————————————————————*
form selectdata_and_sort.
select matnr meins mtart matkl from mara
into corresponding fields of t_mara
up to 500 rows .
select matnr maktx spras from makt
into corresponding fields of t_makt
where matnr = t_mara-matnr and
spras = sy-langu.
select matnr werks minbe from marc
into corresponding fields of t_marc
where matnr = t_mara-matnr.
append t_marc.
endselect.
append t_makt.
endselect.
append t_mara.
endselect.
perform populate_itab1.
sort itab1 by matnr.
endform. ” SELECTDATA_AND_SORT
*———————————————————————*
* FORM MERGE_FIELDCATALOG *
*———————————————————————*
* …….. *
*———————————————————————*
form merge_fieldcatalog.
call function ‘REUSE_ALV_FIELDCATALOG_MERGE’
exporting
i_program_name = syrepid
i_internal_tabname = ‘ITAB1′
* i_structure_name = ‘COLORSTRUCT’
* I_CLIENT_NEVER_DISPLAY = ‘X’
i_inclname = syrepid
changing
ct_fieldcat = fieldcatalog[]
exceptions
inconsistent_interface = 1
program_error = 2
others = 3.
endform. ” MERGE_FIELDCATALOG
* IN THIS FUNCTION THE MINIMUM PARAMETERS THAT WE NEED TO PASS IS AS
* FOLLOWS:-
* i_callback_program –> CALLING PROGRAM NAME
* i_structure_name –> STRUCTURE NAME.
* is_layout –> LAYOUT NAME.
* it_fieldcat —> BODY OF THE FIELD CATALOGUE INTERNAL TABLE
form display_alv_list.
call function ‘REUSE_ALV_LIST_DISPLAY’
exporting
* I_INTERFACE_CHECK = ‘ ‘
i_callback_program = syrepid
* I_CALLBACK_PF_STATUS_SET = ‘ ‘
* I_CALLBACK_USER_COMMAND = ‘ ‘
i_structure_name = ‘ITAB1′
is_layout = fieldlayout
it_fieldcat = fieldcatalog[]
* IT_EXCLUDING =
* IT_SPECIAL_GROUPS =
* IT_SORT =
* IT_FILTER =
* IS_SEL_HIDE =
* I_DEFAULT = ‘X’
* THE FOLLOWING PARAMETER IS SET AS ‘A’ INORDER TO DISPLAY THE STANDARD
* TOOL BAR
i_save = ‘A’
* IS_VARIANT = ‘ ‘
it_events = eventstab[]
* IT_EVENT_EXIT =
* IS_PRINT =
* I_SCREEN_START_COLUMN = 0
* I_SCREEN_START_LINE = 0
* I_SCREEN_END_COLUMN = 0
* I_SCREEN_END_LINE = 0
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER =
* ES_EXIT_CAUSED_BY_USER =
tables
t_outtab = itab1
exceptions
program_error = 1
others = 2.
endform. ” DISPLAY_ALV_LIST
*&———————————————————————*
*& Form POPULATE_ITAB1
*&———————————————————————*
* text
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
form populate_itab1.
loop at t_mara.
loop at t_makt where matnr = t_mara-matnr.
loop at t_marc where matnr = t_mara-matnr.
move-corresponding t_mara to itab1.
move-corresponding t_makt to itab1.
move-corresponding t_marc to itab1.
append itab1.
endloop.
endloop.
endloop.
endform. ” POPULATE_ITAB1
*&———————————————————————*
*& Form FILL_EVENTSTAB
*&———————————————————————*
* text
*———————————————————————-*
* –>P_EVENTSTAB text *
*———————————————————————-*
form fill_eventstab tables p_eventstab structure eventstab.
* WHEN THE FOLLOWING FUNCTION IS CALLED THE SYSTEM POPULATES THE
* INTERNAL TABLE EVENTSTAB WITH A LIST OF EVENTS NAME.
* AS SHOWN BELOW WHEN USING I_LIST_TYPE = 0 THE FUNCTION RETURNS 14
* EVENTS NAME.
call function ‘REUSE_ALV_EVENTS_GET’
exporting
i_list_type = 0
importing
et_events = p_eventstab[]
exceptions
list_type_wrong = 1
others = 2.
* BY CALLING THE ABOVE FUNCTION WE FIRST POPULATE THE EVENTSTAB WITH
* THE PREDEFINED EVENTS AND THEN WE MOVE THE FORM NAME AS SHOWN BELOW.
* WE ASSIGN A FORM NAME TO THE EVENT AS REQUIRED BY THE USER.
* FORM NAME CAN BE ANYTHING.THE PERFORM STATEMENT FOR THIS FORM
* IS DYNAMICALY CALLED.
read table p_eventstab with key name = slis_ev_top_of_page.
if sy-subrc = 0 .
move ‘TOP_OF_PAGE’ to p_eventstab-form.
append p_eventstab.
endif.
read table p_eventstab with key name = slis_ev_top_of_coverpage.
if sy-subrc = 0 .
move ‘TOP_OF_COVERPAGE’ to p_eventstab-form.
append p_eventstab.
endif.
read table p_eventstab with key name = slis_ev_end_of_coverpage .
if sy-subrc = 0 .
move ‘END_OF_COVERPAGE’ to p_eventstab-form.
append p_eventstab.
endif.
read table p_eventstab with key name = slis_ev_foreign_top_of_page.
if sy-subrc = 0 .
move ‘FOREIGN_TOP_OF_PAGE’ to p_eventstab-form.
append p_eventstab.
endif.
read table p_eventstab with key name = slis_ev_foreign_end_of_page.
if sy-subrc = 0 .
move ‘FOREIGN_END_OF_PAGE’ to p_eventstab-form.
append p_eventstab.
endif.
read table p_eventstab with key name = slis_ev_list_modify.
if sy-subrc = 0 .
move ‘LIST_MODIFY’ to p_eventstab-form.
append p_eventstab.
endif.
read table p_eventstab with key name = slis_ev_top_of_list.
if sy-subrc = 0 .
move ‘TOP_OF_LIST’ to p_eventstab-form.
append p_eventstab.
endif.
read table p_eventstab with key name = slis_ev_end_of_page.
if sy-subrc = 0 .
move ‘END_OF_PAGE’ to p_eventstab-form.
append p_eventstab.
endif.
read table p_eventstab with key name = slis_ev_end_of_list .
if sy-subrc = 0 .
move ‘END_OF_LIST’ to p_eventstab-form.
append p_eventstab.
endif.
endform. ” FILL_EVENTSTAB
*&———————————————————————*
*& Form FILL_HEADINGTABLE
*&———————————————————————*
* text
*———————————————————————-*
* –>P_HEADING text *
*———————————————————————-*
form fill_headingtable tables p_heading structure heading
using tablename.
case tablename.
when ‘HEADING’.
p_heading-typ = ‘H’.
concatenate
‘ REPORT NAME:-’ syrepid
‘ ABB Industry Pte Ltd’ into p_heading-info.
append p_heading.
write sy-datum using edit mask ‘__/__/____’ to sydatum.
concatenate
‘ DATE:-’ sydatum ‘ USER: ‘ sy-uname ‘PAGE NO:’ sypagno
into p_heading-info.
append p_heading.
when ‘HEADING1′.
p_heading-typ = ‘H’.
p_heading-info = ‘TOP-OF-COVER-PAGE’.
append p_heading.
when ‘HEADING2′.
p_heading-typ = ‘H’.
p_heading-info = ‘END-OF-COVER-PAGE’.
append p_heading.
when ‘HEADING3′.
p_heading-typ = ‘H’.
p_heading-info = ‘FOREIGN-TOP-OF-PAGE’.
append p_heading.
when ‘HEADING4′.
p_heading-typ = ‘H’.
p_heading-info = ‘FOREIGN-END-OF-PAGE’.
append p_heading.
* WHEN ‘HEADING5′.
* P_HEADING-TYP = ‘H’.
* P_HEADING-INFO = ‘LIST-MODIFY’.
* APPEND P_HEADING.
when ‘HEADING6′.
p_heading-typ = ‘H’.
p_heading-info = ‘END-OF-PAGE’.
append p_heading.
when ‘HEADING7′.
p_heading-typ = ‘H’.
p_heading-info = ‘END-OF-LIST’.
append p_heading.
when ‘HEADING8′.
p_heading-typ = ‘H’.
p_heading-info = ‘TOP-OF-LIST’.
append p_heading.
endcase.
endform. ” FILL_HEADINGTABLE
*———————————————————————*
* FORM TOP_OF_PAGE *
*———————————————————————*
* …….. *
*———————————————————————*
form top_of_page.
call function ‘REUSE_ALV_COMMENTARY_WRITE’
exporting
it_list_commentary = heading[]
exceptions
others = 1.
endform.
*&———————————————————————*
*& Form FILL_COLORSTRUCT
*&———————————————————————*
* text
*———————————————————————-*
* –>P_COLORSTRUCT text *
*———————————————————————-*
form fill_colorstruct using p_colorstruct type slis_coltypes .
p_colorstruct-heacolfir-col = 6.
p_colorstruct-heacolfir-int = 1.
p_colorstruct-heacolfir-inv = 1.
endform. ” FILL_COLORSTRUCT
*———————————————————————*
* FORM TOP_OF_COVERPAGE *
*———————————————————————*
* …….. *
*———————————————————————*
form top_of_coverpage.
call function ‘REUSE_ALV_COMMENTARY_WRITE’
exporting
it_list_commentary = heading1[]
exceptions
others = 1.
endform.
*———————————————————————*
* FORM END_OF_COVERPAGE *
*———————————————————————*
* …….. *
*———————————————————————*
form end_of_coverpage.
call function ‘REUSE_ALV_COMMENTARY_WRITE’
exporting
it_list_commentary = heading2[]
exceptions
others = 1.
endform.
*———————————————————————*
* FORM FOREIGN_TOP_OF_PAGE *
*———————————————————————*
* …….. *
*———————————————————————*
form foreign_top_of_page.
call function ‘REUSE_ALV_COMMENTARY_WRITE’
exporting
it_list_commentary = heading3[]
exceptions
others = 1.
endform.

*———————————————————————*
* FORM FOREIGN_END_OF_PAGE *
*———————————————————————*
* …….. *
*———————————————————————*
form foreign_end_of_page.
call function ‘REUSE_ALV_COMMENTARY_WRITE’
exporting
it_list_commentary = heading4[]
exceptions
others = 1.
endform.
*———————————————————————*
* FORM LIST_MODIFY *
*———————————————————————*
* …….. *
*———————————————————————*
*FORM LIST_MODIFY.
* CALL FUNCTION ‘REUSE_ALV_COMMENTARY_WRITE’
* EXPORTING
* IT_LIST_COMMENTARY = HEADING5[]
* EXCEPTIONS
* OTHERS = 1.
*ENDFORM.
*———————————————————————*
* FORM END_OF_PAGE *
*———————————————————————*
* …….. *
*———————————————————————*
form end_of_page.
call function ‘REUSE_ALV_COMMENTARY_WRITE’
exporting
it_list_commentary = heading6[]
exceptions
others = 1.
endform.
*———————————————————————*
* FORM END_OF_LIST *
*———————————————————————*
* …….. *
*———————————————————————*
form end_of_list.
call function ‘REUSE_ALV_COMMENTARY_WRITE’
exporting
it_list_commentary = heading7[]
exceptions
others = 1.
endform.
*———————————————————————*
* FORM TOP_OF_LIST *
*———————————————————————*
* …….. *
*———————————————————————*
form top_of_list.
call function ‘REUSE_ALV_COMMENTARY_WRITE’
exporting
it_list_commentary = heading8[]
exceptions
others = 1.
endform.
*— End of Program

Share and Enjoy:



 

 

 

 

 

 

 

 

 

 

 

Related posts:

1. ABAP ALV Hirarchical Report Sample Code in sap programming


2. About ALV Grid Colors
3. SAP ALV Grid Events – Double Click
4. ALV Example REUSE_ALV_LIST_DISPLAY
5. ALV Pop Up Screen – FM

How to get and modify subobjects of a main ALV class


Today we will discuss about how to get and modify subobjects of main alvclasses. We know that
the alv object model includes a large number of classes and you can use them to make specific
settings. Take a look at the following picture, it compares the class diagram of the three main ALV
classes.
It’s easy to understand the functionality of each ALV object model, because they are are
structured that you can usually tell from the names which areas of the ALV output you can
change with the methods from these classes.

·Singular or plural
Often you will find class names that exist in both plural and in singular (such as
CL_SALV_COLUMN and CL_SALV_COLUMNS). You can see from the names that in the one
class there are methods that only affect a specific object and in the other class the methods are
for all of these objects.
·ALV tool
Many functions are available for all three ALV tools. However, when there are differences
between the classes of the ALV tools, you can find the right variant by the name (such as
CL_SALV_COLUMN_TABLE, _SALV_COLUMN_HIERSEQ, CL_SALV_COLUMN_TREE)
You get the objects for these classes using the Get methods of the three main ALV classes. The
following table shows which methods in the main ALV classes provide which subobjects:

Methods of Main ALV Classes to Get Subobjects

Method Returns Object of Type Remarks


GET_AGGREGATIONS CL_SALV_AGGREGATIONS
GET_COLUMNS CL_SALV_COLUMNS_TABLE or
CL_SALV_COLUMNS_HIERSEQ or

CL_SALV_COLUMNS_TREE

GET_DISPLAY_SETTINGS CL_SALV_DISPLAY_SETTINGS Not in CL_SALV_TREE


GET_EVENT CL_SALV_EVENTS_TABLE or
CL_SALV_EVENTS_HIERSEQ or

CL_SALV_EVENTS_TREE

GET_FILTERS CL_SALV_FILTERS Not in CL_SALV_TREE


GET_FUNCTIONAL_SETTINGS CL_SALV_FUNCTIONAL_SETTINGS Not in CL_SALV_TREE
GET_FUNCTIONS CL_SALV_FUNCTIONS_LIST Not in CL_SALV_TREE
CL_SALV_FUNCTIONS_TREE Only in CL_SALV_TREE
GET_LAYOUT CL_SALV_LAYOUT
GET_LEVEL CL_SALV_HIERSEQ_LEVEL Only in
CL_SALV_HIERSEQ_TABLE
GET_NODES CL_SALV_NODES Only in CL_SALV_TREE
GET_PRINT CL_SALV_PRINT
GET_SELECTIONS CL_SALV_SELECTIONS Not in CL_SALV_TREE
CL_SALV_SELECTIONS_TREE Only in CL_SALV_TREE
GET_SORTS CL_SALV_SORTS Not in CL_SALV_TREE
GET_TREE_SETTINGS CL_SALV_TREE_SETTINGS Only in CL_SALV_TREE
If one of the classes uses another class, you can also get its objects using the Get
methods.
Class Used Includes Get method Returns Object of Type
CL_SALV_AGGREGATIONS GET_AGGREGATIO CL_SALV_AGGREGATIO
N N
CL_SALV_COLUMNS_TABLE GET_COLUMN CL_SALV_COLUMN
CL_SALV_COLUMNS_HIERSE
Q
CL_SALV_COLUMNS_TREE
CL_SALV GET_DROPDOWNS CL_SALV_DROPDOWNS
_FUNCTIONAL_SETTINGS GET_HYPERLINKS CL_SALV_HYPERLINKS
GET_TOOLTIPS CL_SALV_TOOLTIPS
CL_SALV_FUNCTIONS_LIST GET_FUNCTIONS CL_SALV_FUNCTION
CL_SALV_FUNCTIONS_TREE
CL_SALV_FILTERS GET_FILTER CL_SALV_FILTER
CL_SALV_NODES GET_NODE CL_SALV_NODE
CL_SALV_SORTS GET_SORTS CL_SALV_SORT
If you have created the internal data table and the instance of the ALV main class, you are able to
generate an object for each class used and make the settings as well. To do this, proceed as
follows:

·You declare an object variable for each type of class that you want to use
(such as data: gr_display type ref to cl_salv_display_settings. )
·You use the respective Get method the main ALV class to get the objects of the corresponding
type
(for example gr_display = gr_table->get_display_settings ( ). )
·You use the methods of the class used to assign the desired properties to the object.
(For example
gr_display->set_striped_pattern(value = ‘X’).
gr_display->set_vertical_lines(value = ‘X’).
)

When you have made the settings you display the ALV output with the display method.

How to find BADI


There are several ways to find BADIs in the program.
1. Set a beark-point in method ‘CL_EXITHANDLER=>GET_INSTANCE’. Because old BAID
technology will alwasys call this method to get the BADI instance.

2. Search string ‘CL_EXITHANDLER=>GET_INSTANCE’ in the program. This drawback of this


method is that the program may call another program in the runtime. In this case, you will be able
to find the BADI in another program.

3. You can also go the t-code SPRO, you can also find plenty of BADIs in the related area.

4. You can also find the BADI call using ST05. Below is the steps:

 Go to ST05, then choose ‘table buffer trace’ and activate the trace.
 Run the t-code from where you want to find the BADI.
 Deactive the trace and then display trace.
 using selection criteria object ‘V_EXT_IMP’ and ‘V_EXT_ACT’ and then you can get
called BADI list.
This analyzing technique is based on the fact that all BAdIs are registrated in SAP database
tables. So for each BAdI call these database tables will be accessed. The BAdI database tables
are SXS_INTER, SXC_EXIT, SXC_CLASS and SXC_ATTR. These tables are always accessed
by the views V_EXT_IMP and V_EXT_ACT. So these two ABAP views (T: SE11) will be the basis
for the trace.

Popup for an alv report

************************************************************************
*  Program ID     : ZPOP_UP_SCREEN                                     *
*  Program Title  : alv POP UP Screen                                  *
*  Created By     : kingsxiaoz                                         *
*  Creation Date  : 08.10.2009                                         *
************************************************************************
REPORT zpop_up_screen.
TYPE-POOLS slis.
SELECTION-SCREEN PUSHBUTTON 2(20) btn USER-COMMAND push.
INITIALIZATION.
btn = ‘Open new window’.
AT SELECTION-SCREEN.
PERFORM f_main.
*&———————————————————————*
*&      Form  f_main
*&———————————————————————*
*       ALV display
*———————————————————————-*
FORM f_main.
DATA: lt_spfli TYPE TABLE OF spfli,
lt_fieldcat TYPE slis_t_fieldcat_alv.
PERFORM get_data TABLES lt_spfli.
PERFORM set_catalog CHANGING lt_fieldcat.
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’
EXPORTING
it_fieldcat           = lt_fieldcat
i_screen_start_column = 10
i_screen_start_line   = 1
i_screen_end_column   = 100
i_screen_end_line     = 25
TABLES
t_outtab              = lt_spfli.
ENDFORM.                    “f_main
*&———————————————————————*
*&      Form  get_data
*&———————————————————————*
*       Get the data for output
*———————————————————————-*
*      –>PT_SPFLI   data internal table
*———————————————————————-*
FORM get_data TABLES pt_spfli.
SELECT * FROM spfli INTO TABLE pt_spfli.
ENDFORM.                    “get_data
*&———————————————————————*
*&      Form  set_catalog
*&———————————————————————*
*       set fieldcatalog for alv output
*———————————————————————-*
FORM set_catalog CHANGING pt_fieldcat TYPEslis_t_fieldcat_alv.
DATA lwa_fieldcat TYPE slis_fieldcat_alv.
“macro
DEFINE add_field.
clear lwa_fieldcat.
lwa_fieldcat-col_pos   = &1.
lwa_fieldcat-fieldname = &2.
lwa_fieldcat-seltext_l = &3.
append lwa_fieldcat to pt_fieldcat.
END-OF-DEFINITION.
“add field catalog
add_field 1 ‘MANDT’ ‘Client’.
add_field 2 ‘CARRID’ ‘Airline Code’.
add_field 3 ‘CONNID’ ‘Fight Number’.
add_field 4 ‘COUNTRYFR’ ‘Country From’.
add_field 5 ‘CITYFROM’ ‘City From’.
add_field 6 ‘AIRPFROM’ ‘Airpart From’.
add_field 7 ‘COUNTRYTO’ ‘Country To’.
add_field 8 ‘CITYTO’ ‘City To’.
add_field 9 ‘AIRPTO’ ‘Airpart To’.
add_field 10 ‘FLTIME’ ‘Fly Time’.
add_field 11 ‘DEPTIME’ ‘Dept Time’.
add_field 12 ‘ARRTIME’ ‘Arrive Time’.
add_field 13 ‘DISTANCE’ ‘Distance’.
add_field 14 ‘DISTID’ ‘Mass unit of distance’.
add_field 15 ‘FLTYPE’ ‘Flight type’.
add_field 16 ‘PERIOD’ ‘Arrival n day(s) later’.
ENDFORM.                    “set_catalog

ALV Tips 5 – Use macro to build field catalog


If the output table strcture does not exist in the DDIC, we may need to build field catalog
manually. If you use macro, it can short our code and make the code readable.

 Here is from SAP help about macros.

If you want to reuse the same set of statements more than once in a program, you can include
them in a macro. For example, this can be useful for long calculations or
complex WRITE statements. You can only use a macro within the program in which it is defined,
and it can only be called in lines of the program following its definition.
http://help.sap.com/saphelp_nw70/helpdata/en/9f/db972835c111d1829f0000e829fbfe/frameset.ht
m
You can use below sample code to build field catalog or change it to meet your requirement.

    DATA: ls_line TYPE slis_fieldcat_alv.
    CLEAR: gt_field_cat, gt_field_cat[].
    DEFINE append_alv_field.
      clear ls_line.
      ls_line-fieldname = &1.
      ls_line-tabname = &2.
      ls_line-seltext_l = &3.
      ls_line-no_zero = &4.
      ls_line-do_sum = &5.
      append ls_line to gt_field_cat.
    END-OF-DEFINITION.
    append_alv_field ‘BELNR’    ‘GT_OUTPUT’ text-h01 ” ”.
    append_alv_field ‘BUZEI’    ‘GT_OUTPUT’ text-h02 ” ”.
    append_alv_field ‘BLDAT’    ‘GT_OUTPUT’ text-h03 ” ”.
We use text-h01, text-h02 in order to make it easily to be translated to other languages.

REPORT demo_mod_tech_macros.
DATA: result TYPE i,
      n1     TYPE i VALUE 5,
      n2     TYPE i VALUE 6.
DEFINE operation.
  result = &1 &2 &3.
  output   &1 &2 &3 result.
END-OF-DEFINITION.
DEFINE output.
  write: / 'The result of &1 &2 &3 is', &4.
END-OF-DEFINITION.
operation 4 + 3.
operation 2 ** 7.
operation n2 - n1.
This produces the following output:
The result of 4 + 3 is          7
The result of 2 ** 7 is        128
The result of N2 - N1 is          1
In this example, the two macros operation and output are defined. output is
nested in operation.operation is called three times with different parameters.
Note how the placeholders &1, &2,... are replaced in the macros.
SAP ALV Tutorial 4 – Add button to ToolBar
     In this alv tutorial serial, we have already learned how to handle event. This article will show
you how to add your custom button to the toolbar of alv. As usual, we will introduce it both in
object-oriented method and the function module method. You can choose the method you like.
Function Module Method
Because the function module method default use the full screen and the alvbutton is implemented
using GUI Status. If you want to add your own button to tool bar, what you want to do is just copy
the standard GUI status out and make your own modification.
Step 1. Copy standard ALV GUI status to your own program from programSAPLKKBL GUI
Status STANDARD_FULLSCREEN.
Go to this program and select status, click on GUI Status -> Copy, then enter your Z program and
the name you want for the new status.

Step 2. Add or delete buttons in the z-veriosn of your GUI status.


Step 3. There are two way to use your own GUI status in the ALV. A simple way is that pass the
status name to function module as a parameter.
call function ‘REUSE_ALV_GRID_DISPLAY’
    EXPORTING
      i_callback_program = ‘ZSDBOLST_REPORT’
      i_callback_pf_status_set = ‘STANDARD’ 
      i_callback_user_command = ‘USER_COMMAND’
      i_structure_name = ‘I_BOLACT’
      i_grid_title = ‘BOL Action Report’(031)
      is_layout = gs_layout
      it_fieldcat = gt_fieldcat[]
      i_save = ‘A’
      is_variant = v_variant
   TABLES
      t_outtab = i_bolact
   EXCEPTIONS
      program_error = 1
      others = 2.
Another way is that register an subroutine for event ‘PF_STATUS_SET’. In the routine, you use
statement “SET PF-STATUS ‘STANDARD’.”.
Step 4. This is an optional step, but we also need to register event ‘USER_COMMAND’ to handle
your custom function code.
At last, also it is not an common requirement, but we can exclude some button from standard
ALV toolbar without copying it out.
First, we need to define an internal table to contain excluded function code.

DATA : it_exclude TYPE slis_t_extab,


                wa_exclude TYPE slis_extab.
Then, add the excluded button from alv toolbar and pass this internal table to the funciton module
‘REUSE_ALV_GRID_DISPLAY’.

wa_exclude-fcode = ‘&OUP’.
APPEND wa_exclude TO it_exclude.
CLEAR wa_exclude.
wa_exclude-fcode = ‘&ODN’.
APPEND wa_exclude TO it_exclude.

CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’


   EXPORTING
      i_callback_program = rep_id
      i_callback_user_command = ‘COMMAND’
      i_callback_top_of_page = ‘TOP’
      i_grid_title = wa_title
      is_layout = wa_layout
      it_fieldcat = it_field
      it_excluding = it_exclude “excule buttons
Object Oriented Method
For object oriented method, SAP has alreay provide some example code for developer to
reference. Report ‘BCALV_GRID_08′ is a good start for adding your own button to ALV. Because
the tool bar is part of custom control,  you must use method provide by class ‘CL_GUI_GRID’ to
add your own toolbar. If you can access SAP system, you can find a very detailed comments in
the code, and it is also easy to use.

SAP ALV Tutorial 2 – Event


     This article deals with event handling in SAP alv and mechanism behind it. In a
nutshell, alv Grid is an implementation of custom control. You must be very familiar with the
dynpro programming. In dynpro programming, we general have two events: PBO and PAI. If user
have some actions in the frontend screen, the event PAI will be triggered and program can have
different reactions according to different function codes. In the custom control, things become a
slight different. It will trigger an specific event, if user does some actions in the frontend. There
are two types of event: system event and application event.
The only difference of the two events are the sequence of triggering event handler method and
the PAI event.

System event: A system event is triggered before any automatic field checks (for
example,required fields) have taken place on the screen, and before any field transport. The PAI
and PBO events are not triggered. Consequently, you cannot access any values that the user has
just changed on the screen. Furthermore, there is no field transport back to the screen after the
event, so values that you have changed in the event handling are not updated onthe screen.
The handler method that you defined for the event is called automatically by the system.However,
you can use the method set_new_ok_code  to set a new value for the OK_CODE field. This then
triggers the PAI and PBO modules, and you can evaluate the contents of the OK_CODE field as
normal in a PAI module.
Application event: This event is processed in the PAI event. Consequently, all field checks and
field transport has taken place. If you want the event handler method to be called at a particular
point in your application program, you must process the event using the static
method CL_GUI_CFW=>DISPATCH.
Events in alv
The ALV Grid Control uses the grid control to display the output table. So we can say that the
ALV Grid Control is a wrapper that uses the wrapper of a Basis control. As the ‘outer layer’, this
wrapper spares the developer from having to register the events on the frontend in order to
simplify event handling. The ALV Grid Control differs from Basis controls in the following
respects:All events are registered as system events when the control is instantiated.If you want to
register all events as application events, you must use parameter I_APPL_EVENTS ( See also:
CONSTRUCTOR). As usual, you must then call method CL_GUI_CFW=>DISPATCH in the PAI
module.
Events DELAYED_CALLBACK or DELAYED_CHANGED_SEL_CALLBACK are registered using
method register_delayed_event.

Below are all the events that are valid in ALV. But there are some differences depanding on how
we call the ALV.

In object oriented method, we generally have below events:

User-defined Text Output


print_end_of_list Define output text to be printed at the end of the entire list
print_top_of_list Define output text to be printed at the beginning of the entire list
print_end_of_page Define output text to be printed at the end of each page
print_top_of_page Define output text to be printed at the beginning of each page
subtotal_text Define self-defined subtotals texts
Mouse-controlled Actions in the Grid Control
button_click Query a click on a pushbutton in the ALV Grid Control
double_click Query a double-click on a cell of the ALV Grid control
hotspot_click Query a hotspot click on columns defined for this purpose in advance
onDrag Collect information when elements of the ALV Grid Control are dragged
onDrop Process information when elements of the ALV Grid Control are dropped
onDropComplete Perform final actions after successful Drag&Drop
onDropGetFlavor Distinguish between options for Drag&Drop behavior
Processing of Self-defined and Standard Functions
before_user_command Query self-defined and standard function codes
user_command Query self-defined function codes
after_user_command Query self-defined and standard function codes
Definition of Self-defined Functions
toolbar Change, delete or add GUI elements in the toolbar
menu_button Define menus for menu buttons in the toolbar
context_menu_request Change context menu
onf1 Define self-defined F1 help
In function module method, we can call funciton module’REUSE_ALV_EVENTS_GET’to get all
the events supported by FM ‘REUSE_ALV_GRID_DISPLAY’. You can also find all the event
constants in the type pool SLIS, e.g. SLIS_EV_TOP_OF_PAGE,SLIS_EV_USER_COMMAND.
Ok, above are all the basic introduction about ALV event and its mechanism. In the next article of
this series, I will use some specific code to demo how to use event both in OO and FM method.

SAP ALV Tutorial 3 – Event Example


In some cases, we have requirements to handle events in alv. There are some difference of using
event between OO method and FM method.
Object-oriented method
We can see all the supported events as well as the parameters in class CL_GUI_ALV_GRID. We
need to create an class definition and an class implementation. Below is an code example for
event ‘Double Click’.

CLASS grid_event_receiver DEFINITION.
PUBLIC SECTION.
METHODS:
handler_Double_click
FOR EVENT DOUBLE_CLICK of cl_gui_alv_grid ” we need to register this method as an event
handler
IMPORTING e_row e_column,
ENDCLASS.                    “grid_event_rece
CLASS grid_event_receiver IMPLEMENTATION.
METHOD handler_Double_click.
DATA: ls_sflight like line of gt_sflight.
READ TABLE gt_sflight INDEX e_row-index INTO ls_sflight.
SELECT * from sbook INTO TABLE gt_sbook
WHERE carrid  = ls_sflight-carrid
and    connid  = ls_sflight-connid
and    fldate  = ls_sflight-fldate.
PERFORM DETAIL_LIST.
ENDMETHOD.                 “handler_Double_click
ENDCLASS.                   “grid_event_receiver IMPLEMENTATION
Then, we need to define some variants in the program.

DATA: custom_container type ref to cl_gui_custom_container,    “custom_container:the container
of grid1
grid1  type ref to cl_gui_alv_grid,       “The main alv grid to show the mail data
grid2  type ref to cl_gui_alv_grid,       “The Detail alv grid to show the detail data
dialogbox_container type ref to cl_gui_dialogbox_container,        “dialogbox_container:The
container of grid2
event_receiver type ref to grid_event_receiver,             “event_receiver:Point
to grid_event_recerive
In the PBO of the screen, we first initialize all the variants and register the event instance to the
grid instance. Below is the code:

IF custom_container is INITIAL.
PERFORM Get_DATA TABLES GT_OUTPUT[].     “Get data from table and fill the data into inter
nal table GT_OUTPUT
CREATE OBJECT custom_container            “Create the instance of custom_container
EXPORTING
CONTAINER_NAME = ’ALV_GRID’         “Set the Container_name to ’ALV_GRID’(Define in Scre
en)
exceptions
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5.
CREATE OBJECT grid1 EXPORTING i_parent = custom_container.  “Create the instance of grid
1 and set custom_container as grid1’s Container
gs_layout-grid_title = ’Flight’(100).
gs_layout-excp_fname = g_lights_name.
gs_layout-sel_mode = ’A’.
CALL METHOD grid1->SET_TABLE_FOR_FIRST_DISPLAY       “Call the method to show data
EXPORTING
I_STRUCTURE_NAME              = ’SFLIGHT’
CHANGING
IT_OUTTAB                     = GT_OUTPUT[]
*    EXCEPTIONS
*      INVALID_PARAMETER_COMBINATION = 1
*      PROGRAM_ERROR                 = 2
*      TOO_MANY_LINES                = 3
*      others                        = 4.
IF SY-SUBRC <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
CREATE OBJECT event_receiver.                     “Create the instance of event_receiver
set HANDLER event_receiver->handler_Double_click FOR grid1.   “register the double_click eve
nt to grid1
ENDIF.
Ok, if user double click any cell, the method ‘handler_Double_click’ of class ‘grid_event_receiver’
will be executed. You can add the your custom code in this method implentation. For example,
read the current line user clicked and displayed an detailed ALV list.

Function module method


If you use the function module, such as ‘REUSE_ALV_GRID_DISPLAY’, thing become simpler
than object oriented method. Because the function module encapsulate the event class detail and
registration of event class to the grid instance. what we need to do is build an event and
subroutine mapping table and transfer this table to the FM. If any registered event is triggered by
the user, the subroutine will be called automatically. Below is an example:

GT_EVENTS      TYPE SLIS_T_EVENT,  ” internal table that hold all the events and it’s call back
subroutine

CALL FUNCTION ’REUSE_ALV_EVENTS_GET’
EXPORTING
I_LIST_TYPE = 0
IMPORTING
ET_EVENTS   = RT_EVENTS.
READ TABLE RT_EVENTS WITH KEY NAME = SLIS_EV_TOP_OF_PAGE
INTO LS_EVENT.
IF SY-SUBRC = 0.
MOVE G_TOP_OF_PAGE TO LS_EVENT-FORM.
APPEND LS_EVENT TO RT_EVENTS.
ENDIF.

*”Display List
CALL FUNCTION ’REUSE_ALV_GRID_DISPLAY’
IT_EVENTS          = GT_EVENTS[]

SAP ALV Tutorial 4 – Add button to ToolBar


     In this alv tutorial serial, we have already learned how to handle event. This article will show
you how to add your custom button to the toolbar of alv. As usual, we will introduce it both in
object-oriented method and the function module method. You can choose the method you like.
Function Module Method
Because the function module method default use the full screen and the ALV button is
implemented using GUI Status. If you want to add your own button to tool bar, what you want to
do is just copy the standard GUI status out and make your own modification.

Step 1. Copy standard ALV GUI status to your own program from programSAPLKKBL GUI
Status STANDARD_FULLSCREEN.
Go to this program and select status, click on GUI Status -> Copy, then enter your Z program and
the name you want for the new status.

Step 2. Add or delete buttons in the z-veriosn of your GUI status.


Step 3. There are two way to use your own GUI status in the ALV. A simple way is that pass the
status name to function module as a parameter.
call function ‘REUSE_ALV_GRID_DISPLAY’
    EXPORTING
      i_callback_program = ‘ZSDBOLST_REPORT’
      i_callback_pf_status_set = ‘STANDARD’ 
      i_callback_user_command = ‘USER_COMMAND’
      i_structure_name = ‘I_BOLACT’
      i_grid_title = ‘BOL Action Report’(031)
      is_layout = gs_layout
      it_fieldcat = gt_fieldcat[]
      i_save = ‘A’
      is_variant = v_variant
   TABLES
      t_outtab = i_bolact
   EXCEPTIONS
      program_error = 1
      others = 2.
Another way is that register an subroutine for event ‘PF_STATUS_SET’. In the routine, you use
statement “SET PF-STATUS ‘STANDARD’.”.
Step 4. This is an optional step, but we also need to register event ‘USER_COMMAND’ to handle
your custom function code.
At last, also it is not an common requirement, but we can exclude some button from standard
ALV toolbar without copying it out.
First, we need to define an internal table to contain excluded function code.

DATA : it_exclude TYPE slis_t_extab,


                wa_exclude TYPE slis_extab.
Then, add the excluded button from alv toolbar and pass this internal table to the funciton module
‘REUSE_ALV_GRID_DISPLAY’.

wa_exclude-fcode = ‘&OUP’.
APPEND wa_exclude TO it_exclude.
CLEAR wa_exclude.
wa_exclude-fcode = ‘&ODN’.
APPEND wa_exclude TO it_exclude.

CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’


   EXPORTING
      i_callback_program = rep_id
      i_callback_user_command = ‘COMMAND’
      i_callback_top_of_page = ‘TOP’
      i_grid_title = wa_title
      is_layout = wa_layout
      it_fieldcat = it_field
      it_excluding = it_exclude “excule buttons
Object Oriented Method
For object oriented method, SAP has alreay provide some example code for developer to
reference. Report ‘BCALV_GRID_08′ is a good start for adding your own button to ALV. Because
the tool bar is part of custom control,  you must use method provide by class ‘CL_GUI_GRID’ to
add your own toolbar. If you can access SAP system, you can find a very detailed comments in
the code, and it is also easy to use.
SAP ALV Tutorial 5 – Sort,Subtotal,Total
     End user always wants to see total values of alv data. Although, they can download
the alv report to an Excel file and do some more operations, but ALV already provides these
basic functionalities to the user for saving time. So this article will cover this topic.
Function module method
It is very convenient to implement this using function module method. Below is the step.

Step 1. Create an internal table definition for which fields need to be sorted.

gt_sort type slis_t_sortinfo_alv,
gs_sort type slis_sortinfo_alv.
Step 2. Fill above internal table according your requirement.

clear gs_sort.
 gs_sort-fieldname = ‘KHD’.
 gs_sort-up        = ‘X’. “ the sort direction of this field
 gs_sort-subtot    = ‘X’. ” whether the subtotal will be showed on this field
 append gs_sort to gt_sort.
 clear ls_sort.
 gs_sort-fieldname = ‘MATNR’.
 gs_sort-up        = ‘X’.
 gs_sort-subtot    = ‘X’.
 append gs_sort to gt_sort.
Step 3. Enable sort on a field when define the field catalog.

  data ls_fieldcat type slis_fieldcat_alv.

  clear ls_fieldcat.
  ls_fieldcat-fieldname    = ‘KHD’.
  ls_fieldcat-tabname      = ‘gt_outtab’.
  ls_fieldcat-outputlen    = 10.
  ls_fieldcat-do_sum       = ‘X’. “ do total flag
  ls_fieldcat-key          = ‘X’.
  append ls_fieldcat to lt_fieldcat.

Step 4. Finally, we need to is to pass these internal table to function module
‘REUSE_ALV_GRID_DISPLAY’.
This is from program pespective. We can also change the ALV layout when displaying the ALV if
it allows you to do so.

Object-oriented method
It is also very easy to implement sort and total using object-oriented.

Step 1. Create an internal table definition for which fields need to be sorted. We need to note that
the type for sort is diffrerent from that in FM method.

Data: gt_sort type lvc_t_sort,


            gs_sort type lvc_s_sort.
Step 2. Fill above internal table according your requirement. This step is the same as fm method.
Although, they have different types but have the same attibutes.

Step 3. Enable sort on a field when define the field catalog. This is same also.

Step 4. Pass above internal table to the method ‘set_table_for_first_display‘ of class


‘CL_GUI_ALV_GRID’.
SAP ALV Tutorial 7 – Make ALV editable
Sometimes, we need to make our alv editable. SAP has already provide table control to modify
table data(previously we use loop step instead). But alv can provide more functionality to support
sort, total,filter, etc.
  As far as the edit feature is concerned, 
a cell of the alv grid control can have the following states:

1.   non-editable : edit feature not set for this cell
2.   editable     : edit feature set for this cell
   The second state (“editable”) has two substates:

1.   editable and not ready for input: edit feature set but not active
2.   editable and ready for input: edit feature set and active
You switch between “editable and not ready for input” and “editable and ready for input” using
method SET_READY_FOR_INPUT. Below is a piece of code.

    CALL METHOD g_grid->set_table_for_first_display
         EXPORTING i_structure_name = ’SFLIGHT’
                   is_layout        = gs_layout
         CHANGING  it_outtab        = gt_outtab.
*2.Use SET_READY_FOR_INPUT to allow editing initially.
*   (state ”editable and ready for input”).
    CALL METHOD g_grid->set_ready_for_input
          EXPORTING i_ready_for_input = 1.
As mentioned above, we can set the editable feature to a cell level. It means that we can set
some cells to be editable and some other in display mode according your requirement. Below is
the step to achive this.

Step 1. entend your output table for a field  e.g., CELLTAB, that holds  


information about the edit status of each cell for the corresponding row.

DATA: BEGIN OF gt_outtab occurs 0.  ”with header line
        include structure sflight.
DATA: celltab type LVC_T_STYL.
DATA: END OF gt_outtab.
Step 2.   After selecting data, set edit status for each row in a loop according to your requirement.

  ls_celltab-fieldname = ’CARRID’.
  ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
  INSERT ls_celltab INTO TABLE pt_celltab.
  ls_celltab-fieldname = ’CONNID’.
  ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.
  INSERT ls_celltab INTO TABLE pt_celltab.

Step 3.  
Provide the fieldname of the celltab field by using field STYLEFNAME of the layout structure.

  gs_layout-stylefname = ’CELLTAB’.

Now we can control the editable of the ALV using method.

FORM switch_edit_mode.

  IF grid1->is_ready_for_input( ) eq 0.
* set edit enabled cells ready for input
    CALL METHOD grid1->set_ready_for_input
                     EXPORTING i_ready_for_input = 1.
  ELSE.
* lock edit enabled cells against input
    CALL METHOD grid1->set_ready_for_input
                    EXPORTING i_ready_for_input = 0.
  ENDIF.
ENDFORM.                               ” SWITCH_EDIT_MODE
Standard programs ‘BCALV_EDIT_01′ and ‘BCALV_EDIT_02′ are very good example for ALV
edit functionality. After this article, we can make the ALV grid to be editable, but no event is
registered and no error handling is performed. In next article, we use other SAP standard
program to do more explaination.

SAP ALV Tutorial 8 – Handling Data Change


     In previous article, we have already set alv editable, but we still need to validate the input data
as well as save the input data to the database. In another case, we also need to change
according to user’s event.
   As we can now make our alv Grid editable we may require controlling input data. The ALV Grid
has events data_changed anddata_changed_finished. The former method is triggered just
after the change at an editable field is perceived.  Here you can make checks for the input. And
the second event is triggered after the change is committed. You can select the way how the
control perceives data changes by using the methodregister_edit_event. You have two choices:

    1. After return key is pressed: To select this way, to the parameter event_idpass
cl_gui_alv_grid=>mc_evt_enter.
    2. After the field is modified and the cursor is moved to another field: For this, pass
cl_gui_alv_grid=>mc_evt_modifies to the same parameter.
To make events controlling data changes be triggered, you must select eitherway by calling this
method. Otherwise, these events will not be triggered.

To control field data changes, ALV  Grid uses an instance of the class
“CL_ALV_CHANGED_DATA_PROTOCOL” and passes this via the event data_changed. Using
methods of this class, you can get and modify cell values and produce error messages.

Utilizing these methods and attributes you can check and give proper message and also modify
the cell content.

FORM handle_data_changed USING ir_data_changed


                         TYPE REF TO cl_alv_changed_data_proto
 
  DATA : ls_mod_cell  TYPE lvc_s_modi  ,
         lv_value      TYPE lvc_value  .
 
  SORT ir_data_changed->mt_mod_cells BY row_id .
  LOOP AT ir_data_changed->mt_mod_cells
                         INTO ls_mod_cell
                         WHERE fieldname = ‘SEATSMAX’ .
 
    CALL METHOD ir_data_changed->get_cell_value
                  EXPORTING i_row_id     =  ls_mod_cell-row_id
                            i_fieldname  =  ‘CARRID’
                  IMPORTING e_value      =  lv_value .
 
    IF lv_value = ‘THY’ AND ls_mod_cell-value > ‘500′ .
      CALL METHOD ir_data_changed->add_protocol_entry
           EXPORTING
             i_msgid = ‘SU’
             i_msgno = ‘000′
             i_msgty = ‘E’
             i_msgv1 = ‘This number can not exceed 500 for ‘
             i_msgv2 = lv_value
             i_msgv3 = ‘The value is et to ”500”’
             i_fieldname = ls_mod_cell-fieldname
             i_row_id = ls_mod_cell-row_id .
 
      CALL METHOD ir_data_changed->modify_cell
             EXPORTING i_row_id    = ls_mod_cell-row_id
                       i_fieldname = ls_mod_cell-fieldname
                       i_value     = ‘500′ .
    ENDIF .
 
  ENDLOOP .
 
ENDFORM.                    ” handle_data_changed

SAP ALV Tutorial 11 – Currency/Quantity field


If the field in alv output table is for currency or quantity, we need to do some effort to display it in
our alv. you must assign these fields to a currency or unit. There are three ways how you can do
this in the field catalog:
 Reference to a Currency or Unit Field
 Value or Unit for the Entire Column
 Format the value field manually
Reference to a Currency or Unit Field
If you define your output table as an structure in DDIC, you need to provide corrsponding
reference table and field for currency/quantity field when you activate it. The reference field can
be used to display values in the correct user-specific format.

When you read the whole field catalog of the output table using function
module‘REUSE_ALV_FIELDCATALOG_MERGE’, you can automatically fill the
fieldCFIELDNAME or QFIELDNAME in field catalog structure. That means you can fill above two
field maually if your output table type is defined in the program not in DDIC.
It is also very useful when you want to have subtotal according to different currency or unit.The
field catalog also contains an entry for the unit field. If you do not want to display the unit as a
column in the list and do not want to allow users to interactively show it as a column, you can
mark the field catalog entry for the unit field as a technical field by setting field TECH. This makes
sense, for example, if the unit is always unique and therefore explicitly output in the list header by
the caller.
For initial value or currency fields, you must consider the following points:

Totals calculation and display of value fields


  Value of unit field
not initial initial
    Value of Unit-specific display of digits For such cells, alv introduces the
value field after the comma in the list and in unit SPACE . In the totals
the totals display, this unit is given as a
separate unit.
initial Unit-specific output as ‘0′ Output asSPACE . The value field
(provided field NO_ZERO of is ignored when the totals are
the field catalog is initial). When calculated.
calculating totals, the ALV uses
the value ‘0′ and the unit
specified.
Below is an example when vlaue of value field is initial. It displays value ‘o.oo’ or space according
to the value of reference unit field.

When we have value in reference unit field, the value field displays as ‘o.oo’. But if we set field
‘NO_ZERO’ in field catlog, the output should be space.
If the reference unit field is initial and value field is also initial,  the output is space.

Value or Unit for the Entire Column


For fields that use the same quantity or currency unit for all column values, the field catalog
contains fieldsCURRENCY QUANTITY see below). You use these fields to determine a unit for
your value field (such as USD or KG ). This makes sense, for example, if there is only one unit or
currency for the entire column (which has been entered by the user, for example). In this case,
the output table does not need any additional fields.
Format the value field manually
Fields in field catalog structure DECIMALS_O , EDIT_MASK , EXPONENT , NO_SIGN and
ROUND correspond to WRITE additions DECIMALS , USING EDIT MASK , EXPONENT , NO-
SIGN and ROUND . We set proper value in above field to display custom format of value without
setting reference field. You can find SAP standard document about how to use these fields here:

SAP ALV Tutorial 10 – Convert ALV to PDF


In some case, the report is executed in the background because the report will execute for a long
time. If we define a background job for a report,  the result of the report will be send to the spool
system.
To run an ABAP program in the background, you must provide a variant for reports with selection
screens or the ABAP program must supply its own runtime parameters. You can specify the SAP
user under whose authorizations the program should run, as well as how the spool system should
handle output from the program.

Output generated by an ABAP program is held in the SAP spool system as a spool request. The
job step printing and archiving specifications specify how to handle this output. Spool requests
can also be automatically mailed via SAPoffice to any user designated in the job definition, but
because output can be rather large, this option should be used with care.

But if we use the object oriented method to display the alv, we need some more code to handle
background option, otherwise, we may encounter an error “Control Framework: Fatal error – GUI
cannot be reached”. We need to use method offline in class CL_GUI_GRID to determine if
the alv Grid Control runs without frontend handling. Below is sample code you can use when
creating analv Grid instance.
DATA: g_dock TYPE REF TO cl_gui_docking_container,
      g_grid TYPE REF TO cl_gui_alv_grid.
IF cl_gui_alv_grid=>offline( ) is initial.
    CREATE OBJECT g_dock.
ENDIF.
CREATE OBJECT g_grid
    EXPORTING I_PARENT = g_dock.
After we display the ALV, we can check whether it is running in the background using system
field ‘sy-batch‘. Then,we call an function module named‘GET_JOB_RUNTIME_INFO’ to get the
current job information. Then go to spool request table tbtcp to get the spool id.
* Get current job details
  CALL FUNCTION ‘GET_JOB_RUNTIME_INFO’
       IMPORTING
            eventid                 = gd_eventid
            eventparm               = gd_eventparm
            external_program_active = gd_external_program_active
            jobcount                = gd_jobcount
            jobname                 = gd_jobname
            stepcount               = gd_stepcount
       EXCEPTIONS
            no_runtime_info         = 1
            OTHERS                  = 2. 
SELECT * FROM  tbtcp
                 INTO TABLE it_tbtcp
                 WHERE      jobname     = gd_jobname
AND jobcount = gd_jobcount
AND stepcount = gd_stepcount
AND listident <> ‘0000000000′
                 ORDER BY   jobname
                            jobcount
                            stepcount.
  READ TABLE it_tbtcp INTO wa_tbtcp INDEX 1.

Finally, we can call function module ‘CONVERT_ABAPSPOOLJOB_2_PDF‘ to convert spool


reqeust(which is stored in OTF format) to PDF format. Then we can call either function
module ‘SO_DOCUMENT_SEND_API1′ or SAP BCS (Business Communication Service) to
send the pdf as an email attachment.
CALL FUNCTION ‘CONVERT_ABAPSPOOLJOB_2_PDF’
       EXPORTING
            src_spoolid              = gd_spool_nr
            no_dialog                = c_no
            dst_device               = c_device
       IMPORTING
pdf_bytecount = gd_bytecount
       TABLES
pdf = it_pdf_output
       EXCEPTIONS
            err_no_abap_spooljob     = 1
            err_no_spooljob          = 2
            err_no_permission        = 3
            err_conv_not_possible    = 4
            err_bad_destdevice       = 5
            user_cancelled           = 6
            err_spoolerror           = 7
            err_temseerror           = 8
            err_btcjob_open_failed   = 9
            err_btcjob_submit_failed = 10
            err_btcjob_close_failed  = 11
            OTHERS                   = 12. 

Das könnte Ihnen auch gefallen