You are on page 1of 18

Table Controls: Examples with Modifications

The following example processes a table control with LOOP with parallel loop using an internal table. By using function codes you can sort columns and delete rows from the internal table. The ready for input status of the table control fields is controlled using a function code.

REPORT demo_dynpro_tabcont_loop_at. CONTROLS flights TYPE TABLEVIEW USING SCREEN 100. DATA: cols LIKE LINE OF flights-cols, lines TYPE i. DATA: ok_code TYPE sy-ucomm, save_ok TYPE sy-ucomm. DATA: itab TYPE TABLE OF demo_conn. TABLES demo_conn. SELECT * FROM spfli INTO CORRESPONDING FIELDS OF TABLE itab. LOOP AT flights-cols INTO cols WHERE index GT 2. cols-screen-input = '0'. MODIFY flights-cols FROM cols INDEX sy-tabix. ENDLOOP. CALL SCREEN 100. MODULE status_0100 OUTPUT. SET PF-STATUS 'SCREEN_100'. DESCRIBE TABLE itab LINES lines. flights-lines = lines. ENDMODULE. MODULE cancel INPUT. LEAVE PROGRAM. ENDMODULE. MODULE read_table_control INPUT. MODIFY itab FROM demo_conn INDEX flights-current_line. ENDMODULE. MODULE user_command_0100 INPUT. save_ok = ok_code. CLEAR ok_code. CASE save_ok. WHEN 'TOGGLE'. LOOP AT flights-cols INTO cols WHERE index GT 2. IF cols-screen-input = '0'.

cols-screen-input = '1'. ELSEIF cols-screen-input = '1'. cols-screen-input = '0'. ENDIF. MODIFY flights-cols FROM cols INDEX sy-tabix. ENDLOOP. WHEN 'SORT_UP'. READ TABLE flights-cols INTO cols WITH KEY selected = 'X'. IF sy-subrc = 0. SORT itab STABLE BY (cols-screen-name+10) ASCENDING. cols-selected = ' '. MODIFY flights-cols FROM cols INDEX sy-tabix. ENDIF. WHEN 'SORT_DOWN'. READ TABLE flights-cols INTO cols WITH KEY selected = 'X'. IF sy-subrc = 0. SORT itab STABLE BY (cols-screen-name+10) DESCENDING. cols-selected = ' '. MODIFY flights-cols FROM cols INDEX sy-tabix. ENDIF. WHEN 'DELETE'. READ TABLE flights-cols INTO cols WITH KEY screen-input = '1'. IF sy-subrc = 0. LOOP AT itab INTO demo_conn WHERE mark = 'X'. DELETE itab. ENDLOOP. ENDIF. ENDCASE. ENDMODULE. The layout of screen 100 is:

A resizable table control called FLIGHTS is defined. The fields of the table control are transferred from the structure DEMO_CONN in the ABAP Dictionary. The first two columns are lead columns. The corresponding fields are output fields. A title bar, columns headers, and a selection column are created. The component MARK of type character with length 1 from structure DEMO_CONN is assigned to the selection column. You can select one column and several lines. It has the following flow logic: PROCESS BEFORE OUTPUT. MODULE status_0100. LOOP AT itab INTO demo_conn WITH CONTROL flights. ENDLOOP. PROCESS AFTER INPUT. MODULE cancel AT EXIT-COMMAND. LOOP AT itab. MODULE read_table_control. ENDLOOP. MODULE user_command_0100. A loop is executed at PBO and PAI using the table control FLIGHTS and is also executed using the internal table ITAB of the ABAP program. During the PBO loop, no module is called to fill the table control from table ITAB of the ABAP program. However, during the PAI loop, a module is called to modify table ITAB. At PBO the component LINES of control structure FLIGHTS is filled explicitly with the current number of rows of the internal table before the PBO loop to install the scroll bar of the table control.

