Manipulation of Table Control
Introduction
A table control is an area on the screen in which the system displays data in tabular form. It is processed using a loop.
Benefits of using a table control are
- Resizable table for displaying and editing data.
- The user or program can change the column width and position, save the
- changes, and reload them later.
- Check column for marking lines; marked lines are highlighted in a different color.
- Line selection: single lines, multiple lines, all lines, and deselection.
- Column headings double as pushbuttons for marking columns.
- Scrollbars for horizontal and vertical scrolling.
- Any number of key (leading) columns can be set.
Go to SE38 and type Program Name and click ‘CREATE’
Select type as ‘MODULE POOL’ and status as ‘TEST PROGRAM’ and click ‘SAVE’
Select Local Object ( You can also select your own package and save)
Click the icon (Display Object List) in the toolbar
Write the below coding (Defining Internal tables for table controls SOURCE and DESTINATION)
(Cont.)Define table controls SORUCE and DESTINATION to be created in the screen
Right Click the progam name and select Create->Screen
Provide screen number (Here 9000) and click ‘CONTINUE’
Provide Short Description and click ‘LAYOUT’ in the toolbar
Select ‘TABLE CONTROL’ icon in the toolbar
Tap on the screen to create table controls as shown below
Click ‘DICTIONARY/PROGRAM FIELDS’ in the toolbar
Enter the structure defined in the program and click ‘GET FROM PROGRAM’ and select all fields
Click on the table control to copy the structure
Provide the following attributes in the attribute panel for each table control
Create push buttons as shown below using ‘PUSH BUTTON’ icon in the toolbar and provide attributes for each element as following and Save the screen
Double click on screen 9000 and enter the below code in the flow logic tab
Double click on ‘STATUS_9000’ and select ‘YES’
Write the below code in the generated module block
(Cont.)
Add a code ‘MODULE modify_source’ and double click on it and select ‘YES’
Write the below code in the generated module block
Double click ‘USER_COMMAND_9000’ and select ‘YES’
Write the below code in the generated module block and activate program
(Cont.)
(Cont.)
(Cont.)
(Cont.)
(Cont.)
Right click on program name and select Create->Transaction
Provide Transaction Name and short description and select ‘CONTINUE’
Provide attributes shown below and save and execute the program
EXECUTE : Activate Program and Right click Program name and select
Execute->In a new window
OUTPUT :
Select All
Deselect All
Invert Selection
Delete Rows
Restore deleted rows
Move selected rows to destination table
Sort rows in ascending order
Sort rows in descending order
Complete Coding in text format
Program
*General Structure
TYPES : BEGIN OF LS,
MATNR TYPE MARA-MATNR,
ERNAM TYPE MARA-ERNAM,
MTART TYPE MARA-MTART,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
MARK,
END OF LS.*Work area and internal table for both table control with similiar data
DATA : LS_SOURCE TYPE LS,
LS_DESTINATION TYPE LS,
LT_SOURCE LIKE TABLE OF LS_SOURCE,
LT_DESTINATION LIKE TABLE OF LS_DESTINATION.*Internal table for restoration data
DATA : LT_RESTORE LIKE TABLE OF LS_SOURCE.*Table controls
CONTROLS : SOURCE TYPE TABLEVIEW USING SCREEN 9000,
DESTINATION TYPE TABLEVIEW USING SCREEN 9000.*Work area for table control columns
DATA : LS_SRC_COL LIKE LINE OF SOURCE-COLS,
LS_DST_COL LIKE LINE OF DESTINATION-COLS.DATA : FLAG, "To fill source table once
FIELD(20). "To store column name in table control
TYPES : BEGIN OF LS,
MATNR TYPE MARA-MATNR,
ERNAM TYPE MARA-ERNAM,
MTART TYPE MARA-MTART,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
MARK,
END OF LS.*Work area and internal table for both table control with similiar data
DATA : LS_SOURCE TYPE LS,
LS_DESTINATION TYPE LS,
LT_SOURCE LIKE TABLE OF LS_SOURCE,
LT_DESTINATION LIKE TABLE OF LS_DESTINATION.*Internal table for restoration data
DATA : LT_RESTORE LIKE TABLE OF LS_SOURCE.*Table controls
CONTROLS : SOURCE TYPE TABLEVIEW USING SCREEN 9000,
DESTINATION TYPE TABLEVIEW USING SCREEN 9000.*Work area for table control columns
DATA : LS_SRC_COL LIKE LINE OF SOURCE-COLS,
LS_DST_COL LIKE LINE OF DESTINATION-COLS.DATA : FLAG, "To fill source table once
FIELD(20). "To store column name in table control
Screen Flow Logic
PROCESS BEFORE OUTPUT.MODULE STATUS_9000.LOOP AT LT_SOURCE INTO LS_SOURCE WITH CONTROL SOURCE.ENDLOOP.LOOP AT LT_DESTINATION INTO LS_DESTINATION WITH CONTROL DESTINATION.ENDLOOP.
PROCESS AFTER INPUT.LOOP AT LT_SOURCE.MODULE MODIFY_SOURCE.ENDLOOP.LOOP AT LT_DESTINATION.ENDLOOP.MODULE USER_COMMAND_9000.
PROCESS AFTER INPUT.LOOP AT LT_SOURCE.MODULE MODIFY_SOURCE.ENDLOOP.LOOP AT LT_DESTINATION.ENDLOOP.MODULE USER_COMMAND_9000.
PBO (Module STATUS_9000 output)
module STATUS_9000 output.* SET PF-STATUS 'xxxxxxxx'.
* SET TITLEBAR 'xxx'.
*To fill table control(SOURCE) once
IF FLAG = ''.*Data selection for source table
SELECT * UP TO 5 ROWS
FROM MARA
INTO CORRESPONDING FIELDS OF TABLE LT_SOURCE.
FLAG = 'X'.ENDIF.*If internal table is not empty
IF NOT LINES( LT_SOURCE ) = 0.*Set no. of table control lines(SOURCE)
SOURCE-LINES = LINES( LT_SOURCE ).ELSE.*Disable table control lines
SOURCE-LINES = -1.ENDIF.*If internal table is not empty
IF NOT LINES( LT_DESTINATION ) = 0.*Set no. of table control lines(DESTINATION)
DESTINATION-LINES = LINES( LT_DESTINATION ).ELSE.*Disable table control lines
DESTINATION-LINES = -1.ENDIF."To set all columns in read only mode
LOOP AT DESTINATION-COLS INTO LS_DST_COL.
LS_DST_COL-SCREEN-INPUT = ''.MODIFY DESTINATION-COLS FROM LS_DST_COL.ENDLOOP."To set all columns in read only mode
LOOP AT SOURCE-COLS INTO LS_SRC_COL.
LS_SRC_COL-SCREEN-INPUT = ''.MODIFY SOURCE-COLS FROM LS_SRC_COL.ENDLOOP.endmodule. " STATUS_9000 OUTPUT
* SET TITLEBAR 'xxx'.
*To fill table control(SOURCE) once
IF FLAG = ''.*Data selection for source table
SELECT * UP TO 5 ROWS
FROM MARA
INTO CORRESPONDING FIELDS OF TABLE LT_SOURCE.
FLAG = 'X'.ENDIF.*If internal table is not empty
IF NOT LINES( LT_SOURCE ) = 0.*Set no. of table control lines(SOURCE)
SOURCE-LINES = LINES( LT_SOURCE ).ELSE.*Disable table control lines
SOURCE-LINES = -1.ENDIF.*If internal table is not empty
IF NOT LINES( LT_DESTINATION ) = 0.*Set no. of table control lines(DESTINATION)
DESTINATION-LINES = LINES( LT_DESTINATION ).ELSE.*Disable table control lines
DESTINATION-LINES = -1.ENDIF."To set all columns in read only mode
LOOP AT DESTINATION-COLS INTO LS_DST_COL.
LS_DST_COL-SCREEN-INPUT = ''.MODIFY DESTINATION-COLS FROM LS_DST_COL.ENDLOOP."To set all columns in read only mode
LOOP AT SOURCE-COLS INTO LS_SRC_COL.
LS_SRC_COL-SCREEN-INPUT = ''.MODIFY SOURCE-COLS FROM LS_SRC_COL.ENDLOOP.endmodule. " STATUS_9000 OUTPUT
PAI ( Module MODIFY_SOURCE input)
module MODIFY_SOURCE input."Updating changes from table control to internal table
MODIFY LT_SOURCE FROM LS_SOURCE INDEX SOURCE-CURRENT_LINE.endmodule. " MODIFY_SOURCE INPUT
MODIFY LT_SOURCE FROM LS_SOURCE INDEX SOURCE-CURRENT_LINE.endmodule. " MODIFY_SOURCE INPUT
PAI ( Module USER_COMMAND_9000 input)
module USER_COMMAND_9000 input."User command field
DATA OK_CODE TYPE SY-UCOMM.
OK_CODE = SY-UCOMM.CASE OK_CODE.
WHEN 'DELETE'.
"Retrieving record selected
LOOP AT LT_SOURCE INTO LS_SOURCE WHERE MARK = 'X'.
"Appending record to be deleted in restore table
APPEND LS_SOURCE TO LT_RESTORE.
"Deleting record to be deleted
DELETE TABLE LT_SOURCE FROM LS_SOURCE.
"Decrementing table control line by one
SOURCE-LINES = SOURCE-LINES - 1.
ENDLOOP.
DATA OK_CODE TYPE SY-UCOMM.
OK_CODE = SY-UCOMM.CASE OK_CODE.
WHEN 'DELETE'.
"Retrieving record selected
LOOP AT LT_SOURCE INTO LS_SOURCE WHERE MARK = 'X'.
"Appending record to be deleted in restore table
APPEND LS_SOURCE TO LT_RESTORE.
"Deleting record to be deleted
DELETE TABLE LT_SOURCE FROM LS_SOURCE.
"Decrementing table control line by one
SOURCE-LINES = SOURCE-LINES - 1.
ENDLOOP.
WHEN 'RESTORE'.
"Moving records from restore table to source table
APPEND LINES OF LT_RESTORE TO LT_SOURCE.
"Sorting source table in ascending order
SORT LT_SOURCE BY MATNR.
"Clearing contents in restore table
REFRESH LT_RESTORE.
WHEN 'MOVE'.
"Retrieving record selected
LOOP AT LT_SOURCE INTO LS_SOURCE WHERE MARK = 'X'.
"Making records being not selected
CLEAR LS_SOURCE-MARK.
"Moving record to destination table
APPEND LS_SOURCE TO LT_DESTINATION.
ENDLOOP.
"Sorting destination table in ascending order
SORT LT_DESTINATION.
"Deleting duplicate records
DELETE ADJACENT DUPLICATES FROM LT_DESTINATION COMPARING ALL FIELDS.
WHEN 'SELECT'.
"Retrieving record being not selected
LOOP AT LT_SOURCE INTO LS_SOURCE WHERE MARK <> 'X'.
"Making record being selected
LS_SOURCE-MARK = 'X'.
"Updating Changes to source table
MODIFY LT_SOURCE FROM LS_SOURCE TRANSPORTING MARK.
ENDLOOP.
WHEN 'DESELECT'.
"Retrieving record being selected
LOOP AT LT_SOURCE INTO LS_SOURCE WHERE MARK = 'X'.
"Making record being not selected
LS_SOURCE-MARK = ''.
"Updating Changes to source table
MODIFY LT_SOURCE FROM LS_SOURCE TRANSPORTING MARK.
ENDLOOP.
WHEN 'INVERT'.
"Retrieving record being selected
LOOP AT LT_SOURCE INTO LS_SOURCE WHERE MARK = 'X'.
"Setting mark value to temporary value other than 'X' or space( here 'Y')
LS_SOURCE-MARK = 'Y'.
MODIFY LT_SOURCE FROM LS_SOURCE TRANSPORTING MARK.
ENDLOOP.
"Retreiving record being not selected
LOOP AT LT_SOURCE INTO LS_SOURCE WHERE MARK = ''.
"Making record being selected
LS_SOURCE-MARK = 'X'.
MODIFY LT_SOURCE FROM LS_SOURCE TRANSPORTING MARK.
ENDLOOP.
"Retrieving record whose mark value has been set to temporary value( here 'Y')
LOOP AT LT_SOURCE INTO LS_SOURCE WHERE MARK = 'Y'.
"Making record being not selected
LS_SOURCE-MARK = ''.
MODIFY LT_SOURCE FROM LS_SOURCE TRANSPORTING MARK.
ENDLOOP.
WHEN 'SORT_A'.
"Retrieving column being selected
READ TABLE SOURCE-COLS INTO LS_SRC_COL WITH KEY SELECTED = 'X'.
"Assigning column name to variable
FIELD = LS_SRC_COL-SCREEN-NAME+10(10).
IF SY-SUBRC = 0.
"Sorting source table in ascending order if any column selected
SORT LT_SOURCE BY (FIELD).
ENDIF.
WHEN 'SORT_D'.
"Retrieving column being selected
READ TABLE SOURCE-COLS INTO LS_SRC_COL WITH KEY SELECTED = 'X'.
"Assigning column name to variable
FIELD = LS_SRC_COL-SCREEN-NAME+10(10).
IF SY-SUBRC = 0.
"Sorting source table in descending order if any column selected
SORT LT_SOURCE BY (FIELD) DESCENDING.
ENDIF.
WHEN 'EXIT'.
"Leaving program
LEAVE PROGRAM.
ENDCASE.endmodule. " USER_COMMAND_9000 INPUT
By Karthick.R, Kaavian Systems Pvt Ltd.
Check out http://www.sapdev.co.uk/dialog/tabcontrol/tc_basic.htm for simple step by step instructions on how to create a dynpro table control
ReplyDeleteGood Example for the beginners with all the possible actions on the table control.
ReplyDeleteThanks...........
Well done!
ReplyDeleteVery Nice sir, can u provide a tutorial to change the field name and record at run time ?
ReplyDeleteThanks & regards
Prashant N.
Hi Prashant,
DeletePlease find below link for solution...
http://theabap.blogspot.in/2013/06/changing-label-of-push-button.html
Very good tutorial. Could have been best if you would have added insert/create/modify table control records fields too. But still very well explained.
ReplyDeletesooooper very useful for beginners.....
ReplyDeletethanks very much
ReplyDeleteHow to select column in table control.Pls help.I am not able to make selected = 'X'.
ReplyDeleteGreat post. Keep up the good work!!!
ReplyDeleteVisit our SAP-ABAP , OO-ABAP, HANA tutorial
abe pdhai likhai me dhyan do .
ReplyDeleteABAP sikho
This comment has been removed by the author.
ReplyDeleteMy troubles with this program :
ReplyDeleteScrolling up using mouse is impossible for 5 or 50 records (scrolling down ok)
Buttons events are not handled (except of Exit, Deselect)
Uh, i solved it in 20 minutes))
ReplyDelete1. Add Ok_code to the list of screen fields (it was not obviously for an abap-newbie like me )
2. In the list of screen fields manually set 'X' for fields, and scrolling would be ok.
3. Add something like:
set cursor field 'LT_SOURSE-ERNAM' line 1.
To the status 9000.