Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  User Dialogs →  Screens →  Statements in the Screen Flow Logic →  Table Controls →  Table Control - Examples 

Table Control with Modifications

This example demonstrates sorting by columns and deleting rows in table controls.

Other versions: 7.31 | 7.40 | 7.54

Source 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
       ##TOO_MANY_ITAB_FIELDS.

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.

Description

The table control flights is defined and can be resized. The fields of the table control are copied from the DEMO_CONN structure in the ABAP Dictionary. The first two columns are lead columns. The corresponding fields are output fields. A title list, column headers and a selection column are created. The component MARK, of type CHAR, length 1, from the structure DEMO_CONN is assigned to the selection column. One column and multiple rows can be selected.

In the flow logic of screen 100, one loop is executed during PBO and another during PAI using the table control flights and the internal table itab of the ABAP program in parallel. In the PBO loop, no modules are called to fill the table control from the table itab of the ABAP program. However, in the PAI loop a module is called to modify the table itab.

During PBO, the component lines of the control structure flights is explicitly filled with the current number of rows in internal table itab to set up the scroll bar of the table control before the PBO loop.

In the PAI loop, the rows of the internal table with row indexes corresponding to the current table control rows are overwritten with the contents of the work area demo_conn. In this way, user inputs in the input fields of the control are transferred to the internal table. A flag is set in the internal table column MARK indicating whether the table control row is selected or not.

After the PAI loop, the user inputs are processed in the user_command_0100 module. The GUI status SCREEN_100 enables the corresponding function codes.

The fields of the table control are not ready for input when the program is called. The static settings of the control table in the screen painter are modified before CALL SCREEN in the program. The table cols in the control structure flights is used. All columns with a column position greater than two are set to not ready for input using a loop through the flights-cols table. By selecting the function code TOGGLE, the input readiness of these fields can be switched on and off.

Using the function codes "SORT_UP" and "SORT_DOWN", it is possible to sort the columns of the internal table itab into either ascending or descending order. Due to the static settings of the table control, it is only possible to select one column at a time. The selected column is removed from the internal table flights-cols. The name of the sort criteria in the SORT statement is dynamically assigned from the component cols-screen-name. The prefix demo_conn- must be removed using an offset specification. After sorting, the selection is reversed by assigning an empty character to the component selected in the table flights-cols.

Using the function code "DELETE" it is possible to delete selected rows from the internal table itab. First, flights-cols is checked to establish whether the fields in the table control are ready for input. Then all the selected rows are deleted using a loop through the internal table itab. As the table control is completely repopulated during the PBO loop from the internal table itab, the rows are also deleted from the screen.