Abap Module Ppt
Transcript of Abap Module Ppt
ITB258ABAP Programming
Lecture 11
Using Tables in Transactions
Objectives
• This lecture aims to introduce the concepts associated with: – Table controls: graphical screen elements used
for displaying more than n rows of a table on a single screen
– screen flow logic for handling table controls
Using Tables in a Screen
• ABAP offers the Table Control graphical element as a mechanism for displaying and modifying tabular data in a screen
• a Table Control is a screen ‘container’ or object that allows display of multiple rows of a table (database or internal) on a screen
• Good for displaying a one-to-many relationship on the one screen.
Table Control
Table Controls• Table Control features allow
– horizontal & vertical scrolling– column width resizing– scrolling within a field (where contents are wider
than the screen field width)– reordering the sequence of columns– saving the current display settings for future use– selection of table rows & columns
• Formatting features include– automatic table resizing on window resize– separator lines between rows & columns– column header fields for all columns
Creating a Table Control
• In the TOP Include object:
CONTROLS ctrl TYPE TABLEVIEW
USING SCREEN scr.
• TABLEVIEW corresponds to a complex type– (CXTAB_CONTROL defined in the ABAP dictionary)
• USING SCREEN determines the screen from which the table control will get its initial values
CXTAB_CONTROL
Adding a Table Control to a Screen
In the Graphical Fullscreen Editor choose the Table Control element button.
Use the left mouse button to position and size the control on the screen.
Adding a Table Control to a Screen
Enter a Name for the Table Control
Press Dict/program fields pushbutton
Adding Fields to a Table Control
Adding Fields to a Table Control
Enter database table name
Select Get From Dictionary
Adding Fields to a Table Control
Enter internal table name
Select Get From program
Adding Fields to a Table Control
Mark the fields to be added to the table control
Then click on Copy to select the fields
Adding Fields to a Table Control
Drag the selectedfields into the table control, then release the mouse button.
Adding Fields to a Table Control
The label for each column is a textfield, created by clicking on thetext field icon and then dragging the new text field onto the header of the column before typing in the label
Text field icon
Table Control Principles
Database Table
Internal TableInternal Table(Buffer)(Buffer)
1. Internal Table filled with selected data
Table Control Principles
Internal TableInternal Table(Buffer)(Buffer)
2. Table Control contents filled from internal table
TableTableControlControl
Table Control Principles
Internal TableInternal Table(Buffer)(Buffer)
TableTableControlControl
3. Changes to Table Control contents copied to internal table
Table Control Principles
Database Table
Internal TableInternal Table(Buffer)(Buffer)
4. When screen is closed, internal table contents copied back to database
Table Control Principles
PBO
PAI
Select rows fromSelect rows fromthe database intothe database intoan internal tablean internal table
Screen 1
PBO
PAI
Screen 2 (with table control)
LOOP ...LOOP ... read internal table into read internal table into table control line by linetable control line by lineENDLOOP.ENDLOOP.
LOOP ...LOOP ... update internal table fromupdate internal table from table control line by linetable control line by lineENDLOOP.ENDLOOP.
Table Control Principles
• Fields are transported from the module pool to the screen each time a loop is processed
• The remaining screen fields are transported at the end of PBO processing
LOOP start
LOOP end
transport ABAP table control fieldsto screen fields
transport remainingABAP fieldsto screen fields
PROCESS BEFORE OUTPUT
Table Control Principles
• All screen fields that do not belong to a table control and are not specified in a FIELD statement are transported to module pool fields
• Table control fields are transported row by row to module pool fields
• Fields specified in FIELD statements are transported immediately before the FIELD statement is executed
LOOP start
LOOP end
transport screen tablecontrol fields to ABAP module pool fields
transport all fields fromthe screen to ABAP fieldsexcluding table control fields
PROCESS AFTER INPUT
Updating Data in Table Controls
• Method 1: Read the internal table into the Table Control in the screen's flow logic. – Used when the names of the Table Control fields are
based on fields of the internal table.
• Method 2: Read the internal table into the Table Control in the module pool code. – Used when the names of the Table Control fields are
based on fields of the database table.
Table Controls in the Flow Logic
PROCESS BEFORE OUTPUT.LOOP AT ITAB WITH CONTROL FLIGHTS
CURSOR FLIGHTS-CURRENT_LINE.ENDLOOP.
PROCESS AFTER INPUT.LOOP AT ITAB. MODULE MODIFY_ITAB. ENDLOOP.
MODULE MODIFY_ITAB INPUT. MODIFY ITAB INDEX FLIGHTS-CURRENT_LINE.ENDMODULE.
• Method 1 (table control fields = itab fields)
Table Controls in the Flow Logic• Method 2 (table control fields = dict. fields)
PROCESS BEFORE OUTPUT.LOOP WITH CONTROL FLIGHTS. MODULE READ_ITAB.ENDLOOP.
PROCESS AFTER INPUT.LOOP WITH CONTROL FLIGHTS. MODULE MODIFY_ITAB. ENDLOOP.
MODULE READ_ITAB OUTPUT. READ TABLE ITAB INDEX FLIGHTS-CURRENT_LINE. IF SY-SUBRC EQ 0. MOVE-CORRESPONDING ITAB TO SFLIGHT. ELSE. EXIT FROM STEP-LOOP. ENDIF.ENDMODULE.
MODULE MODIFY_ITAB INPUT. MOVE-CORRESPONDING SFLIGHT TO ITAB. MODIFY ITAB INDEX FLIGHTS-CURRENT_LINE.ENDMODULE.
Table Controls in the Flow Logic
PROCESS BEFORE OUTPUT.LOOP WITH CONTROL FLIGHTS. MODULE READ_ITAB.ENDLOOP.
PROCESS AFTER INPUT.LOOP WITH CONTROL FLIGHTS. CHAIN. FIELD: SFLIGHT-PRICE, SFLIGHT-FLDATE. MODULE MODIFY_ITAB ON CHAIN-REQUEST. ENDCHAIN.ENDLOOP.
MODULE READ_ITAB OUTPUT. READ TABLE ITAB INDEX FLIGHTS-CURRENT_LINE. IF SY-SUBRC EQ 0. MOVE-CORRESPONDING ITAB TO SFLIGHT. ELSE. EXIT FROM STEP-LOOP. ENDIF.ENDMODULE.
MODULE MODIFY_ITAB INPUT. MOVE-CORRESPONDING SFLIGHT TO ITAB. MODIFY ITAB INDEX FLIGHTS-CURRENT_LINE.ENDMODULE.
• Method 2 (table control fields = dict. fields)
Table Control Attributesdata: begin of cxtab_control,
...
cols type cxtab_column occurs 10,
end of cxtab_control.
data: begin of cxtab_column,
...
selected,
screen like screen,
end of cxtab_column.
SCREEN FIELDS:
name
length
active
...
Sorting a Table Control by a Selected Field
DATA: WA LIKE LINE OF FLIGHTS-COLS....
MODULE USER_COMMAND_200....LOOP AT FLIGHTS-COLS INTO WA. IF WA-SELECTED = ‘X’.
SORT ITAB BY (WA-SCREEN-NAME+5).
ENDIF.ENDLOOP.
ENDMODULE.
Sample Fields List
Output only fieldsOutput only fields
Table ControlTable Control
Handling Line Selection
PROCESS AFTER INPUT.LOOP AT ITAB.
FIELD ITAB-MARK MODULE MODIFY_ITAB ON REQUEST.
ENDLOOP.
MODULE MODIFY_ITAB INPUT. MODIFY ITAB INDEX FLIGHTS-CURRENT_LINE.ENDMODULE.
Updating the Internal Table
PROCESS AFTER INPUT.LOOP WITH CONTROL FLIGHTS. CHAIN. FIELD: SFLIGHT-PRICE, SFLIGHT-FLDATE. MODULE MODIFY_ITAB ON CHAIN-REQUEST. ENDCHAIN.ENDLOOP.
Method 1 Method 2
PROCESS AFTER INPUT.LOOP AT ITAB. CHAIN. FIELD: SFLIGHT-PRICE, SFLIGHT-FLDATE. MODULE MODIFY_ITAB ON CHAIN-REQUEST. ENDCHAIN.ENDLOOP.
MODULE MODIFY_ITAB INPUT. ITAB-MARK = 'X'. MODIFY ITAB INDEX FLIGHTS-CURRENT_LINE.ENDMODULE.
MODULE MODIFY_ITAB INPUT. MOVE-CORRESPONDING SFLIGHT TO ITAB. ITAB-MARK = 'X'. MODIFY ITAB INDEX FLIGHTS-CURRENT_LINE.ENDMODULE.
Updating the DatabaseSaving the Updated Data (both methods)
MODULE USER_COMMAND_200.
CASE OK_CODE.
WHEN ‘SAVE’.
LOOP AT ITAB.
CHECK ITAB-MARK = ‘X’.
MOVE-CORRESPONDING ITAB TO SFLIGHT.
UPDATE SFLIGHT.
ENDLOOP.
WHEN ...
...
ENDCASE.
ENDMODULE.
Updating the database
• Detecting changes to the screen fields– SY-DATAR
• automatically set to ‘X’ when data is changed on a screen
• automatically reset every time the screen is updated– means that SY-DATAR could be reset although changes have
been made but not saved
– "FLAG" field (declared in TOP INCLUDE)• Create a module to set the flag whenever data is changed
on the screen.
Updating the database
• Exit modules are processed before any data is transferred from screen to program (including updating SY-DATAR), so this is the only way to capture changed data if an exit is triggered.
PROCESS AFTER INPUT.
FIELD: <all screen fields>
MODULE SET_FLAG ON REQUEST.
MODULE SET_FLAG INPUT.
FLAG = 'X'.
ENDMODULE.
MODULE EXIT INPUT.
IF FLAG = 'X'.
<save dialog>
ENDIF.
LEAVE TO SCREEN 0.
ENDMODULE.
Conclusion
• This lectured examined concepts associated with the use of Table Controls in a screen– definition– creation– maintenance
• filling• modifying• sorting• saving
Related Reading
• Textbook– Ch 3.3 - Dialog Applications
• On Line Help– R/3 Library…BC..ABAP Workbench
• BC ABAP User’s Guide– ABAP User Interface … Screens
• Processing User input on a Screen, Controlling the Screen Flow, Modifying the Screen, Using Tables in a Screen
Next Week
• Review of semester– Please ask questions!