During the PBO loop, in the module FILL_TABLE_CONTROL the work area DEMO_CONN is filled with values from the internal table, where the row index corresponds to the current row of the table control. During the PAI loop, the rows of the internal table, whose row index corresponds to the current row of the table control, are overwritten with the contents of the work area DEMO_CONN. User input is transferred from the input fields of the control to the internal table. In particular, the internal table also contains a flag in the column MARK to indicate whether the row of the table control is selected or not. After the PAI loop, user input is processed in the module USER_COMMAND. The GUI status SCREEN_100 provides the appropriate function codes. When the program is called not all of the fields in the table control are ready for input. The static specifications of the table control in the Screen Painter are modified before CALL SCREEN in the program. The system uses the table COLS in control structure FLIGHTS. All columns with a column position larger than two are set to not ready for input status in a loop using the table FLIGHTCOLS. By choosing the function code TOGGLE, you can change the ready for input status of the columns. The function codes SORT_UP and SORT_DOWN allow you to sort selected columns of the internal table ITAB ascending or descending. The static settings of the table control allow you to select only a single column. The selected column is derived from the internal table FLIGHT-COLS. The name of the sort criteria in the SORT statement is determined dynamically from the component COLSSCREEN-NAME. The prefix DEMO_CONN- must be removed using an offset specification. After sort the selection is undone, and the component SELECTED in table FLIGHT-COLS is assigned a blank character. You can delete selected rows from the internal table ITAB using the function code DELETE. First the system checks whether the fields of the table control are ready for input. Then all selected rows are deleted in a loop using the internal table ITAB. Since the table control is read again from the internal table in the PBO loop, the rows on the screen are deleted.

Back to ABAP Program Stations

SAP Table Control Example


This example shows how to build a table control using ABAP. The program builds a simple phone list. The code and accompanying screen shots describe the entire procedure. SCREEN 100
PROCESS BEFORE OUTPUT. MODULE STATUS_0100. LOOP WITH CONTROL TCTRL_USERDATA. MODULE DISPLAY_USERDATA. ENDLOOP. MODULE AFTER_LOOPING. * PROCESS AFTER INPUT. MODULE USER_COMMAND_0100. LOOP WITH CONTROL TCTRL_USERDATA. MODULE EXTRACT_USERDATA. ENDLOOP. MODULE USER_COMMAND_0100_AFTER_LOOP.

ZTBLDTOP
*&---------------------------------------------------------------------* *& Include ZTBLDEMO *& Report ZTBLDEMO * *

*&---------------------------------------------------------------------*

REPORT

ZTBLDEMO

MESSAGE-ID AT

DATA: BEGIN OF RECORD, NAME(30), PHONE(20), EMAIL(50), END OF RECORD.

DATA: ITAB_PHONELIST LIKE RECORD OCCURS 0 WITH HEADER LINE.

CONTROLS: TCTRL_PHONELIST TYPE TABLEVIEW USING SCREEN 100.

DATA: SAVE_CODE(4), OK_CODE(4), FILENAME LIKE RLGRAP-FILENAME, CONFIRMATION, SELECTION.

ZTBLDEMO
*&---------------------------------------------------------------------* *& Report *& ZUSRDATA * *

*&---------------------------------------------------------------------* *& *& * *

*&---------------------------------------------------------------------*

INCLUDE ZTBLDTOP.

" global Data

* INCLUDE ZUSRDO01. * INCLUDE ZUSRDI01. * INCLUDE ZUSRDF01.

" PBO-Modules " PAI-Modules " FORM-Routines

* * *

*&---------------------------------------------------------------------* *& Module STATUS_0100 OUTPUT

*&---------------------------------------------------------------------* * text

*----------------------------------------------------------------------* MODULE STATUS_0100 OUTPUT. SET PF-STATUS 'FOR100'. SET TITLEBAR '100'. ENDMODULE. " STATUS_0100 OUTPUT

*&---------------------------------------------------------------------* *& Module DISPLAY_USERDATA OUTPUT

*&---------------------------------------------------------------------* * text

