03_ALV Exercise Solutions
-
Upload
bakkalibilal -
Category
Documents
-
view
5 -
download
0
description
Transcript of 03_ALV Exercise Solutions
ALV Exercises
ECC 6.0
Exercises: ABAP List Viewer
SAP Development ABAP Training
Chapter 1Exercise 1.1Create a program that uses REUSE_ALV_LIST_DISPLAY function module to show Airline and Flight Schedule from SCARR (Airline) and SPFLI (Flight schedule).1. Populate the Field Catalog before calling the AVL List display function module.
2. Display the following columns in the output.
Solution: Follow the standard SAP program BALVSD03.REPORT YALVXX01_1.
*DatadeclarationTYPE-POOLS:slis.*GlobalstructureoflistTYPES:BEGINOFud_struct,carridTYPES_CARR_ID,carrnameTYPES_CARRNAME,connidTYPES_CONN_ID,cityfromTYPES_FROM_CIT,airfromTYPES_FROMAIRP,citytoTYPES_TO_CITY,airptoTYPES_TOAIRP,arrtimeTYPES_ARR_TIME,distanceTYPES_DISTANCE,distidTYPES_DISTID,ENDOFud_struct.
DATA:gt_fieldcatTYPEslis_t_fieldcat_alv.DATA:gt_outtabTYPESTANDARDTABLEOFud_struct.DATA:g_repidLIKEsy-repid.
*InitializationfieldcatalogINITIALIZATION.g_repid=sy-repid.PERFORMfieldcat_initCHANGINGgt_fieldcat[].
*DataselectionSTART-OF-SELECTION.PERFORMselect_dataCHANGINGgt_outtab.
*DisplaylistEND-OF-SELECTION.
CALLFUNCTION'REUSE_ALV_LIST_DISPLAY'EXPORTING
I_CALLBACK_PROGRAM=g_repid
IT_FIELDCAT=gt_fieldcat[]TABLESt_outtab=gt_outtab.
*-----------------------------------------------------------------------*Forms*-----------------------------------------------------------------------
*Initializationfieldcatalog
FORMfieldcat_initCHANGINGrt_fieldcatTYPEslis_t_fieldcat_alv.
DATA:ls_fieldcatTYPEslis_fieldcat_alv.DATA:posTYPEiVALUE1.
CLEARls_fieldcat.ls_fieldcat-col_pos=pos.ls_fieldcat-fieldname='CARRID'.ls_fieldcat-key='X'.APPENDls_fieldcatTOrt_fieldcat.
CLEARls_fieldcat.pos=pos+1.ls_fieldcat-col_pos=pos.ls_fieldcat-fieldname='CARRNAME'.ls_fieldcat-ref_fieldname='CARRNAME'.ls_fieldcat-ref_tabname='SCARR'.APPENDls_fieldcatTOrt_fieldcat.
CLEARls_fieldcat.pos=pos+1.ls_fieldcat-col_pos=pos.ls_fieldcat-fieldname='CONNID'.ls_fieldcat-ref_fieldname='CONNID'.ls_fieldcat-ref_tabname='SPFLI'.APPENDls_fieldcatTOrt_fieldcat.
CLEARls_fieldcat.pos=pos+1.ls_fieldcat-col_pos=pos.ls_fieldcat-fieldname='CITYFROM'.ls_fieldcat-ref_fieldname='CITYFROM'.ls_fieldcat-ref_tabname='SPFLI'.APPENDls_fieldcatTOrt_fieldcat.
CLEARls_fieldcat.pos=pos+1.ls_fieldcat-col_pos=pos.ls_fieldcat-fieldname='AIRPFROM'.ls_fieldcat-ref_fieldname='AIRPFROM'.ls_fieldcat-ref_tabname='SPFLI'.ls_fieldcat-no_out='X'.APPENDls_fieldcatTOrt_fieldcat.
CLEARls_fieldcat.pos=pos+1.ls_fieldcat-col_pos=pos.ls_fieldcat-fieldname='CITYTO'.ls_fieldcat-ref_fieldname='CITYTO'.ls_fieldcat-ref_tabname='SPFLI'.APPENDls_fieldcatTOrt_fieldcat.
CLEARls_fieldcat.pos=pos+1.ls_fieldcat-col_pos=pos.ls_fieldcat-fieldname='AIRPTO'.ls_fieldcat-ref_fieldname='AIRPTO'.ls_fieldcat-ref_tabname='SPFLI'.ls_fieldcat-no_out='X'.APPENDls_fieldcatTOrt_fieldcat.
CLEARls_fieldcat.pos=pos+1.ls_fieldcat-col_pos=pos.ls_fieldcat-fieldname='ARRTIME'.ls_fieldcat-ref_fieldname='ARRTIME'.ls_fieldcat-ref_tabname='SPFLI'.APPENDls_fieldcatTOrt_fieldcat.
CLEARls_fieldcat.pos=pos+1.ls_fieldcat-col_pos=pos.ls_fieldcat-fieldname='DISTANCE'.ls_fieldcat-ref_fieldname='DISTANCE'.ls_fieldcat-ref_tabname='SPFLI'.ls_fieldcat-no_out='X'.APPENDls_fieldcatTOrt_fieldcat.
CLEARls_fieldcat.pos=pos+1.ls_fieldcat-col_pos=pos.ls_fieldcat-fieldname='DISTID'.ls_fieldcat-ref_fieldname='DISTID'.ls_fieldcat-ref_tabname='SPFLI'.ls_fieldcat-no_out='X'.APPENDls_fieldcatTOrt_fieldcat.ENDFORM."fieldcat_init
*DataselectionFORMselect_dataCHANGINGrt_outtabLIKEgt_outtab[].SELECTDISTINCTscarr~carridscarr~carrnamespfli~connidspfli~cityfromspfli~airpfromspfli~citytospfli~airptospfli~arrtimespfli~distancespfli~distidINTOCORRESPONDINGFIELDSOFTABLErt_outtabFROMscarrINNERJOINspfliONscarr~carrid=spfli~carridWHEREscarr~carridBETWEEN'AA'AND'LH'.
ENDFORM."select_dataThe output should be displayed as follows:
Exercise 1.2 Create a program that uses REUSE_ALV_GRID_DISPLAY function module to show the content of YCONCERT table.
1. Modify the program so that the report displays a title Concert Information as report title.
2. Change the display position of CDATE as column 1 and ARTIST as column 2.
3. Do not show the column MAXSEATS in the output.4. Optimize width of the output columns.Solution:*&---------------------------------------------------------------------*
*& Report YALVXX01_2
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT YALVXX01_2. *DatadeclarationTYPE-POOLS:slis.
TYPES:BEGINOFt_outtab,artistTYPEchar30,"ArtistNamecdateTYPEdatum,"ConcertDateoccupiedTYPEnumc2,"OccupiedSeatsmaxseatsTYPEnumc2,"MaximumSeatslocationTYPEchar10,"ConcertLocationpriceTYPEzprice,"PriceofConcertTicketENDOFt_outtab.
TYPES:ty_outtabTYPESTANDARDTABLEOFt_outtab.DATA:gt_fieldcatTYPEslis_t_fieldcat_alv.DATA:gt_outtabTYPESTANDARDTABLEOFt_outtab.DATA:g_repidLIKEsy-repid.DATA:wa_layoutTYPEslis_layout_alv,"ALVLayout
*Initializationfieldcataloginitialization.g_repid=sy-repid.
*PreaparetheFieldCatalogPERFORMfieldcat_initCHANGINGgt_fieldcat[].
*CustomizetheLayoutwa_layout-colwidth_optimize='X'.
*DataselectionSTART-OF-SELECTION.PERFORMselect_dataCHANGINGgt_outtab.
*DisplayGridoutputEND-OF-SELECTION.
CALLFUNCTION'REUSE_ALV_GRID_DISPLAY'EXPORTING*I_INTERFACE_CHECK=''*I_BYPASSING_BUFFER=''*I_BUFFER_ACTIVE=''i_callback_program=g_repid*I_CALLBACK_PF_STATUS_SET=''*I_CALLBACK_USER_COMMAND=''*I_CALLBACK_TOP_OF_PAGE=''*I_CALLBACK_HTML_TOP_OF_PAGE=''*I_CALLBACK_HTML_END_OF_LIST=''*I_STRUCTURE_NAME=*I_BACKGROUND_ID=''*I_GRID_TITLE=*I_GRID_SETTINGS=is_layout=wa_layoutit_fieldcat=gt_fieldcat[]*IT_EXCLUDING=*IT_SPECIAL_GROUPS=*IT_SORT=*IT_FILTER=*IS_SEL_HIDE=*I_DEFAULT='X'*I_SAVE=''*IS_VARIANT=*IT_EVENTS=*IT_EVENT_EXIT=*IS_PRINT=*IS_REPREP_ID=*I_SCREEN_START_COLUMN=0*I_SCREEN_START_LINE=0*I_SCREEN_END_COLUMN=0*I_SCREEN_END_LINE=0*I_HTML_HEIGHT_TOP=0*I_HTML_HEIGHT_END=0*IT_ALV_GRAPHICS=*IT_HYPERLINK=*IT_ADD_FIELDCAT=*IT_EXCEPT_QINFO=*IR_SALV_FULLSCREEN_ADAPTER=*IMPORTING*E_EXIT_CAUSED_BY_CALLER=*ES_EXIT_CAUSED_BY_USER=TABLESt_outtab=gt_outtabEXCEPTIONSprogram_error=1OTHERS=2.IFsy-subrc0.MESSAGEIDsy-msgidTYPEsy-msgtyNUMBERsy-msgnoWITHsy-msgv1sy-msgv2sy-msgv3sy-msgv4.ENDIF.
*-----------------------------------------------------------------------*Forms*-----------------------------------------------------------------------
*Initializationfieldcatalog
FORMfieldcat_initUSINGrt_fieldcatTYPEslis_t_fieldcat_alv.
DATA:ls_fieldcatTYPEslis_fieldcat_alv.
CALLFUNCTION'REUSE_ALV_FIELDCATALOG_MERGE'EXPORTINGi_program_name=g_repidi_internal_tabname='GT_OUTTAB'i_structure_name='YCONCERT'*I_CLIENT_NEVER_DISPLAY='X'*I_INCLNAME=G_REPID*I_BYPASSING_BUFFER=*I_BUFFER_ACTIVE=CHANGINGct_fieldcat=rt_fieldcatEXCEPTIONSinconsistent_interface=1program_error=2OTHERS=3.IFsy-subrc=0.
*SetCDATEatColumn1ls_fieldcat-col_pos=1.MODIFYrt_fieldcatFROMls_fieldcatTRANSPORTINGcol_posWHEREfieldname='CDATE'.
*SetARTISTatColumn2ls_fieldcat-col_pos=2.MODIFYrt_fieldcatFROMls_fieldcatTRANSPORTINGcol_posWHEREfieldname='ARTIST'.
*HideMAXSEATSintheOutputls_fieldcat-no_out='X'.MODIFYrt_fieldcatFROMls_fieldcatTRANSPORTINGno_outWHEREfieldname='MAXSEATS'.
ELSE.MESSAGEIDsy-msgidTYPEsy-msgtyNUMBERsy-msgnoWITHsy-msgv1sy-msgv2sy-msgv3sy-msgv4.ENDIF.
ENDFORM."FIELDCAT_INIT
*&---------------------------------------------------------------------**&FormSELECT_DATA*&---------------------------------------------------------------------**text*----------------------------------------------------------------------**-->RT_OUTTABtext*----------------------------------------------------------------------*FORMselect_dataCHANGINGrt_outtabTYPEty_outtab.
SELECTartist"ArtistNamecdate"ConcertDateoccupied"OccupiedSeatsmaxseats"MaximumSeatslocation"ConcertLocationprice"PriceofConcertTicketFROMyconcert"ConcertTableINTOTABLEgt_outtab.
ENDFORM."SELECT_DATAThe output should be displayed as follows:
Dec-2008Page 9