ABAP的Table Control

    Table Control是ABAP提供的内表显示控件,可以动态的显示操作内表数据。

示例1:自动创建Table Control

    1、创建项目Z_TEST_TC;

    2、创建屏幕100,右键选择creat->Screen;

     3、输入描述,然后点击layout;

    4、拖动1:手动建立Table Tontrol;拖动2:自动建立Table Control,这里我们先自动创建。

       5、弹出Table Control的自动创建步骤,选择continue;

     6、输入Table Control名,点击Continue;

     7、选择是使用ABAP的Dictonary中table的数据结构还是我们当前Program自定义的内表结构,这里我们选择使用Dictornary的数据结构,SPFLI;

     8、选择要显示的列,选择好后点击Continue;

     9、设置Table Control是否可输入,还是只输出显示;

     10、选择是否有Scroll等功能,选择Dictionary,不可选择;

     11、设置完成,显示效果,选择保存,激活;

屏幕100的Flow logic,自动生成代码:

PROCESS BEFORE OUTPUT.
*&SPWIZARD: PBO FLOW LOGIC FOR TABLECONTROL 'TBC_100'
  MODULE TBC_100_INIT.
*&SPWIZARD: MODULE TBC_100_CHANGE_TC_ATTR.
*&SPWIZARD: MODULE TBC_100_CHANGE_COL_ATTR.
  LOOP AT   G_TBC_100_ITAB
       INTO G_TBC_100_WA
       WITH CONTROL TBC_100
       CURSOR TBC_100-CURRENT_LINE.
*&SPWIZARD:   MODULE TBC_100_CHANGE_FIELD_ATTR
    MODULE TBC_100_MOVE.
  ENDLOOP.

* MODULE STATUS_0100.
*
PROCESS AFTER INPUT.
*&SPWIZARD: PAI FLOW LOGIC FOR TABLECONTROL 'TBC_100'
  LOOP AT G_TBC_100_ITAB.
    CHAIN.
      FIELD SPFLI-CARRID.
      FIELD SPFLI-CONNID.
      FIELD SPFLI-COUNTRYFR.
      FIELD SPFLI-CITYFROM.
      FIELD SPFLI-AIRPFROM.
      FIELD SPFLI-COUNTRYTO.
      FIELD SPFLI-CITYTO.
      FIELD SPFLI-AIRPTO.
      FIELD SPFLI-FLTIME.
      MODULE TBC_100_MODIFY ON CHAIN-REQUEST.
    ENDCHAIN.
  ENDLOOP.

*&SPWIZARD: MODULE TBC_100_CHANGE_TC_ATTR.
*&SPWIZARD: MODULE TBC_100_CHANGE_COL_ATTR.
* MODULE USER_COMMAND_0100.

主程序Program,自动生成代码:

REPORT Z_TEST_TC.

***&SPWIZARD: DATA DECLARATION FOR TABLECONTROL 'TBC_100'
*&SPWIZARD: DEFINITION OF DDIC-TABLE
TABLES:   SPFLI.
*&SPWIZARD: TYPE FOR THE DATA OF TABLECONTROL 'TBC_100'
TYPES: BEGIN OF T_TBC_100,
         CARRID LIKE SPFLI-CARRID,
         CONNID LIKE SPFLI-CONNID,
         COUNTRYFR LIKE SPFLI-COUNTRYFR,
         CITYFROM LIKE SPFLI-CITYFROM,
         AIRPFROM LIKE SPFLI-AIRPFROM,
         COUNTRYTO LIKE SPFLI-COUNTRYTO,
         CITYTO LIKE SPFLI-CITYTO,
         AIRPTO LIKE SPFLI-AIRPTO,
         FLTIME LIKE SPFLI-FLTIME,
       END OF T_TBC_100.
*&SPWIZARD: INTERNAL TABLE FOR TABLECONTROL 'TBC_100'
DATA:     G_TBC_100_ITAB   TYPE T_TBC_100 OCCURS 0,
          G_TBC_100_WA     TYPE T_TBC_100. "work area
