CreateTableControlUsingABAPProgramming.docx
《CreateTableControlUsingABAPProgramming.docx》由会员分享,可在线阅读,更多相关《CreateTableControlUsingABAPProgramming.docx(12页珍藏版)》请在冰豆网上搜索。
CreateTableControlUsingABAPProgramming
CreateTableControlUsingABAPProgramming
CreateTableControlUsingABAPProgramming
Inlasttwoweeks,Ihavelearnedself-taughthowtocreate tablecontrol in SAPScreenusing ABAP4.Aftertrialanderror,finallyIhavefinishedmysmallproject.Whilelearningabouttablecontrol,Iuse SAPHelp asmainreference,somewebsitesandpresentationslidesfromMichaelAdams,QueenslandUniversityofTechnology.
ATableControlisascreen‘container’orobjectthatallowsdisplayofmultiplerowsofatable(database or internaltable)onascreen.Itisgoodfordisplayingaone-to-manyrelationshipontheonescreen.Thetablecontrolobjecthasmanyfeaturesbuiltintoenhancescreendisplayandusability.
TableControlfeaturesallow
∙horizontal&verticalscrolling
∙columnwidthresizing
∙scrollingwithinafield(wherecontentsarewiderthanthescreenfieldwidth)
∙reorderingthesequenceofcolumns
∙savingthecurrentdisplaysettingsforfutureuse
∙selectionoftablerows&columns
Formattingfeaturesinclude
∙automatictableresizingonwindowresize
∙separatorlinesbetweenrows&columns
∙columnheaderfieldsforallcolumns
Herestepstocreatetablecontrol
1.Declarethetablecontrol
Aswellasdrawingthetablecontrolonthescreenitisnecessarytodeclarethetablecontrolasadataitem(intheTOPincludeprogramforthetransaction).
CONTROLSctrlTYPETABLEVIEW USINGSCREENscr.
∙isthenameofthetablecontrolonascreenintheABAPprogram
∙correspondstoacomplextype– (CXTAB_CONTROLdefinedintheABAPdictionary)
∙isthescreenfromwhichthetablecontrolwillgetitsinitialvalues
2.Addingtablecontroltoascreen
Inthegraphicalscreeneditorchoosethetablecontrolelementbutton. Usetheleftmousebuttontopositionandsizethecontrolonthescreen.
Theninputthenameoftablecontrol.
3.AddingFieldtoatablecontrol
Toaddfieldtotablecontrol,wecanretrievefromtableorinternaltable.Clickonicon“dictionary/programfieldwindow”orfunctionkeyF6.
Theretwooptionwhileretrievefield,i.e.basedondatabasetableorinternaltable.Ifwanttoretrievefromdatabasetable,inputthenameoftablethenclickpushbutton“GetfromDictionary”.Ifwanttoretrievefrominternaltable,inputtheinternaltablenamethenclickpushbutton“Getfromprogram”.
Markthefieldtobeaddedtotablecontrol,andthenclickonpushbuttonOK.
Dragselectedfieldsintotablethenreleasethemousebutton.
Herethefieldsweselectedwillbedisplayedinreversedorder.Idonotexactlywhyithappens.Ihavetriedsomewaysandtrickstodisplayincorrectorder,butthefieldsstilldisplayedinreversedorder.Finally,togetthecorrectorderIselectedthefieldsonebyone.
4.Addinglabelforeachcolumn
Labelcolumnistextfield.Toaddit,justclickonthetextfieldicon,dragitontoheaderofthecolumnandthentypethelabel.
TableControlPrinciple
Thereareasetofprogrammingprinciplesthatshouldbeadheredtowhenusingtablecontrolsandsteploops.Datafromthedatabaseshouldbeinitiallyloadedintoaninternaltable.Thismeansthatthedatabaseisaccessedforreadpurposesonlyonceinthetransaction.Nexttherowsoftheinternaltableareloadedintothetablecontrol.Anychangesthataremadetothedataarethensavedbacktotheinternaltable.Attheendofthetransaction,thecontentsoftheinternaltablecanbewrittenbacktothedatabase,againtominimizedatabaseI/O.
PAIlogicforscreen1(seescreenbelow)loadstheinternaltablewithdatafromthedatabaseaccordingtotheentriessuppliedbytheuser.
PBOlogicforscreen2(seescreenbelow)populatesthetablecontrolfromtheinternaltable(buffer).
Useractioninscreen2triggersthePAIlogic.PAIlogicupdatestheinternaltablewithnewvaluesentered(intothetablecontrolscreenfields)bytheuser.
PAIlogicistriggeredbyactionssuchasscrollingdownasinglerowaswellasactionssuchasBACK,EXIT,etc.
Unlesstheuseractioncausesthetransactiontoleavethecurrentscreen,afterthePAImoduleshavebeenexecuted,thePBOmodulesforthescreenareexecutedagain.Thusthetablecontrolfieldsareupdatedorrefreshedaftereveryuseraction.
PBO(ProcessBeforeOutput)
InPBOprocessingfieldsaretransportedfromthemodulepooltothescreeninapredefinedorder.
∙Thetablecontrolsteploopisprocessedrowbyrow.Fieldswithcorrespondingnamesaretransportedfromthemodulepooltothescreen.
∙Afterthesteploophasbeenprocessedallremainingmodulepoolfieldsaretransportedtothescreen.
PAI(ProcessAfterInput)
∙AllscreenfieldsthatdonotbelongtoatablecontrolandarenotspecifiedinaFIELDstatementaretransportedtomodulepoolfields
∙Tablecontrolfieldsaretransportedrowbyrowtomodulepoolfields
∙FieldsspecifiedinFIELDstatementsaretransportedimmediatelybeforetheFIELDstatementisexecuted
Updatingdataintablecontrol
TheABAPlanguageprovidestwomechanismsforloadingthetablecontrolwithdatafromtheinternaltableandthenstoringthealteredrowsofthetablecontrolbacktotheinternaltable.
1.Method1:
ReadtheinternaltableintotheTableControlinthescreen’sflowlogic. UsedwhenthenamesoftheTableControlfieldsarebasedonfieldsoftheinternaltable.
2.Method2:
ReadtheinternaltableintotheTableControlinthemodulepoolcode.UsedwhenthenamesoftheTableControlfieldsarebasedonfieldsofthedatabasetable.
Method1(tablecontrolfields=itabfields)
IntheflowlogicwecanreadaninternaltableusingtheLOOPstatement.DefinethereferencetotherelevantablecontrolbyspecifyingWITHCONTROL
DeterminewhichtableentryistobereadbyspecifyingCURSOR-CURRENT_LINE.
Afterthereadoperationthefieldcontentsareplacedintheheaderlineoftheinternaltable.Ifthefieldsinthetablecontrolhavethesamenameastheinternaltheywillbefilledautomatically.Otherwiseweneedtowriteamoduletotransfertheinternaltablefieldstothescreenfields.
WemustreflectanychangestheusermakestothefieldsofthetablecontrolintheinternaltableotherwisetheywillnotappearwhenthescreenisredisplayedafterPBOprocessing,(eg,aftertheuserpressesEnterorscrolls)However,thisprocessingshouldbeperformedonlyifchangeshaveactuallybeenmadetothescreenfieldsofthetablecontrol(hencetheuseoftheONREQUEST)
PROCESSBEFOREOUTPUT.
LOOPATITAB_REGWITHCONTROLTCREG
CURSORTCREG-CURRENT_LINE.
ENDLOOP.
PROCESSAFTERINPUT.
LOOPATITAB_REG.
MODULEMODIFY_ITAB_REG.
ENDLOOP.
MODULEMODIFY_ITAB_REGINPUT.
MODIFYITAB_REGINDEXTCREG-CURRENT_LINE.
ENDMODULE.
Method2(tablecontrolfields=dict.fields)
IfusingaLOOPstatementwithoutaninternaltableintheflowlogic,wemustreadthedatainaPBOmodulewhichiscalledeachtimetheloopisprocessed.
Since,inthiscase,thesystemcannotdeterminethenumberofinternaltableentriesitself,wemustusetheEXITFROMSTEP-LOOPstatementtoensurethatnoblanklinesaredisplayedinthetablecontroliftherearenomorecorrespondingentriesintheinternaltable.
PROCESSBEFOREOUTPUT.
LOOPWITHCONTROLTCREG.
MODULEREAD_ITAB_REG.
ENDLOOP.
PROCESSAFTERINPUT.
LOOPWITHCONTROLTCREG.
CHAIN.
FIELD:
ITAB_REG-REG,
ITAB_REG-DESC.
MODULEMODIFY_ITAB_REG
ONCHAIN-REQUEST.
ENDCHAIN.
ENDLOOP.
MODULEREAD_ITAB_REGOUTPUT.
READTABLEITAB_REGINDEXTCREG-CURRENT_LINE.
IFSY-SUBRCEQ0.
MOVE-CORRESPONDINGITAB_REREGTOTCREG.
ELSE.
EXITFROMSTEP-LOOP.
ENDIF.
ENDMODULE.
MODULEMODIFY_ITAB_REGINPUT.
MOVE-CORRESPONDINGTCREGTOITAB_REG.
MODIFYITAB_REGINDEX
TCREG-CURRENT_LINE.
ENDMODULE.
Updatingtheinternaltable
Method1
PROCESSAFTERINPUT.
LOOPATITAB_REG.
CHAIN.
FIELD:
ITAB_REG-REG,
ITAB_REG-DESC.
MODULEMODIFY_ITAB_REGONCHAIN-REQUEST.
ENDCHAIN.
ENDLOOP.
MODULEMODIFY_ITAB_REGINPUT.
ITAB_REG-MARK=‘X’.
MODIFYITAB_REGINDEXTCREG-CURRENT_LINE.
ENDMODULE.
Method2
PROCESSAFTERINPUT.
LOOPWITHCONTROLTCREG.
CHAIN.
FIELD:
TCREG-REG,
TCREG-DESC.
MODULEMODIFY_ITAB_REGONCHAIN-REQUEST.
ENDCHAIN.
ENDLOOP.
MODULEMODIFY_ITAB_REGINPUT.
MOVE-CORRESPONDINGTCREGTOITAB_REG.
ITAB_REG-MARK=‘X’.
MODIFYITAB_REGINDEXTCREG-CURRENT_LINE.
ENDMODULE.
Updatingthedatabase
MODULEUSER_COMMAND_100.
CASEOK_CODE.
WHEN‘SAVE’.
LOOPATITAB-REG.
CHECKITAB_REG-MARK=‘X’.
MOVE-CORRESPONDINGITAB_REGTOTCREG.
UPDATETCREG.
ENDLOOP.
WHEN…
…
ENDCASE.
ENDMODULE.
Thosearethesimplestepshowtocreatetablecontrol.WhatIhavelearnedthisweekonlythebeginning.ActuallytherearemoreareasinSAPTableControlwithabapprogrammingthatcanbeexploreddeeper,butmaybenexttime.