*----------------------------------------------------------------------* MODULE DISPLAY_USERDATA OUTPUT. READ TABLE ITAB_PHONELIST INDEX TCTRL_PHONELIST-CURRENT_LINE. IF SY-SUBRC EQ 0. RECORD-NAME = ITAB_PHONELIST-NAME. RECORD-PHONE = ITAB_PHONELIST-PHONE. RECORD-EMAIL = ITAB_PHONELIST-EMAIL. ELSE.

EXIT FROM STEP-LOOP. ENDIF. ENDMODULE. " DISPLAY_USERDATA OUTPUT

*&---------------------------------------------------------------------* *& Module USER_COMMAND_0100 INPUT

*&---------------------------------------------------------------------* * text

*----------------------------------------------------------------------* MODULE USER_COMMAND_0100 INPUT. SAVE_CODE = OK_CODE. CLEAR OK_CODE. CASE SAVE_CODE. WHEN 'BACK'. CALL FUNCTION 'COPO_POPUP_TO_GOON' EXPORTING TEXTLINE1 = 'Any modifications will be lost!' TEXTLINE2 = 'Are you sure?' TITEL IMPORTING answer = confirmation. = 'Exit The Program'

if confirmation = 'G'. LEAVE PROGRAM. ENDIF. WHEN 'DELE'. CALL FUNCTION 'COPO_POPUP_TO_GOON' EXPORTING TEXTLINE1 = 'Selected rows will be deleted!' TEXTLINE2 = 'Are you sure?'

TITEL IMPORTING answer

= 'Delete rows'

= confirmation.

IF CONFIRMATION = 'G'. REFRESH ITAB_PHONELIST. CLEAR ITAB_PHONELIST. ENDIF. WHEN 'SAVE'. REFRESH ITAB_PHONELIST. CLEAR ITAB_PHONELIST. WHEN 'APND'. REFRESH ITAB_PHONELIST. CLEAR ITAB_PHONELIST. WHEN 'READ'. IF NOT FILENAME IS INITIAL. CALL FUNCTION 'COPO_POPUP_TO_GOON' EXPORTING TEXTLINE1 = 'Any modifications will be lost!' TEXTLINE2 = 'Are you sure?' TITEL IMPORTING answer ENDIF. if confirmation = 'G' or FILENAME IS INITIAL. CALL FUNCTION 'UPLOAD' EXPORTING FILENAME = = confirmation. = 'Read Another File'

'c:\temp\fonelist.txt' IMPORTING ACT_FILENAME TABLES DATA_TAB ENDIF. ENDCASE. ENDMODULE. " USER_COMMAND_0100 INPUT = ITAB_PHONELIST. = FILENAME

*&---------------------------------------------------------------------* *& Module EXTRACT_USERDATA INPUT

*&---------------------------------------------------------------------* * text

*----------------------------------------------------------------------* MODULE EXTRACT_USERDATA INPUT. CASE SAVE_CODE. WHEN 'DELE'. IF SELECTION IS INITIAL AND confirmation = 'G'. PERFORM TRANSFER. ENDIF. WHEN 'SAVE'. PERFORM TRANSFER. WHEN 'APND'. PERFORM TRANSFER. ENDCASE. ENDMODULE. " EXTRACT_USERDATA INPUT

*&---------------------------------------------------------------------* *& Form TRANSFER

*&---------------------------------------------------------------------* * text

*----------------------------------------------------------------------* * * --> <-p1 p2 text text

*----------------------------------------------------------------------* FORM TRANSFER. CLEAR ITAB_PHONELIST. ITAB_PHONELIST-NAME = RECORD-NAME. ITAB_PHONELIST-PHONE = RECORD-PHONE. ITAB_PHONELIST-EMAIL = RECORD-EMAIL. APPEND ITAB_PHONELIST. ENDFORM. " TRANSFER

*&---------------------------------------------------------------------* *& Module USER_COMMAND_0100_AFTER_LOOP INPUT

*&---------------------------------------------------------------------* * text