DATA:     G_TBC_100_COPIED.           "copy flag

*&SPWIZARD: DECLARATION OF TABLECONTROL 'TBC_100' ITSELF
CONTROLS: TBC_100 TYPE TABLEVIEW USING SCREEN 0100.
*&SPWIZARD: OUTPUT MODULE FOR TC 'TBC_100'. DO NOT CHANGE THIS LINE!
*&SPWIZARD: COPY DDIC-TABLE TO ITAB
MODULE TBC_100_INIT OUTPUT.
  IF G_TBC_100_COPIED IS INITIAL.
*&SPWIZARD: COPY DDIC-TABLE 'SPFLI'
*&SPWIZARD: INTO INTERNAL TABLE 'g_TBC_100_itab'
    SELECT * FROM SPFLI
       INTO CORRESPONDING FIELDS
       OF TABLE G_TBC_100_ITAB.
    G_TBC_100_COPIED = 'X'.
    REFRESH CONTROL 'TBC_100' FROM SCREEN '0100'.
  ENDIF.
ENDMODULE.

*&SPWIZARD: OUTPUT MODULE FOR TC 'TBC_100'. DO NOT CHANGE THIS LINE!
*&SPWIZARD: MOVE ITAB TO DYNPRO
MODULE TBC_100_MOVE OUTPUT.
  MOVE-CORRESPONDING G_TBC_100_WA TO SPFLI.
ENDMODULE.

*&SPWIZARD: INPUT MODULE FOR TC 'TBC_100'. DO NOT CHANGE THIS LINE!
*&SPWIZARD: MODIFY TABLE
MODULE TBC_100_MODIFY INPUT.
  MOVE-CORRESPONDING SPFLI TO G_TBC_100_WA.
  MODIFY G_TBC_100_ITAB
    FROM G_TBC_100_WA
    INDEX TBC_100-CURRENT_LINE.
ENDMODULE.

在Controls:语句添加调用屏幕100就可以看到显示Table Control

"调用屏幕
CALL SCREEN 100.

设置工具栏,PF-STATUS

屏幕100,Flow logic解除MODUL STATUS_0100这行注释

MODULE STATUS_0100.

主程序逻辑:

"设置工具栏
MODULE STATUS_0100 OUTPUT.
  SET PF-STATUS 'STA_100'.
ENDMODULE.

设置用户操作功能

屏幕100,Flow logic解除USER_COMMAND_0100这行注释

MODULE USER_COMMAND_0100.

主程序逻辑:

MODULE USER_COMMAND_0100 INPUT.
  "点击退出
  CASE SY-UCOMM.
    WHEN 'EXIT'.
      LEAVE PROGRAM.
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_0100  INPUT

示例2:选择Program自定义内表

    第7步选择Internal program table,设置Program中自定义的内表。这里我们就是使用主程序中的自定义内表t_spfli。

主程序逻辑:

*&---------------------------------------------------------------------*
*&
*&表格控制器(自动创建)
*&---------------------------------------------------------------------*
DATA: OK_CODE TYPE SY-UCOMM.
"两种写法类似,OO中不能使用occurs
DATA:t_spfli LIKE TABLE OF spfli WITH HEADER LINE.
*DATA:t_spfli LIKE spfli OCCURS 0 WITH HEADER LINE.

"查询数据
SELECT * INTO CORRESPONDING FIELDS OF TABLE t_spfli FROM spfli.

"调用屏幕
CALL SCREEN 100.

"设置工具栏
MODULE STATUS_0100 OUTPUT.
  SET PF-STATUS 'STATUS_100'.
ENDMODULE.

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
  OK_CODE = SY-UCOMM.
  "点击退出
  CASE OK_CODE.
    WHEN 'CANCEL'.
      LEAVE PROGRAM.
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_0100  INPUT

