ITB258 ABAP Programming Lecture 11 Using Tables in Transactions
Objectives <ul><li>This lecture aims to introduce the concepts associated with:  </li></ul><ul><ul><li>Table controls:  gr...
Using Tables in a Screen <ul><li>ABAP offers the  Table Control  graphical element as a mechanism for displaying and modif...
Table Control
Table Controls <ul><li>Table Control features allow </li></ul><ul><ul><li>horizontal & vertical scrolling </li></ul></ul><...
Creating a Table Control <ul><li>In the TOP Include object: </li></ul>CONTROLS  ctrl  TYPE TABLEVIEW  USING SCREEN  scr . ...
CXTAB_CONTROL
Adding a Table Control to a Screen In the Graphical Fullscreen Editor choose the  Table Control  element button. Use the l...
Adding a Table Control to a Screen Enter a Name for the Table Control
Adding Fields to a Table Control Press Dict/program fields pushbutton
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 selected fields into the table control,  then release the mouse button.
Adding Fields to a Table Control The label for each column is a text field, created by clicking on the text field icon and...
Table Control Principles Database Table Internal Table (Buffer) 1. Internal Table filled with selected data
Table Control Principles Internal Table (Buffer) 2. Table Control contents filled from internal table Table Control
Table Control Principles Internal Table (Buffer) Table Control 3. Changes to Table Control contents copied to internal table
Table Control Principles Database Table Internal Table (Buffer) 4. When screen is closed, internal table contents copied b...
Table Control Principles PBO PAI Select rows from the database into an internal table Screen 1 PBO PAI Screen 2 ( with tab...
Table Control Principles <ul><li>Fields are transported from the module pool to the screen each time a loop is processed <...
Table Control Principles <ul><li>All screen fields that do not belong to a table control and are not specified in a FIELD ...
Updating Data in Table Controls <ul><li>Method 1:   Read the internal table into the Table Control in the screen's flow lo...
Table Controls in the Flow Logic <ul><li>Method 1 (table control fields = itab fields) </li></ul>PROCESS BEFORE OUTPUT. LO...
Table Controls in the Flow Logic <ul><li>Method 2 (table control fields = dict. fields) </li></ul>PROCESS BEFORE OUTPUT. L...
Table Controls in the Flow Logic PROCESS BEFORE OUTPUT. LOOP WITH CONTROL FLIGHTS. MODULE READ_ITAB. ENDLOOP. PROCESS AFTE...
Table Control Attributes data: begin of cxtab_control, ... cols type  cxtab_column  occurs 10, end of cxtab_control. data:...
Sorting a Table Control by a Selected Field <ul><li>DATA: WA LIKE LINE OF FLIGHTS-COLS. </li></ul><ul><li>... </li></ul><u...
 
Sample Fields List  Output only fields Table Control
Handling Line Selection PROCESS AFTER INPUT. LOOP AT ITAB. FIELD ITAB-MARK   MODULE MODIFY_ITAB ON REQUEST. ENDLOOP. MODUL...
Updating the Internal Table PROCESS AFTER INPUT. LOOP WITH CONTROL FLIGHTS. CHAIN. FIELD: SFLIGHT-PRICE, SFLIGHT-FLDATE. M...
Updating the Database Saving the Updated Data (both methods) <ul><li>MODULE USER_COMMAND_200. </li></ul><ul><li>CASE OK_CO...
Updating the database <ul><li>Detecting changes to the screen fields </li></ul><ul><ul><li>SY-DATAR </li></ul></ul><ul><ul...
Updating the database <ul><li>Exit modules are processed before any data is transferred from screen to program (including ...
Conclusion <ul><li>This lectured examined concepts associated with the use of Table Controls in a screen </li></ul><ul><ul...
Related Reading <ul><li>Textbook </li></ul><ul><ul><li>Ch 3.3 - Dialog Applications </li></ul></ul><ul><li>On Line Help </...
Next Week <ul><li>Review of semester </li></ul><ul><ul><li>Please ask questions! </li></ul></ul>
Upcoming SlideShare
Loading in …5
×

258lec11

2,547 views

Published on

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,547
On SlideShare
0
From Embeds
0
Number of Embeds
50
Actions
Shares
0
Downloads
272
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • Very often it is desirable to be able to represent a one-to-many relationship on the screen. For instance, given an airline carrier and a flight code show all the scheduled flights. (Or given an order number show all the ordered products.) ABAP provides the ‘table control’ as a graphical element on a screen that is capable of holding and displaying multiple rows of a table in a ‘spreadsheet’ format.
  • This slide illustrates a table control object in a screen.
  • The table control object has many features built in to enhance screen display and usability. They include the ability to scroll through records (horizontally and vertically) and inside a field if the field width is greater than the table control’s column width. Table controls also allow the user to dynamically resize column widths, reorder columns, and select columns and rows for subsequent processing.
  • As well as drawing the table control on the screen it is necessary to declare the table control as a data item (in the TOP include program for the transaction).
  • To view the structure of the cxtab-control object use the Repository Browser and choose Dictionary Objects…Choose the Type Group radiobutton and enter CXTAB…Press the Display icon on the application toolbar. Note that a component of the object is an internal table, cols. Each displayed field of the table control will have a row in the cols internal table which describes various attributes of the field.
  • There are a set of programming principles that should be adhered to when using table controls and step loops. Data from the database should be initially loaded into an internal table. (This means that the database is accessed for read purposes only once in the transaction.) Next the rows of the internal table are loaded into the table control Any changes that are made to the data are then saved back to the internal table. At the end of the transaction, the contents of the internal table can be written back to the database, again to minimise database I/O.
  • There are a set of programming principles that should be adhered to when using table controls and step loops. Data from the database should be initially loaded into an internal table. (This means that the database is accessed for read purposes only once in the transaction.) Next the rows of the internal table are loaded into the table control Any changes that are made to the data are then saved back to the internal table. At the end of the transaction, the contents of the internal table can be written back to the database, again to minimise database I/O.
  • There are a set of programming principles that should be adhered to when using table controls and step loops. Data from the database should be initially loaded into an internal table. (This means that the database is accessed for read purposes only once in the transaction.) Next the rows of the internal table are loaded into the table control Any changes that are made to the data are then saved back to the internal table. At the end of the transaction, the contents of the internal table can be written back to the database, again to minimise database I/O.
  • There are a set of programming principles that should be adhered to when using table controls and step loops. Data from the database should be initially loaded into an internal table. (This means that the database is accessed for read purposes only once in the transaction.) Next the rows of the internal table are loaded into the table control Any changes that are made to the data are then saved back to the internal table. At the end of the transaction, the contents of the internal table can be written back to the database, again to minimise database I/O.
  • PAI logic for screen 1 loads the internal table with data from the database according to the entries supplied by the user. PBO logic for screen 2 populates the table control from the internal table (buffer). User action in screen 2 triggers the PAI logic. PAI logic updates the internal table with new values entered (into the table control screen fields) by the user. PAI logic is triggered by actions such as scrolling down a single row as well as actions such as BACK, EXIT, etc. Unless the user action causes the transaction to leave the current screen, after the PAI modules have been executed, the PBO modules for the screen are executed again. Thus the table control fields are updated/refreshed after every user action.
  • In PBO processing fields are transported from the module pool to the screen in a predefined order. 1. The table control step loop is processed row by row. Fields with corresponding names are transported from the module pool to the screen. 2. After the step loop has been processed all remaining module pool fields are transported to the screen.
  • The ABAP language provides two mechanisms for loading the table control with data from the internal table and then storing the altered rows of the table control back to the internal table.
  • In the flow logic you can read an internal table using the LOOP statement. You define the reference to the relevant able control by specifying WITH CONTROL &lt;ctrl&gt; You determine which table entry is to be read by specifying CURSOR &lt;ctrl&gt;-CURRENT_LINE. After the read operation the field contents are placed in the header line of the internal table. If the fields in the table control have the same name as the internal they will be filled automatically. (Otherwise you need to write a module to transfer the internal table fields to the screen fields.) You must reflect any changes the user makes to the fields of the table control in the internal table otherwise they will not appear when the screen is redisplayed after PBO processing, (eg, after the user presses Enter or scrolls) However, this processing should be performed only if changes have actually been made to the screen fields of the table control (hence the use of the ON REQUEST)
  • If you use a LOOP statement without an internal table in the flow logic, you must read the data in a PBO module which is called each time the loop is processed. Since, in this case, the system cannot determine the number of internal table entries itself, you must use the EXIT FROM STEP-LOOP statement to ensure that no blank lines are displayed in the table control if there are no more corresponding entries in the internal table.
  • If you use a LOOP statement without an internal table in the flow logic, you must read the data in a PBO module which is called each time the loop is processed. Since, in this case, the system cannot determine the number of internal table entries itself, you must use the EXIT FROM STEP-LOOP statement to ensure that no blank lines are displayed in the table control if there are no more corresponding entries in the internal table.
  • Sorting the display by a selected column is very simple to implement using the table control attributes &lt;ctrl&gt;-SELECTED and &lt;ctrl&gt;-SCREEN-NAME. Here we assume that the table control displays the fields of an internal table (ITAB) as per Option 1 discussed previously. WA-SCREEN-NAME refers to the name of the selected column of the table control. The expression (WA-SCREEN-NAME+5) refers to the contents of the variable offset 5 characters from the left. In this example WA-SCREEN-NAME will have a value like ITAB-PRICE. The expression (WA-SCREEN-NAME+5) will have the value PRICE. SORT ITAB BY (WA-SCREEN-NAME+5) will resolve to SORT ITAB BY PRICE. To adjust this code for use in your own program substitute the name of the control used in your program and calculate the offset required to trim the structure name from the selected SCREEN-NAME.
  • This slide illustrates a table control object in a screen.
  • 258lec11

    1. 1. ITB258 ABAP Programming Lecture 11 Using Tables in Transactions
    2. 2. Objectives <ul><li>This lecture aims to introduce the concepts associated with: </li></ul><ul><ul><li>Table controls: graphical screen elements used for displaying more than n rows of a table on a single screen </li></ul></ul><ul><ul><li>screen flow logic for handling table controls </li></ul></ul>
    3. 3. Using Tables in a Screen <ul><li>ABAP offers the Table Control graphical element as a mechanism for displaying and modifying tabular data in a screen </li></ul><ul><li>a Table Control is a screen ‘container’ or object that allows display of multiple rows of a table (database or internal) on a screen </li></ul><ul><li>Good for displaying a one-to-many relationship on the one screen. </li></ul>
    4. 4. Table Control
    5. 5. Table Controls <ul><li>Table Control features allow </li></ul><ul><ul><li>horizontal & vertical scrolling </li></ul></ul><ul><ul><li>column width resizing </li></ul></ul><ul><ul><li>scrolling within a field (where contents are wider than the screen field width) </li></ul></ul><ul><ul><li>reordering the sequence of columns </li></ul></ul><ul><ul><li>saving the current display settings for future use </li></ul></ul><ul><ul><li>selection of table rows & columns </li></ul></ul><ul><li>Formatting features include </li></ul><ul><ul><li>automatic table resizing on window resize </li></ul></ul><ul><ul><li>separator lines between rows & columns </li></ul></ul><ul><ul><li>column header fields for all columns </li></ul></ul>
    6. 6. Creating a Table Control <ul><li>In the TOP Include object: </li></ul>CONTROLS ctrl TYPE TABLEVIEW USING SCREEN scr . <ul><li>TABLEVIEW corresponds to a complex type </li></ul><ul><ul><li>(CXTAB_CONTROL defined in the ABAP dictionary) </li></ul></ul><ul><li>USING SCREEN determines the screen from which the table control will get its initial values </li></ul>
    7. 7. CXTAB_CONTROL
    8. 8. 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.
    9. 9. Adding a Table Control to a Screen Enter a Name for the Table Control
    10. 10. Adding Fields to a Table Control Press Dict/program fields pushbutton
    11. 11. Adding Fields to a Table Control Enter database table name Select Get From Dictionary
    12. 12. Adding Fields to a Table Control Enter internal table name Select Get From program
    13. 13. Adding Fields to a Table Control Mark the fields to be added to the table control Then click on Copy to select the fields
    14. 14. Adding Fields to a Table Control Drag the selected fields into the table control, then release the mouse button.
    15. 15. Adding Fields to a Table Control The label for each column is a text field, created by clicking on the text field icon and then dragging the new text field onto the header of the column before typing in the label Text field icon
    16. 16. Table Control Principles Database Table Internal Table (Buffer) 1. Internal Table filled with selected data
    17. 17. Table Control Principles Internal Table (Buffer) 2. Table Control contents filled from internal table Table Control
    18. 18. Table Control Principles Internal Table (Buffer) Table Control 3. Changes to Table Control contents copied to internal table
    19. 19. Table Control Principles Database Table Internal Table (Buffer) 4. When screen is closed, internal table contents copied back to database
    20. 20. Table Control Principles PBO PAI Select rows from the database into an internal table Screen 1 PBO PAI Screen 2 ( with table control ) LOOP ... read internal table into table control line by line ENDLOOP. LOOP ... update internal table from table control line by line ENDLOOP.
    21. 21. Table Control Principles <ul><li>Fields are transported from the module pool to the screen each time a loop is processed </li></ul><ul><li>The remaining screen fields are transported at the end of PBO processing </li></ul>LOOP start LOOP end transport ABAP table control fields to screen fields transport remaining ABAP fields to screen fields PROCESS BEFORE OUTPUT
    22. 22. Table Control Principles <ul><li>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 </li></ul><ul><li>Table control fields are transported row by row to module pool fields </li></ul><ul><li>Fields specified in FIELD statements are transported immediately before the FIELD statement is executed </li></ul>LOOP start LOOP end transport screen table control fields to ABAP module pool fields transport all fields from the screen to ABAP fields excluding table control fields PROCESS AFTER INPUT
    23. 23. Updating Data in Table Controls <ul><li>Method 1: Read the internal table into the Table Control in the screen's flow logic. </li></ul><ul><ul><li>Used when the names of the Table Control fields are based on fields of the internal table. </li></ul></ul><ul><li>Method 2: Read the internal table into the Table Control in the module pool code. </li></ul><ul><ul><li>Used when the names of the Table Control fields are based on fields of the database table. </li></ul></ul>
    24. 24. Table Controls in the Flow Logic <ul><li>Method 1 (table control fields = itab fields) </li></ul>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.
    25. 25. Table Controls in the Flow Logic <ul><li>Method 2 (table control fields = dict. fields) </li></ul>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.
    26. 26. 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. <ul><li>Method 2 (table control fields = dict. fields) </li></ul>
    27. 27. Table Control Attributes data: 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 ...
    28. 28. Sorting a Table Control by a Selected Field <ul><li>DATA: WA LIKE LINE OF FLIGHTS-COLS. </li></ul><ul><li>... </li></ul><ul><li>MODULE USER_COMMAND_200. </li></ul><ul><li>... </li></ul><ul><li>LOOP AT FLIGHTS-COLS INTO WA. </li></ul><ul><li> IF WA-SELECTED = ‘X’. </li></ul><ul><li> SORT ITAB BY (WA-SCREEN-NAME+5). </li></ul><ul><li>ENDIF. </li></ul><ul><li>ENDLOOP. </li></ul><ul><li>ENDMODULE. </li></ul>
    29. 30. Sample Fields List Output only fields Table Control
    30. 31. 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.
    31. 32. 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.
    32. 33. Updating the Database Saving the Updated Data (both methods) <ul><li>MODULE USER_COMMAND_200. </li></ul><ul><li>CASE OK_CODE. </li></ul><ul><li>WHEN ‘SAVE’. </li></ul><ul><ul><li>LOOP AT ITAB. </li></ul></ul><ul><ul><li>CHECK ITAB-MARK = ‘X’. </li></ul></ul><ul><ul><li>MOVE-CORRESPONDING ITAB TO SFLIGHT. </li></ul></ul><ul><ul><li>UPDATE SFLIGHT. </li></ul></ul><ul><ul><li>ENDLOOP. </li></ul></ul><ul><ul><li>WHEN ... </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><li>ENDCASE. </li></ul><ul><li>ENDMODULE. </li></ul>
    33. 34. Updating the database <ul><li>Detecting changes to the screen fields </li></ul><ul><ul><li>SY-DATAR </li></ul></ul><ul><ul><ul><li>automatically set to ‘X’ when data is changed on a screen </li></ul></ul></ul><ul><ul><ul><li>automatically reset every time the screen is updated </li></ul></ul></ul><ul><ul><ul><ul><li>means that SY-DATAR could be reset although changes have been made but not saved </li></ul></ul></ul></ul><ul><ul><li>&quot;FLAG&quot; field (declared in TOP INCLUDE) </li></ul></ul><ul><ul><ul><li>Create a module to set the flag whenever data is changed on the screen. </li></ul></ul></ul>
    34. 35. Updating the database <ul><li>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. </li></ul><ul><li>PROCESS AFTER INPUT. </li></ul><ul><li>FIELD: < all screen fields > </li></ul><ul><li>MODULE SET_FLAG ON REQUEST. </li></ul><ul><li>MODULE SET_FLAG INPUT. </li></ul><ul><li>FLAG = 'X'. </li></ul><ul><li>ENDMODULE. </li></ul><ul><li>MODULE EXIT INPUT. </li></ul><ul><li>IF FLAG = 'X'. </li></ul><ul><li>< save dialog > </li></ul><ul><li>ENDIF. </li></ul><ul><li>LEAVE TO SCREEN 0. </li></ul><ul><li>ENDMODULE. </li></ul>
    35. 36. Conclusion <ul><li>This lectured examined concepts associated with the use of Table Controls in a screen </li></ul><ul><ul><li>definition </li></ul></ul><ul><ul><li>creation </li></ul></ul><ul><ul><li>maintenance </li></ul></ul><ul><ul><ul><li>filling </li></ul></ul></ul><ul><ul><ul><li>modifying </li></ul></ul></ul><ul><ul><ul><li>sorting </li></ul></ul></ul><ul><ul><ul><li>saving </li></ul></ul></ul>
    36. 37. Related Reading <ul><li>Textbook </li></ul><ul><ul><li>Ch 3.3 - Dialog Applications </li></ul></ul><ul><li>On Line Help </li></ul><ul><ul><li>R/3 Library…BC..ABAP Workbench </li></ul></ul><ul><ul><ul><li>BC ABAP User’s Guide </li></ul></ul></ul><ul><ul><ul><ul><li>ABAP User Interface … Screens </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>Processing User input on a Screen, Controlling the Screen Flow, Modifying the Screen, Using Tables in a Screen </li></ul></ul></ul></ul></ul>
    37. 38. Next Week <ul><li>Review of semester </li></ul><ul><ul><li>Please ask questions! </li></ul></ul>

    ×