实现了Table Control的主要的一些功能,可以作为例子参考,实现的功能有是否可编辑切换,选择某一条记录点击按钮显示详细信息,新增记录,删除记录,选择所有记录,选择光标所有记录,取消选择所有,排序,行选择栏位,列不可编辑,固定列,Table Control标题,分页功能,根据输入A字段的值显示B字段的值,某字段的值如果等于XXX就不可编辑等功能。相信这些功能已经可以满足大部份的开发了。
主程序代码:
DATA: ok_code TYPE sy-ucomm,
save_ok TYPE sy-ucomm.
DATA: l_field TYPE char50,
l_line TYPE i.
TYPES: BEGIN OF ty_spfli,
carrid TYPE spfli-carrid,
connid TYPE spfli-connid,
countryfr TYPE spfli-countryfr,
cityfrom TYPE spfli-cityfrom,
airpfrom TYPE spfli-airpfrom,
mask(1),
END OF ty_spfli.
DATA g_lines TYPE i.
DATA sp2 TYPE TABLE OF ty_spfli WITH HEADER LINE.
DATA sp1 TYPE ty_spfli.
SELECT * INTO CORRESPONDING FIELDS OF TABLE sp2 FROM spfli.
CONTROLS content TYPE TABLEVIEW USING SCREEN 100.
content-top_line = 1.
content-lines = LINES( sp2 ).
CALL SCREEN 100.
LOOP AT sp2.
WRITE: / sp2-carrid,sp2-connid,sp2-countryfr,sp2-cityfrom.
ENDLOOP.
*----------------------------------------------------------------------*
* MODULE status_0100 OUTPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS 'STA'.
ENDMODULE. "status_0100 OUTPUT
*----------------------------------------------------------------------*
* MODULE user_command_0100 INPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
save_ok = ok_code.
CLEAR ok_code.
CASE save_ok.
WHEN 'EXIT'.
LEAVE TO SCREEN 0.
WHEN 'SWITH'.
PERFORM f_switch.
WHEN 'SORT_UP'.
PERFORM f_sort USING 'UP'.
WHEN 'SORT_DOWN'.
PERFORM f_sort USING 'DOWN'.
WHEN 'DETAIL'.
PERFORM f_detail.
WHEN 'P--'.
PERFORM f_page USING save_ok.
WHEN 'P-'.
PERFORM f_page USING save_ok.
WHEN 'P+'.
PERFORM f_page USING save_ok.
WHEN 'P++'.
PERFORM f_page USING save_ok.
WHEN 'SELECT'.
PERFORM f_select USING 'SELECT'.
WHEN 'BLOCK'.
PERFORM f_select USING 'BLOCK'.
WHEN 'DESELECT'.
PERFORM f_select USING 'DESELECT'.
WHEN 'INSERT'.
PERFORM f_row USING 'INSERT'.
WHEN 'DELETE'.
PERFORM f_row USING 'DELETE'.
ENDCASE.
ENDMODULE. "user_command_0100 INPUT
*----------------------------------------------------------------------*
* MODULE filltab OUTPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE filltab OUTPUT.
READ TABLE sp2 INTO sp1 INDEX content-current_line.
ENDMODULE. "filltab OUTPUT
*----------------------------------------------------------------------*
* MODULE readtab INPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE readtab INPUT.
g_lines = sy-loopc.
MODIFY sp2 FROM sp1 INDEX content-current_line.
ENDMODULE. "readtab INPUT
*----------------------------------------------------------------------*
* MODULE chang_val INPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE chang_val INPUT.
DATA lwa_cols LIKE LINE OF content-cols.
LOOP AT content-cols INTO lwa_cols.
IF lwa_cols-screen-name = 'SP1-COUNTRYFR'
AND sp1-countryfr = 'AA'.
sp1-cityfrom = 'SDSDSD'.
MODIFY sp2 FROM sp1 INDEX content-current_line.
ENDIF.
ENDLOOP.
ENDMODULE. "chang_val INPUT
*----------------------------------------------------------------------*
* MODULE get_cursor INPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE get_cursor INPUT.
GET CURSOR FIELD l_field LINE l_line.
ENDMODULE. "get_cursor INPUT
*----------------------------------------------------------------------*
* MODULE set_cursor OUTPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE set_cursor OUTPUT.
SET CURSOR FIELD l_field LINE l_line.
ENDMODULE. "set_cursor OUTPUT
*----------------------------------------------------------------------*
* MODULE SET_EDITABLE
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE set_editable OUTPUT.
LOOP AT SCREEN.
IF screen-name = 'SP1-CITYFROM'.
IF sp1-cityfrom = 'SDSDSD'.
screen-input = 0.
MODIFY SCREEN.
ENDIF.
ENDIF.
ENDLOOP.
ENDMODULE. "SET_EDITABLE
*&---------------------------------------------------------------------*
*& Form f_page
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->CODE text
*----------------------------------------------------------------------*
FORM f_page USING code TYPE sy-ucomm.
DATA: i TYPE i,
j TYPE i.
CASE code.
WHEN 'P--'.
content-top_line = 1.
WHEN 'P-'.
content-top_line = content-top_line - g_lines.
IF content-top_line <= 0.
content-top_line = 1.
ENDIF.
WHEN 'P+'.
i = content-top_line + g_lines.
j = content-lines - g_lines + 1.
IF j <= 0.
j = 1.
ENDIF.
IF i <= j.
content-top_line = i.
ELSE.
content-top_line = j.
ENDIF.
WHEN 'P++'.
content-top_line = content-lines - g_lines + 1.
IF content-top_line <= 0.
content-top_line = 1.
ENDIF.
ENDCASE.
ENDFORM. "f_page
*&---------------------------------------------------------------------*
*& Form f_switch
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM f_switch.
DATA lwa_cols LIKE LINE OF content-cols.
LOOP AT content-cols INTO lwa_cols WHERE index > 2.
IF lwa_cols-screen-input = 0.
lwa_cols-screen-input = 1.
ELSE.
lwa_cols-screen-input = 0.
ENDIF.
MODIFY content-cols FROM lwa_cols INDEX sy-tabix.
ENDLOOP.
ENDFORM. "f_switch
*&---------------------------------------------------------------------*
*& Form F_SORT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->CODE text
*----------------------------------------------------------------------*
FORM f_sort USING code.
DATA: str1 TYPE string,
str2 TYPE string,
lwa_cols LIKE LINE OF content-cols.
READ TABLE content-cols INTO lwa_cols WITH KEY selected = 'X'.
IF sy-subrc = 0.
SPLIT lwa_cols-screen-name AT '-' INTO str1 str2.
IF code = 'UP'.
SORT sp2 STABLE BY (str2) ASCENDING.
ELSEIF code = 'DOWN'.
SORT sp2 STABLE BY (str2) DESCENDING.
ENDIF.
lwa_cols-selected = ''.
MODIFY content-cols FROM lwa_cols TRANSPORTING selected WHERE selected = 'X'.
ENDIF.
ENDFORM. "F_SORT
*&---------------------------------------------------------------------*
*& Form f_detail
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM f_detail.
READ TABLE sp2 INTO sp1 WITH KEY mask = 'X'.
IF sy-subrc = 0 AND sp1-mask = 'X'.
CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'
EXPORTING
textline1 = sp1-carrid
textline2 = sp1-connid.
ENDIF.
ENDFORM. "f_detail
*&---------------------------------------------------------------------*
*& Form f_select
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->CODE text
*----------------------------------------------------------------------*
FORM f_select USING code.
CASE code.
WHEN 'SELECT'.
sp1-mask = 'X'.
MODIFY sp2 FROM sp1 TRANSPORTING mask WHERE mask IS INITIAL.
WHEN 'BLOCK'.
sp1-mask = 'X'.
MODIFY sp2 FROM sp1 INDEX l_line TRANSPORTING mask.
WHEN 'DESELECT'.
sp1-mask = ''.
MODIFY sp2 FROM sp1 TRANSPORTING mask WHERE mask = 'X'.
ENDCASE.
ENDFORM. "f_select
*&---------------------------------------------------------------------*
*& Form F_ROW
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->CODE text
*----------------------------------------------------------------------*
FORM f_row USING code.
DATA lwa_cols LIKE LINE OF content-cols.
DATA: l_int TYPE i,
lt_cols LIKE TABLE OF l_int.
CASE code.
WHEN 'INSERT'.
INSERT INITIAL LINE INTO sp2 INDEX l_line.
WHEN 'DELETE'.
DELETE sp2 WHERE mask = 'X'.
ENDCASE.
ENDFORM. "F_ROW
屏幕100的代码:
PROCESS BEFORE OUTPUT.
MODULE status_0100.
LOOP WITH CONTROL content.
MODULE filltab.
MODULE set_editable.
ENDLOOP.
MODULE set_cursor.
PROCESS AFTER INPUT.
LOOP WITH CONTROL content.
MODULE readtab.
MODULE chang_val.
ENDLOOP.
MODULE get_cursor.
MODULE user_command_0100.
主程序代码:
DATA: ok_code TYPE sy-ucomm,
save_ok TYPE sy-ucomm.
DATA: l_field TYPE char50,
l_line TYPE i.
TYPES: BEGIN OF ty_spfli,
carrid TYPE spfli-carrid,
connid TYPE spfli-connid,
countryfr TYPE spfli-countryfr,
cityfrom TYPE spfli-cityfrom,
airpfrom TYPE spfli-airpfrom,
mask(1),
END OF ty_spfli.
DATA g_lines TYPE i.
DATA sp2 TYPE TABLE OF ty_spfli WITH HEADER LINE.
DATA sp1 TYPE ty_spfli.
SELECT * INTO CORRESPONDING FIELDS OF TABLE sp2 FROM spfli.
CONTROLS content TYPE TABLEVIEW USING SCREEN 100.
content-top_line = 1.
content-lines = LINES( sp2 ).
CALL SCREEN 100.
LOOP AT sp2.
WRITE: / sp2-carrid,sp2-connid,sp2-countryfr,sp2-cityfrom.
ENDLOOP.
*----------------------------------------------------------------------*
* MODULE status_0100 OUTPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS 'STA'.
ENDMODULE. "status_0100 OUTPUT
*----------------------------------------------------------------------*
* MODULE user_command_0100 INPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
save_ok = ok_code.
CLEAR ok_code.
CASE save_ok.
WHEN 'EXIT'.
LEAVE TO SCREEN 0.
WHEN 'SWITH'.
PERFORM f_switch.
WHEN 'SORT_UP'.
PERFORM f_sort USING 'UP'.
WHEN 'SORT_DOWN'.
PERFORM f_sort USING 'DOWN'.
WHEN 'DETAIL'.
PERFORM f_detail.
WHEN 'P--'.
PERFORM f_page USING save_ok.
WHEN 'P-'.
PERFORM f_page USING save_ok.
WHEN 'P+'.
PERFORM f_page USING save_ok.
WHEN 'P++'.
PERFORM f_page USING save_ok.
WHEN 'SELECT'.
PERFORM f_select USING 'SELECT'.
WHEN 'BLOCK'.
PERFORM f_select USING 'BLOCK'.
WHEN 'DESELECT'.
PERFORM f_select USING 'DESELECT'.
WHEN 'INSERT'.
PERFORM f_row USING 'INSERT'.
WHEN 'DELETE'.
PERFORM f_row USING 'DELETE'.
ENDCASE.
ENDMODULE. "user_command_0100 INPUT
*----------------------------------------------------------------------*
* MODULE filltab OUTPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE filltab OUTPUT.
READ TABLE sp2 INTO sp1 INDEX content-current_line.
ENDMODULE. "filltab OUTPUT
*----------------------------------------------------------------------*
* MODULE readtab INPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE readtab INPUT.
g_lines = sy-loopc.
MODIFY sp2 FROM sp1 INDEX content-current_line.
ENDMODULE. "readtab INPUT
*----------------------------------------------------------------------*
* MODULE chang_val INPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE chang_val INPUT.
DATA lwa_cols LIKE LINE OF content-cols.
LOOP AT content-cols INTO lwa_cols.
IF lwa_cols-screen-name = 'SP1-COUNTRYFR'
AND sp1-countryfr = 'AA'.
sp1-cityfrom = 'SDSDSD'.
MODIFY sp2 FROM sp1 INDEX content-current_line.
ENDIF.
ENDLOOP.
ENDMODULE. "chang_val INPUT
*----------------------------------------------------------------------*
* MODULE get_cursor INPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE get_cursor INPUT.
GET CURSOR FIELD l_field LINE l_line.
ENDMODULE. "get_cursor INPUT
*----------------------------------------------------------------------*
* MODULE set_cursor OUTPUT
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE set_cursor OUTPUT.
SET CURSOR FIELD l_field LINE l_line.
ENDMODULE. "set_cursor OUTPUT
*----------------------------------------------------------------------*
* MODULE SET_EDITABLE
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
MODULE set_editable OUTPUT.
LOOP AT SCREEN.
IF screen-name = 'SP1-CITYFROM'.
IF sp1-cityfrom = 'SDSDSD'.
screen-input = 0.
MODIFY SCREEN.
ENDIF.
ENDIF.
ENDLOOP.
ENDMODULE. "SET_EDITABLE
*&---------------------------------------------------------------------*
*& Form f_page
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->CODE text
*----------------------------------------------------------------------*
FORM f_page USING code TYPE sy-ucomm.
DATA: i TYPE i,
j TYPE i.
CASE code.
WHEN 'P--'.
content-top_line = 1.
WHEN 'P-'.
content-top_line = content-top_line - g_lines.
IF content-top_line <= 0.
content-top_line = 1.
ENDIF.
WHEN 'P+'.
i = content-top_line + g_lines.
j = content-lines - g_lines + 1.
IF j <= 0.
j = 1.
ENDIF.
IF i <= j.
content-top_line = i.
ELSE.
content-top_line = j.
ENDIF.
WHEN 'P++'.
content-top_line = content-lines - g_lines + 1.
IF content-top_line <= 0.
content-top_line = 1.
ENDIF.
ENDCASE.
ENDFORM. "f_page
*&---------------------------------------------------------------------*
*& Form f_switch
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM f_switch.
DATA lwa_cols LIKE LINE OF content-cols.
LOOP AT content-cols INTO lwa_cols WHERE index > 2.
IF lwa_cols-screen-input = 0.
lwa_cols-screen-input = 1.
ELSE.
lwa_cols-screen-input = 0.
ENDIF.
MODIFY content-cols FROM lwa_cols INDEX sy-tabix.
ENDLOOP.
ENDFORM. "f_switch
*&---------------------------------------------------------------------*
*& Form F_SORT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->CODE text
*----------------------------------------------------------------------*
FORM f_sort USING code.
DATA: str1 TYPE string,
str2 TYPE string,
lwa_cols LIKE LINE OF content-cols.
READ TABLE content-cols INTO lwa_cols WITH KEY selected = 'X'.
IF sy-subrc = 0.
SPLIT lwa_cols-screen-name AT '-' INTO str1 str2.
IF code = 'UP'.
SORT sp2 STABLE BY (str2) ASCENDING.
ELSEIF code = 'DOWN'.
SORT sp2 STABLE BY (str2) DESCENDING.
ENDIF.
lwa_cols-selected = ''.
MODIFY content-cols FROM lwa_cols TRANSPORTING selected WHERE selected = 'X'.
ENDIF.
ENDFORM. "F_SORT
*&---------------------------------------------------------------------*
*& Form f_detail
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM f_detail.
READ TABLE sp2 INTO sp1 WITH KEY mask = 'X'.
IF sy-subrc = 0 AND sp1-mask = 'X'.
CALL FUNCTION 'POPUP_TO_DISPLAY_TEXT'
EXPORTING
textline1 = sp1-carrid
textline2 = sp1-connid.
ENDIF.
ENDFORM. "f_detail
*&---------------------------------------------------------------------*
*& Form f_select
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->CODE text
*----------------------------------------------------------------------*
FORM f_select USING code.
CASE code.
WHEN 'SELECT'.
sp1-mask = 'X'.
MODIFY sp2 FROM sp1 TRANSPORTING mask WHERE mask IS INITIAL.
WHEN 'BLOCK'.
sp1-mask = 'X'.
MODIFY sp2 FROM sp1 INDEX l_line TRANSPORTING mask.
WHEN 'DESELECT'.
sp1-mask = ''.
MODIFY sp2 FROM sp1 TRANSPORTING mask WHERE mask = 'X'.
ENDCASE.
ENDFORM. "f_select
*&---------------------------------------------------------------------*
*& Form F_ROW
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->CODE text
*----------------------------------------------------------------------*
FORM f_row USING code.
DATA lwa_cols LIKE LINE OF content-cols.
DATA: l_int TYPE i,
lt_cols LIKE TABLE OF l_int.
CASE code.
WHEN 'INSERT'.
INSERT INITIAL LINE INTO sp2 INDEX l_line.
WHEN 'DELETE'.
DELETE sp2 WHERE mask = 'X'.
ENDCASE.
ENDFORM. "F_ROW
屏幕100的代码:
PROCESS BEFORE OUTPUT.
MODULE status_0100.
LOOP WITH CONTROL content.
MODULE filltab.
MODULE set_editable.
ENDLOOP.
MODULE set_cursor.
PROCESS AFTER INPUT.
LOOP WITH CONTROL content.
MODULE readtab.
MODULE chang_val.
ENDLOOP.
MODULE get_cursor.
MODULE user_command_0100.