"定义的表格控制器TCL_100
*&SPWIZARD: DECLARATION OF TABLECONTROL 'TCL_100' ITSELF
CONTROLS: TCL_100 TYPE TABLEVIEW USING SCREEN 0100.

"内表数据更新,更新显示表
*&SPWIZARD: OUTPUT MODULE FOR TC 'TCL_100'. DO NOT CHANGE THIS LINE!
*&SPWIZARD: UPDATE LINES FOR EQUIVALENT SCROLLBAR
MODULE TCL_100_CHANGE_TC_ATTR OUTPUT.
  DESCRIBE TABLE T_SPFLI LINES TCL_100-lines.
ENDMODULE.

"显示表被编辑,将更新内表数据
*&SPWIZARD: INPUT MODULE FOR TC 'TCL_100'. DO NOT CHANGE THIS LINE!
*&SPWIZARD: MODIFY TABLE
MODULE TCL_100_MODIFY INPUT.
  MODIFY T_SPFLI
    INDEX TCL_100-CURRENT_LINE.
ENDMODULE.

屏幕100的Flow logic程序逻辑:

PROCESS BEFORE OUTPUT.
*&SPWIZARD: PBO FLOW LOGIC FOR TABLECONTROL 'TCL_100'
  MODULE TCL_100_CHANGE_TC_ATTR.
*&SPWIZARD: MODULE TCL_100_CHANGE_COL_ATTR. LOOP AT T_SPFLI WITH CONTROL TCL_100 CURSOR TCL_100-CURRENT_LINE. *&SPWIZARD: MODULE TCL_100_CHANGE_FIELD_ATTR ENDLOOP. MODULE STATUS_0100. PROCESS AFTER INPUT. *&SPWIZARD: PAI FLOW LOGIC FOR TABLECONTROL 'TCL_100' LOOP AT T_SPFLI. CHAIN. FIELD T_SPFLI-CARRID. FIELD T_SPFLI-CONNID. FIELD T_SPFLI-COUNTRYFR. FIELD T_SPFLI-CITYFROM. FIELD T_SPFLI-AIRPFROM. FIELD T_SPFLI-COUNTRYTO. FIELD T_SPFLI-CITYTO. FIELD T_SPFLI-AIRPTO. FIELD T_SPFLI-FLTIME. FIELD T_SPFLI-DEPTIME. FIELD T_SPFLI-ARRTIME. FIELD T_SPFLI-DISTANCE. FIELD T_SPFLI-DISTID. FIELD T_SPFLI-FLTYPE. FIELD T_SPFLI-PERIOD. MODULE TCL_100_MODIFY ON CHAIN-REQUEST. endchain. ENDLOOP. *&SPWIZARD: MODULE TCL_100_CHANGE_TC_ATTR. *&SPWIZARD: MODULE TCL_100_CHANGE_COL_ATTR. MODULE USER_COMMAND_0100.

示例3:手动创建Table Control

 拖选Table Control控件,输入Table Control名,这里TBC;Separators:设置分割线;Line Sel:设置行选择,单行或多行;Column Sel:设置列选择,单列或多列;这里直接选择拖动第1个,创建Table Control。

 拖动2,设置列栏位,拖动1设置列名;

屏幕100的Flow logic逻辑代码:

PROCESS BEFORE OUTPUT.
  MODULE STATUS_0100.
  LOOP WITH CONTROL TBL1.
    MODULE FILLTAB.
  ENDLOOP.

*
PROCESS AFTER INPUT.
  MODULE USER_COMMAND_0100.

  LOOP WITH CONTROL TBL1.
    MODULE UPDATETAB.
  ENDLOOP.

主程序逻辑代码:

DATA: OK_CODE TYPE SY-UCOMM.
"定义的表格控制器TBL1
CONTROLS: TBL1 TYPE TABLEVIEW USING SCREEN 0100.
"表格单元个控制
DATA:WA_COLS LIKE LINE OF TBL1-COLS.

