Beruflich Dokumente
Kultur Dokumente
************************************************************************
* 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
·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:
CL_SALV_COLUMNS_TREE
CL_SALV_EVENTS_TREE
·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.
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.
************************************************************************
* 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
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.
wa_exclude-fcode = ‘&OUP’.
APPEND wa_exclude TO it_exclude.
CLEAR wa_exclude.
wa_exclude-fcode = ‘&ODN’.
APPEND wa_exclude TO it_exclude.
…
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.
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.
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[]
…
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.
wa_exclude-fcode = ‘&OUP’.
APPEND wa_exclude TO it_exclude.
CLEAR wa_exclude.
wa_exclude-fcode = ‘&ODN’.
APPEND wa_exclude TO it_exclude.
…
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.
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.
Step 3. Enable sort on a field when define the field catalog. This is same also.
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.
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’.
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.
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.
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:
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.
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.