Dear All,
This is my first Blog Post. I have seen lot of discussions in SCN related to issues regarding table control. This post is for beginners like me.
And this post is based on my experience with table control.
1.Working principle of Table Control
For example consider a table control say TBC, internal table ITAB(with 10 records,fields MATNR,ERSDA,ERNAM and LAEDA) and work area WA.
But only 3 fields i am using in table control, LAEDA is not putted in TBC.
Table Control Attributes..
This table control can show 6 records at a time.(Size depends on design in screen painter).
To work a table control it required two loop statements in screen flow logic, one in PBO and another in PAI.
The loop in PBO is to put data from internal table to table control. Which take care of displaying data in TBC.
As per my table control this loop is going to loop maximum of 6 times, means the further looping is unnecessary(can't display the rest of record(max is 6)).
Then screen displayed.
Things to take care....
1.Since screen just got appeared, we are seeing records(1-6) from internal table.
2.Similarly the property of table control top_line(index of internal table record, displayed as the first record in table control) will be equal to 1.
3.System variable sy-loopc will be equal to 6(this variable's value get only inside any of the two loop), this is equal to number of records shown in TBC.
4.TBC'slinesproperty denotes number of records enabled(total records able to view), it will be equal to records in ITAB.
Note :If we set TBC-lines = 2, we can see only two records in table control.
Note :In case any screen modification required in table control, that will do inside the loop in PBO using loop at screen. This modification is applicable only for the current processing record in loop.
Note :Scrolling is an event in case of table control. This is because, while scrolling TBC has to display another set of records, for this screen has to refresh.
At the same time at PAI we will capture all modifications.
Now at PAI..
Loop in PAI is to capture any data changes made by user through table control.
Before entering in to this loop.....
1.WA will hold the data of 6th(last record in TBC) record. Because this is where the loop in PBO stopped.
But this work area only hold value for field LAEDA(since it is not in TBC).
Note :If u wrote only LOOP. ENDLOOP. in PBO, during modifying internal table you will get wrong data(value for field LAEDA = 6th record's).
In order to solve this issue, simply use LOOP AT ITAB. ENDLOOP.
Inside loop ...
Note :Do not do anything rather than modify inside the loop in PAI.
2.This loop iterate only through the records that we saw before coming to PAI.
In our case this is 1 to 6(records we were saw).
Can you guess why SAP made it so, rather than looping through all records in ITAB?
The reason is, this loop is to capture data changes, anyhow the user can edit only the records that we watching.
Note :This is the place where you should write a modify statement to modify the internal table records one by one with changes.
To identify the index of record in internal table TBC provided us a property, that is current_line. In other way, it is the index of record that we are currently processing.
3.If you want to find the selected column(s), check cols.
Issues and Symptoms
1. Unable to scroll.
Make sure that you are not manually handling top_line and not refreshing TBC.
Set tbc-lines equal to lines in internal table. Can use, DESCRIBE TABLE ITAB LINES TBC-LINES.
2. Data is not displaying.
Make sure that all fields are named using the work area used in PBO loop.
3. Loosing modified data while scrolling or press enter button.
You have to write MODIFY ITAB FROM WA INDEX TBC-CURRENT_LINE inside loop in PAI.
4.Unable to add new line through table control into internal table.
In this case you have to have a APPEND statement in case MODIFY statement fails.
MODIFY ITAB FROM WA INDEX TBC-CURRENT_LINE.
if sy-subrc <> 0.
APPEND WA TO ITAB.
endif.
5. Table control not showing all records even scrolled down.
DESCRIBE TABLE ITAB LINES TBC-LINES.
6. Not able to capture Line selection.
Verify field name provided at screen painter for sel field.
7.Line selection disappears when scrolling or Press enter.
Use Modify statement at PAI loop.
Few Tricks
1. Find operation in table control
Read ITAB using 'with key'.
If sy-subrc = 0.
TBC-top_line = sy-tabix. "Bring the record to top of table control.
WA-SEL = 'X'. "Set the record as selected
Modify ITAB from WA index sy-tabix."Set the record as selected, final step
endif.
2. Sort Operation
We can do sorting based on selected column.
To get selected column use TBC-cols.
From here we will get the column index.
Identify the respective column using index.
Now sort ITAB by column name.
3. Enable a new line in table control.
TBC-lines = TBC-lines + 1.
This is helpful in case of adding new records through table control.