*----------------------------------------------------------------------* MODULE USER_COMMAND_0100_AFTER_LOOP INPUT. CASE SAVE_CODE. WHEN 'SAVE'. CALL FUNCTION 'DOWNLOAD' EXPORTING FILENAME TABLES DATA_TAB WHEN 'APND'. CLEAR ITAB_PHONELIST. " a blank row being created. = ITAB_PHONELIST. = FILENAME

APPEND ITAB_PHONELIST. " a blank row being added. ENDCASE. ENDMODULE. " USER_COMMAND_0100_AFTER_LOOP INPUT

Field List

GUI Status

Objects

Table Attributes

Table Columns

Table Controls in ABAP Programs


To handle table controls in ABAP programs, you must declare a control in the declaration part of the program for each table control using the following statement: CONTROLS ctrl TYPE TABLEVIEW USING SCREEN scr. where ctrl is the name of the table control on a screen in the ABAP program. The control allows the ABAP program to read the attributes of the table control and to influence the control. The statement also declares a deep structure of name ctrl. The data type of the structure corresponds to the type CXTAB_CONTROL defined in the type group CXTAB in the ABAP Dictionary. At runtime the components of the structure contain the attributes of the table control. Several of the initial values are determined in the Screen Painter. The initial value for the control ctrl is taken from the screen which is determined using the addition USING. You can start a screen sequence from an ABAP program using REFRESH CONTROL ctrl FROM SCREEN scr. you can initialize a table control at any time with the initial value of a screen scr. Values that are not taken from the settings in the Screen Painter, are set to the current status of the table control at PAI.

Structure CXTAB_CONTROL
The deep structure CXTAB_CONTROL contains the general attributes of the table control on the highest level. The component CXTAB_CONTROL is a table of the structure CXTAB_COLUMN and contains the attributes of a column. The component CXTAB_CONTROL-COLS-SCREEN is a flat structure of the same type as system table screen and contains the attributes of the individual screen elements.

Up to component CURRENT_LINE, all attributes of the structure CXTAB_CONTROL in the ABAP program can be set to change the table control display on the screen.

Components of CXTAB_CONTROL
The structure CXTAB_CONTROL has the following components: Component FIXED_COLS Type (Length) INT4 Meaning In the Screen Painter:

Number of lead columns. Transferred Fixed columns from Screen Painter. Can be changed in the ABAP program. Controls the scroll bar of the table control. At LOOP without internal table, LINES has the initial value zero and must be set in the program so that the scroll bar can be used. At LOOP AT itab the system sets this component to the number of rows of the internal table, whenever the table control is processed for the first time. The initialization event of a table control is not determined uniquely. If the corresponding internal table is not fully created at this event, then the LINES variable receives an incorrect value. If LINES in the LOOP loop is smaller as the number of rows of the internal table, then the table control contains blank rows at the end. Therefore you should always set the LINES component explicitly in the ABAP program, including at LOOP AT itab. In this way you have full control over the dimensions of the vertical scroll bar and so can control the number of rows that are ready for input. Initialization should usually occur at PBO directly before theLOOP statement for the table control. Top row at next PBO. Set at PAI by position of the vertical slider box. Can be changed in the ABAP program. Current line in the loop. Set automatically in the LOOP loop to the value sy-stepl+(TOP_LINE-1). No changes allowed in the ABAP program. First displayed horizontal scrollable column after the lead column. Set at PAI by position of the horizontal slider box. LEFT_COL contains the absolute number of the column independent of any column shift by

LINES

INT4

TOP_LINE

INT4

CURRENT_LINE

INT4

LEFT_COL

INT4

the user. Can be changed in the ABAP program. LINE_SEL_MODE INT4 Row selection mode: 0, 1, 2 for none, Row selection one or multiple rows can be selected. Transferred from Screen Painter. Can be changed in the ABAP program. Column selection mode: 0, 1, 2 for none, one or multiple rows can be selected. Transferred from Screen Painter. Can be changed in the ABAP program. Flag (X or blank) for selection column. Transferred from Screen Painter. Can be changed in the ABAP program. Column selection