"定义table显示的字段
DATA:BEGIN OF t_spfli OCCURS 0 ,
   carrid LIKE spfli-CARRID,
   cityfrom LIKE spfli-cityfrom,
   cityto LIKE spfli-cityto,
   fltype LIKE spfli-fltype,
END OF t_spfli.

"屏幕的字段Name就是 WA_SPFLI-内表结构名
DATA:wa_spfli LIKE LINE OF  t_spfli.
"手动创建没有垂直下拉条,需要NUMS参数设置行数
DATA:NUMS TYPE I.

"查询数据
SELECT * INTO CORRESPONDING FIELDS OF TABLE t_spfli FROM spfli.

"调用屏幕
CALL SCREEN 100.

MODULE STATUS_0100 OUTPUT.
  SET PF-STATUS 'STATUS_100'.
  "设置垂直下拉条
  IF NUMS = 0.
    DESCRIBE TABLE t_spfli LINES NUMS.
    TBL1-LINES = NUMS.
  ENDIF.
ENDMODULE.

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
  OK_CODE = SY-UCOMM.
  "点击退出
  CASE OK_CODE.
    WHEN 'CANCEL'.
      LEAVE PROGRAM.
    WHEN 'NOW'.
      PERFORM current_line.
    WHEN 'EDIT'.
      PERFORM change_rw.
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_0100  INPUT

*&---------------------------------------------------------------------*
*&      Module  FILLTAB  OUTPUT
*&---------------------------------------------------------------------*
* 将内表数据填充到显示屏幕,屏幕显示之前执行module,
*----------------------------------------------------------------------*
MODULE FILLTAB OUTPUT.
  READ TABLE t_spfli INTO wa_spfli INDEX TBL1-CURRENT_LINE.
ENDMODULE.                 " FILLTAB  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  UPDATETAB  INPUT
*&---------------------------------------------------------------------*
*       将显示区域表更新到内表
*----------------------------------------------------------------------*
MODULE UPDATETAB INPUT.
  MODIFY t_spfli FROM wa_spfli INDEX TBL1-CURRENT_LINE.
ENDMODULE.                 " UPDATETAB  INPUT

*&---------------------------------------------------------------------*
*&      Form  CURRENT_LINE
*&---------------------------------------------------------------------*
*       获取当前行
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM CURRENT_LINE .
  DATA:LINEA TYPE I.
  "获取鼠标所在行
  GET CURSOR LINE LINEA.
  CHECK SY-SUBRC = 0.
*  LINEA = TBL1-TOP_LINE - 1.
  MESSAGE s006(ymess) WITH '当前行:' LINEA.
ENDFORM.                    " CURRENT_LINE

*&---------------------------------------------------------------------*
*&      Form  CHANGE_RW
*&---------------------------------------------------------------------*
*     修改表的读写设置
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM CHANGE_RW .
  "屏幕控制数据结构
  DATA:temp_screen LIKE WA_COLS-SCREEN.
  LOOP AT TBL1-COLS INTO WA_COLS.
    "SCREEN-INPUT:1能够输入,0不能输入
    "SCREEN-NAME:字段名
    IF WA_COLS-SCREEN-INPUT = '1'.
       WA_COLS-SCREEN-INPUT = '0'.
    ELSE.
      WA_COLS-SCREEN-INPUT = '1'.
    ENDIF.

    "将指定列隐藏
    "WA_COLS-INVISIBLE :X 为隐藏,0显示
*    IF WA_COLS-SCREEN-NAME = 'WA_SPFLI-FLTYPE'.
*      WA_COLS-INVISIBLE = 'X'.
*    ENDIF.
"通过设置可见长度设置列隐藏 IF WA_COLS-SCREEN-NAME = 'WA_SPFLI-FLTYPE'. "设置vislength为0,实现隐藏;要显示设置显示字段长度即可 WA_COLS-VISLENGTH = 0. ENDIF. MODIFY TBL1-COLS FROM WA_COLS INDEX SY-TABIX. ENDLOOP. ENDFORM. " CHANGE_RW
12-20 18:57