Table Controls vs Step Loops
TABLE CONTROLS
TABLE CONTROLS are simply enhanced STEP LOOPS that display data with the look and feel of a table widget in a desktop
application. TABLE CONTROLS are always single lines, but can be very long. ( Table control rows are scrollable ). The structure of table controls is different from step loops.
STEP LOOP
The STEP LOOP are the predecessor of TABLE CONTROL they are used to display tabular data on the screen. They are repeated sequence of blocks of screen element. In a step loop number of screen elements are combined together to form a loop block. A step loop, as a screen object, is simply a series of field rows that appear as a repeating block.
Related: LOOP AT with WHERE Clause
There are 2 types of step loops:
1) Fixed Size
2) Variable Size..
In a fixed size loop the number of loop blocks shown in the screen is fixed,while in case of variable size step loop the number of blocks will change dynamically according to the size of the screen.There could be only one variable step loop per screen and unlimited fixed size step loops per screen.
A step loop can extend more than one line on the screen(see Table Control).A vertical scroll bar is automatically created to show step loops on he screen.
Step loops have no name. We use LOOP ...ENDLOOP to program step loops in a screen in both PBO and PAI.
Since the number of loop blocks in variable step loops can change the number of loop blocks at any moment is placed by the system in system field SY-LOOPC and the current step loop pass number is placed in system field SY-STEPL .
Loop Type attribute is used to specify the type of step loop and Loop Count attribute is used to specify the number of step loop blocks that will be displayed on the screen at a time.
Step Loop Screen Creation
We create step loop in the screen painter(SE51). First we define the screen elements that will be part of the step loop on the screen ,they may extend to more than one line.Select all the elements as one group.Goto Edit menu and select Grouping-->Step Loop-->Define.
To define a step loop as variable or fixed.goto Edit-->Grouping-->Step Loops-->Fix or Variable.
To edit the Step Loop click on the border of the block and goto Edit-->Grouping-->Step Loop here we can use define/undefine(delete)variable fix options.
Once created we have to program step loops through screen key word LOOP...ENDLOOP in PBO and PAI as these events are used to transfer back and forth the data from the ABAP program.
STEP LOOP Coding
We use two flavours of LOOP ... ENDLOOP in screen flow logic to program the step loops.We have to program both in PBO and PAI so that transfer of data can take place between screen and ABAP program.
1) LOOP MODULE fill_data ENDLOOP.
Here in PBO a module should be called that will transfer the data to the screen fields. In PAI the module call is not required only the empty LOOP..ENDLOOP will do or we can call a module to write the data to an internal table.In this method there is no automatic scrolling we have to program it in ABAP.
2) LOOP AT int_table [INTO wa ][CURSOR line_number][FROM n1 TO n2] ENDLOOP.
Here in PBO a module call is not required to fill the step loop screen fields as the data is copied to the workare wa and from there to screen fields in step loop automatically. INTO wa is not required if we use the int_table declared with a header line.
In PAI the addition AT int_table is also required for automatic scrolling.
The parameter CURSOR line_number which is of TYPE I is used to specify
That will be the first to be displayed,it is filled in the ABAP program.
But from a programming point of view, TABLE CONTROLS and STEP LOOPS are almost exactly the same.
NOTE:
1) It is preferable to use TABLE CONTROL instead of STEP LOOPS.
2) It is preferable to use LOOP AT int_table instead of plain LOOP..ENDLOOP.
Steploop and table contol is inevitable in certain transactions. When we run BDC for such transactions, we will face the situation: how many visible lines of steploop/tablecontrol are on the screen? Although we can always find certain method to deal with it, such as function code 'NP', 'POPO', considering some extreme situation: there is only one line visible one the screen, our BDC program should display an error message. (See transaction 'ME21', we you resize your screen to let only one row visible, you can not enter mutiple lines on this screen even you use 'NP')
I find a method with which we can determine the number of visible lines on Transaction Screen from our Calling BDC program. Maybe it is useless to you, but I think it will give your some idea.
Demo ABAP code has two purposes:
1. how to determine number of visible lines and how to calculate page number; (the 'calpage' routine has been modify to meet general purpose usage)
2. using field symbol in BDC program, please pay special attention to the difference in Static ASSIGN and Dynamic ASSIGN.
Now I begin to describe the step to implement my method:
(I use transaction 'ME21', screen 121 for sample, the method using is Call Transation Using..)
Step1: go to screen painter to display the screen 121, then we can count the fixed line on this screen, there is 7 lines above the steploop and 2 lines below the steploop, so there are total 9 fixed lines on this screen. This means except these 9 lines, all the other line is for step loop. Then have a look at steploop itselp, one entry of it will occupy two lines.
(Be careful, for table control, the head and the bottom scroll bar will possess another two fixed lines, and there is a maximum number for table line)
Now we have : FixedLine = 9
LoopLine = 2(for table control, LoopLine is always equal to 1)
Step2: go to transaction itself(ME21) to see how it roll page, in ME21, the first line of new page is always occupied by the last line of last page, so it begin with index '02', but in some other case, fisrt line is empty and ready for input.
Now we have: FirstLine = 0
or FirstLine = 1 ( in our case, FirstLine is 1 because the first line of new page is fulfilled)
Step3: write a subroutine calcalculating number of pages (here, the name of actual parameter is the same as formal parameter)
global data:
FixedLine type i, " number of fixed line on a certain screen
LoopLine type i, " the number of lines occupied by one steploop item
FirstLine type i, " possbile value 0 or 1, 0 stand for the first line of new " scrolling screen is empty, otherwise is 1
Dataline type i, " number of items you will use in BDC, using DESCRIBE to get
pageno type i, " you need to scroll screen how many times.
line type i, " number of lines appears on the screen.
index(2) type N, " the screen index for certain item
begin type i, " from parameter of loop
end type i. " to parameter of loop
*in code sample, the DataTable-linindex stands for the table index number of this line
form calpage using FixedLine type i (see step 1)
LoopLine type i (see step 1)
FirstLine type i (see step 2)
DataLine type i ( this is the item number you will enter in transaction)
changing pageno type i (return the number of page, depends on run-time visible line in table control/ Step Loop)
changing line type i.(visible lines one the screen)
data: midd type i,
vline type i, "visible lines
if DataLine eq 0.
Message eXXX.
endif.
vline = ( sy-srows - FixedLine ) div LoopLine.
*for table control, you should compare vline with maximum line of
*table control, then take the small one that is min(vline, maximum)
*here only illustrate step loop
if FirstLine eq 0.
pageno = DataLine div vline.
if pageno eq 0.
pageno = pageno + 1.
endif.
elseif FirstLine eq 1.
pageno = ( DataLine - 1 ) div ( vline - 1 ) + 1.
midd = ( DataLine - 1 ) mod ( vline - 1).
if midd = 0 and DataLine gt 1.
pageno = pageno - 1.
endif.
endif.
line = vline.
endform.
Step4: write a subroutine to calculate the line index for each item.
form calindex using Line type i (visible lines on the screen)
FirstLine type i(see step 2)
LineIndex type i(item index)
changing Index type n. (index on the screen)
if FirstLine = 0.
index = LineIndex mod Line.
if index = '00'.
index = Line.
endif.
elseif FirstLine = 1.
index = LineIndex mod ( Line - 1 ).
if ( index between 1 and 0 ) and LineIndex gt 1.
index = index + Line - 1.
endif.
if Line = 2.
index = index + Line - 1.
endif.
endif.
endform.
Step5: write a subroutine to calculate the loop range.
form calrange using Line type i ( visible lines on the screen)
DataLine type i
FirstLine type i
loopindex like sy-index
changing begin type i
end type i.
If FirstLine = 0.
if loopindex = 1.
begin = 1.
if DataLine < = Line.
end = DataLine.
else.
end = Line.
endif.
elseif loopindex gt 1.
begin = Line * ( loopindex - 1 ) + 1.
end = Line * loopindex.
if end gt DataLine.
end = DataLine.
endif.
endif.
elseif FirstLine = 1.
if loopindex = 1.
begin = 1.
if DataLine <= Line.
end = DataLine.
else.
end = Line.
endif.
elseif loop index gt 1.
begin = ( Line - 1 ) * ( loopindex - 1 ) + 2.
end = ( Line - 1 ) * ( loopindex - 1 ) + Line.
if end gt DataLine.
end = DataLine.
endif.
endif.
endif.
endform.
Step6: Using field sysbol in your BDC, for example: in ME21, but you should calculate each item will correponding to which index in steploop/Table Control
form creat_bdc.
field-symbols: , , .
data: name1(14) value 'EKPO-EMATN(XX)',
name2(14) value 'EKPO-MENGE(XX)',
name3(15) value 'RM06E-SELKZ(XX)'.
assign: name1 to ,
name2 to ,
name3 to . .
do pageno times.
if sy-index gt 1
*insert scroll page ok_code"
endif .
perform calrange using Line DataLine FirstLine sy-index
changing begin end. .
loop at DataTable from begin to end.
perform calindex using Line FirstLine DataTable-LineIndex changing Index.
name1+11(2) = Index.
name2+11(2) = Index.
name3+12(2) = Index.
perform bdcfield using DataTable-matnr.
perform bdcfield using DataTable-menge.
perform bdcfield using DataTable-indicator. .
endloop.
enddo.
Table controls have both horizontal and vertical scrollers and cursor control logic is designed implicitly.
Step loops have only horizontal scrollers and cursor control logic is to be designed by the user explicitly.