一、创建主程序
1.定义程序相关数据类型
a.定义Table Control的数据来源
b.定义Table Control的数据变量
2.定义主程序的数据屏幕处理逻辑
二、创建承载Table Control 的子屏幕
1.给子屏幕添加table control控件
2.给子屏幕添加OK_CODE屏幕元素
3.实现Table Control与ABAP的数据交换
*&---------------------------------------------------------------------*
*& Report ZTEST_ZH02
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_cq.
*&---------------------------------------------------------------------*
*& Types & Internal table
*&---------------------------------------------------------------------*
TYPES: BEGIN OF tp_flight,
flag(1) TYPE c,
carrid TYPE sflight-carrid,
connid TYPE sflight-connid,
fldate TYPE sflight-fldate,
price TYPE sflight-price,
currency TYPE sflight-currency,
planetype TYPE sflight-planetype,
END OF tp_flight.
DATA gs_flight TYPE tp_flight.
DATA gt_flight TYPE tp_flight OCCURS 0.
*&---------------------------------------------------------------------*
*& Variants
*&---------------------------------------------------------------------*
CONTROLS tc_0100 TYPE TABLEVIEW USING SCREEN 0100.
DATA: gs_cols TYPE cxtab_column.
DATA: gv_lines TYPE i.
DATA: gv_mode TYPE c VALUE '0'.
DATA: ok_code TYPE sy-ucomm.
DATA: ok_save TYPE sy-ucomm.
*&---------------------------------------------------------------------*
*& start-of-selection
*&---------------------------------------------------------------------*
START-OF-SELECTION.
PERFORM frm_get_data.
PERFORM frm_set_no_edit.
call SCREEN 0100.
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_get_data .
select *
INTO CORRESPONDING FIELDS OF TABLE gt_flight
FROM sflight.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_NO_EDIT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_set_no_edit .
LOOP AT tc_0100-cols INTO gs_cols.
gs_cols-screen-input = gv_mode.
MODIFY tc_0100-cols FROM gs_cols.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Module LOAD_DATA OUTPUT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
MODULE load_data OUTPUT.
READ TABLE GT_FLIGHT INTO GS_FLIGHT INDEX TC_0100-CURRENT_LINE.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Module PUSH_DATA INPUT
*&---------------------------------------------------------------------*
MODULE push_data INPUT.
MODIFY GT_FLIGHT FROM GS_FLIGHT INDEX TC_0100-CURRENT_LINE.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS 'ST0100'.
SET TITLEBAR 'TI_0100'.
DESCRIBE TABLE GT_FLIGHT LINES GV_LINES.
TC_0100-LINES = GV_LINES.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Module CANCEL INPUT
*&---------------------------------------------------------------------*
MODULE cancel INPUT.
LEAVE PROGRAM.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
ok_save = ok_code.
Clear ok_code.
case ok_save.
when 'BACK'.
LEAVE SCREEN.
WHEN 'CANCEL' OR 'CLOSE'.
LEAVE PROGRAM.
WHEN 'EDIT'.
PERFORM FRM_SET_EDIT.
WHEN 'ADD'.
PERFORM FRM_ADD_LINE.
WHEN 'DELETE'.
PERFORM FRM_DELETE_LINES.
WHEN 'SORT_UP'.
PERFORM FRM_SORT_UP.
WHEN 'SORT_DOWN'.
PERFORM FRM_SORT_DOWN.
WHEN OTHERS.
ENDCASE.
ENDMODULE.
*&---------------------------------------------------------------------*
*& Form FRM_SET_EDIT
*&---------------------------------------------------------------------*
FORM frm_set_edit .
LOOP AT TC_0100-COLS INTO GS_COLS.
IF GS_COLS-SCREEN-INPUT = 1.
GS_COLS-SCREEN-INPUT = 0.
ELSE.
GS_COLS-SCREEN-INPUT = 1.
ENDIF.
MODIFY TC_0100-COLS FROM GS_COLS.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_ADD_LINE
*&---------------------------------------------------------------------*
FORM frm_add_line .
DATA: LV_SELLINE TYPE SY-STEPL. "当前行号
DATA: LV_LINE TYPE I. "插入行的行号
* 获取当前行号
GET CURSOR LINE LV_SELLINE.
IF sy-subrc <> 0. "如果光标在表外,则在顶部新增一行
LV_SELLINE = 1.
ELSE. "如果光标在表中,但不在数据区,则在顶部新增一行
IF lv_selline > tc_0100-lines.
LV_SELLINE = 1.
ENDIF.
ENDIF.
* 往内表光标所在位置插入一行空白行
INSERT INITIAL LINE INTO gt_flight INDEX lv_selline.
* table control的总行号+1
tc_0100-lines = tc_0100-lines + 1.
* 如果光标不在数据区或者表控制上,则光标会定位在新插入的顶部的行上
IF lv_selline = 1.
set CURSOR LINE lv_line.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_DELETE_LINES
*&---------------------------------------------------------------------*
FORM frm_delete_lines .
READ TABLE gt_flight WITH key flag = 'X' TRANSPORTING NO FIELDS.
IF SY-SUBRC <> 0.
MESSAGE S001(ZSD) WITH '请选择要删除的行!' DISPLAY LIKE 'E'.
ELSE.
LOOP AT GT_FLIGHT INTO GS_FLIGHT WHERE FLAG = 'X'.
DELETE GT_FLIGHT INDEX SY-TABIX.
ENDLOOP.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SORT_UP
*&---------------------------------------------------------------------*
FORM frm_sort_up .
READ TABLE TC_0100-COLS INTO GS_COLS WITH KEY SELECTED = 'X'.
IF SY-SUBRC = 0.
SORT GT_FLIGHT STABLE BY (GS_COLS-SCREEN-NAME+10) ASCENDING.
GS_COLS-SELECTED = ''.
MODIFY TC_0100-COLS FROM GS_COLS INDEX SY-TABIX.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SORT_DOWN
*&---------------------------------------------------------------------*
FORM frm_sort_down .
READ TABLE TC_0100-COLS INTO GS_COLS WITH KEY SELECTED = 'X'.
IF SY-SUBRC = 0.
SORT GT_FLIGHT STABLE BY (GS_COLS-SCREEN-NAME+10) DESCENDING.
GS_COLS-SELECTED = ''.
MODIFY TC_0100-COLS FROM GS_COLS INDEX SY-TABIX.
ENDIF.
ENDFORM.