COL_SEL_MODE

INT4

LINE_SELECTOR

CHAR(1)

Selection column

H_GRID

CHAR(1)

Flag (X or blank) for horizontal Separators separators. Transferred from Screen Painter. Can be changed in the ABAP program. Flag (X or blank) for vertical Separators separators. Transferred from Screen Painter. Can be changed in the ABAP program.

V_GRID

CHAR(1)

COLS INVISIBLE

CXTAB_COLUMN Control table for single columns (see below). CHAR(1) Flag (X or blank) for visibility of entire table control.

Internal table CXTAB_COLUMN is a component of the structure CXTAB_CONTROL.

Components of CXTAB_COLUMN
Each column of the table control corresponds to a row of the table CXTAB_COLUMN. The internal table CXTAB_COLUMN has no header line and the following columns: Component SCREEN INDEX Type (Length) SCREEN INT4 Meaning Structure for the attributes of screen elements of the column (see below). Current position of the column in the table control. Transfered with initial value from Screen Painter. At PAI the current column configuration of the table control is withdrawn. Can be changed in the ABAP program. Flag (X or blank) whether column is selected or not. At PAI the current status of the table control is withdrawn. Can be changed in the ABAP program. Visible length of the column. Transferred from Screen Painter. Can be changed in the ABAP program. Flag (X or blank) whether column is visible. Transferred from Screen Painter. Can be changed in the ABAP

SELECTED

CHAR(1)

VISLENGTH INVISIBLE

INT4 CHAR(1)

program. The structure screen is a column of the table CXTAB_COLUMN.

Components of SCREEN
The component SCREEN-NAME is the name of the screen element that creates the column. The initial values of the remaining components correspond to the attributes of the screen elements in the Screen Painter. The attributes are transferred from here with initial values. They can be changed directly in the ABAP program. Note that directly set values can be overwritten by modifying these attributes using MODIFY SCREEN within the loop LOOP AT SCREEN. Furthermore, note that the attributes of the structure screen are not set with blank characters and X, but are set with 0 and 1 as in the normal processing of screen.

Scrolling in Table Controls


Scrolling with scroll bars is automatically implemented with table controls, and managed by the system. However, the componentctrl-LINES must be set to the required row number before editing the table control. This is the case for table controls with or without parallel loops using an internal table. The component LINES is set implicitly for table controls with parallel loops using an internal table, although the correct value is not guaranteed (see table above). It is therefore recommended that in both instances you set the component LINES explicitly to the number of rows in the internal table of the ABAP program. You can determine this number from the statement DESCRIBE TABLE (see the following example). The event PAI with an empty function code is triggered when scrolling using scroll bars. When the loops at PAI and PBO are executed, the system sets the component TOP_LINE of the structure CXTAB_CONTROL before PBO, so that the correct row is edited at the loop pass. You can easily implement program-controlled vertical scrolling (line by line, page by page, or goto page) by using the component of the structure CXTAB_CONTROL. Essentially the component TOP_LINE must be provided with a value. For page by page scrolling, the number of rows to be scrolled during the loop pass can be taken from the system field sy-loopc. sy-loopccontains the number of rows currently display, whereas the component LINES of structure CXTAB_CONTROL contains the number of all rows in the table control.

Cursor Position on Table Controls


At PBO you can set the cursor on a specific field of a specific row of a table control. SET CURSOR FIELD f LINE lin [OFFSET off]. Using the optional addition OFFSET, you can enter the offset of the cursor in the field as described under Setting the Cursor Position. At PAI you can read the current cursor position. GET CURSOR FIELD f LINE lin ... In addition to the information given under Finding Out the Cursor Position , field lin contains information on which row of the table control the cursor is currently on. You can also use GET CURSOR LINE lin. . sy-subrc allows you to check if the cursor is placed in a row of a table control.

You might also like