38
ITB258 ABAP Programming Lecture 11 Using Tables in Transactions

Abap Module Ppt

Embed Size (px)

Citation preview

Page 1: Abap Module Ppt

ITB258ABAP Programming

Lecture 11

Using Tables in Transactions

Page 2: Abap Module Ppt

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

Page 3: Abap Module Ppt

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.

Page 4: Abap Module Ppt

Table Control

Page 5: Abap Module Ppt

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

Page 6: Abap Module Ppt

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

Page 7: Abap Module Ppt

CXTAB_CONTROL

Page 8: Abap Module Ppt

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.

Page 9: Abap Module Ppt

Adding a Table Control to a Screen

Enter a Name for the Table Control

Page 10: Abap Module Ppt

Press Dict/program fields pushbutton

Adding Fields to a Table Control

Page 11: Abap Module Ppt

Adding Fields to a Table Control

Enter database table name

Select Get From Dictionary

Page 12: Abap Module Ppt

Adding Fields to a Table Control

Enter internal table name

Select Get From program

Page 13: Abap Module Ppt

Adding Fields to a Table Control

Mark the fields to be added to the table control

Then click on Copy to select the fields

Page 14: Abap Module Ppt

Adding Fields to a Table Control

Drag the selectedfields into the table control, then release the mouse button.

Page 15: Abap Module Ppt

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

Page 16: Abap Module Ppt

Table Control Principles

Database Table

Internal TableInternal Table(Buffer)(Buffer)

1. Internal Table filled with selected data

Page 17: Abap Module Ppt

Table Control Principles

Internal TableInternal Table(Buffer)(Buffer)

2. Table Control contents filled from internal table

TableTableControlControl

Page 18: Abap Module Ppt

Table Control Principles

Internal TableInternal Table(Buffer)(Buffer)

TableTableControlControl

3. Changes to Table Control contents copied to internal table

Page 19: Abap Module Ppt

Table Control Principles

Database Table

Internal TableInternal Table(Buffer)(Buffer)

4. When screen is closed, internal table contents copied back to database

Page 20: Abap Module Ppt

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.

Page 21: Abap Module Ppt

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

Page 22: Abap Module Ppt

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

Page 23: Abap Module Ppt

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.

Page 24: Abap Module Ppt

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)

Page 25: Abap Module Ppt

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.

Page 26: Abap Module Ppt

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)

Page 27: Abap Module Ppt

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

...

Page 28: Abap Module Ppt

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.

Page 29: Abap Module Ppt
Page 30: Abap Module Ppt

Sample Fields List

Output only fieldsOutput only fields

Table ControlTable Control

Page 31: Abap Module Ppt

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.

Page 32: Abap Module Ppt

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.

Page 33: Abap Module Ppt

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.

Page 34: Abap Module Ppt

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.

Page 35: Abap Module Ppt

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.

Page 36: Abap Module Ppt

Conclusion

• This lectured examined concepts associated with the use of Table Controls in a screen– definition– creation– maintenance

• filling• modifying• sorting• saving

Page 37: Abap Module Ppt

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

Page 38: Abap Module Ppt

Next Week

• Review of semester– Please ask questions!