代码语言:javascript复制
DATA: gv_repid TYPE sy-repid,
gv_sname TYPE sy-dynnr.
TYPES: BEGIN OF gs_data.
INCLUDE STRUCTURE spfli.
TYPES: celtab TYPE lvc_t_styl.
TYPES: END OF gs_data.
DATA: gs_data1 TYPE gs_data.
DATA: gt_data TYPE TABLE OF gs_data WITH HEADER LINE.
DATA: g_con01 TYPE REF TO cl_gui_custom_container.
DATA: g_grid01 TYPE REF TO cl_gui_alv_grid.
DATA: gs_fieldcat TYPE lvc_s_fcat.
DATA: gt_fieldcat TYPE lvc_t_fcat.
DATA: gs_style TYPE lvc_s_styl,
gt_style TYPE lvc_t_styl.
DATA: ok_code TYPE sy-ucomm,
save_ok TYPE sy-ucomm.
CONSTANTS: g_cus01 TYPE scrfname VALUE 'CUS01'.
START-OF-SELECTION.
PERFORM get_data.
PERFORM edit.
CALL SCREEN 100.
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
* text 获取数据
*----------------------------------------------------------------------*
FORM get_data.
SELECT *
INTO CORRESPONDING FIELDS OF TABLE gt_data
UP TO 30 ROWS
FROM spfli.
ENDFORM. "GET_DATA
*&---------------------------------------------------------------------*
*& Form SET_FIELDCAT
*&---------------------------------------------------------------------*
* text ALV显示字段文本
*----------------------------------------------------------------------*
FORM set_fieldcat.
CLEAR: gt_fieldcat[].
DEFINE macro_fieldcat.
CLEAR gs_fieldcat.
gs_fieldcat-fieldname = &1.
gs_fieldcat-coltext = &2.
gs_fieldcat-hotspot = &3.
APPEND gs_fieldcat TO gt_fieldcat.
END-OF-DEFINITION.
macro_fieldcat 'CARRID' '航线代码' 'X'.
macro_fieldcat 'CONNID' '航班连接Id' ''.
macro_fieldcat 'COUNTRYFR' '起飞国家' ''.
macro_fieldcat 'CITYFROM' '起飞城市' ''.
macro_fieldcat 'AIRPFROM' '起飞机场' ''.
macro_fieldcat 'COUNTRYTO' '目标国家' ''.
macro_fieldcat 'CITYTO' '目标城市' ''.
macro_fieldcat 'AIRPTO' '目标机场' ''.
macro_fieldcat 'FLTIME' '航班时间' ''.
macro_fieldcat 'DEPTIME' '启程时间' ''.
macro_fieldcat 'ARRTIME' '到达时间' ''.
macro_fieldcat 'DISTANCE' '距离' ''.
ENDFORM. "SET_FIELDCAT
*&---------------------------------------------------------------------*
*& Form SET_LAYOUT
*&---------------------------------------------------------------------*
* text 布局
*----------------------------------------------------------------------*
* -->P_LAYOUT text
*----------------------------------------------------------------------*
FORM set_layout CHANGING p_layout TYPE lvc_s_layo.
p_layout-cwidth_opt = 'X'. " 自动调节字段长度
p_layout-sel_mode = 'D'. " 选择模式,可选择多行
p_layout-edit = 'X'.
p_layout-stylefname = 'CELTAB'.
ENDFORM. "SET_LAYOUT
*&---------------------------------------------------------------------*
*& Module INIT_CON OUTPUT
*&---------------------------------------------------------------------*
* text 创建ALV
*----------------------------------------------------------------------*
MODULE init_con OUTPUT.
DATA: ls_layout TYPE lvc_s_layo.
IF g_con01 IS INITIAL.
PERFORM set_fieldcat.
PERFORM set_layout CHANGING ls_layout.
" 创建SAP容器实例
CREATE OBJECT g_con01
EXPORTING
container_name = g_cus01 " 自定义控件名称
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5
OTHERS = 6.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
" 创建GRID实例
CREATE OBJECT g_grid01
EXPORTING
i_parent = g_con01
EXCEPTIONS
error_cntl_create = 1
error_cntl_init = 2
error_cntl_link = 3
error_dp_create = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
" 调用ALV 显示方法
CALL METHOD g_grid01->set_table_for_first_display
EXPORTING
* i_structure_name = 'SPFLI' " 参照表结构字段显示
is_layout = ls_layout " 布局
CHANGING
it_fieldcatalog = gt_fieldcat " 显示字段
it_outtab = gt_data[]
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDIF.
ENDMODULE. " INIT_CON OUTPUT
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS 'S0001'.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_EXIT_0100 INPUT
*&---------------------------------------------------------------------*
*----------------------------------------------------------------------*
MODULE user_exit_0100 INPUT.
save_ok = ok_code.
CLEAR ok_code.
CASE save_ok.
WHEN 'BACK'.
LEAVE PROGRAM.
ENDCASE.
ENDMODULE. " USER_EXIT_0100 INPUT
FORM edit.
DATA: l_index TYPE i.
LOOP AT gt_data INTO gs_data1.
CLEAR gs_data1-celtab.
l_index = sy-tabix.
REFRESH gt_style.
IF gs_data1-carrid = 'AA'.
gs_style-fieldname = 'CONNID'.
gs_style-style = cl_gui_alv_grid=>mc_style_disabled.
INSERT gs_style INTO TABLE gt_style.
ELSE.
gs_style-fieldname = 'CONNID'.
gs_style-style = cl_gui_alv_grid=>mc_style_enabled.
INSERT gs_style INTO TABLE gt_style.
ENDIF.
INSERT LINES OF gt_style INTO TABLE gs_data1-celtab.
MODIFY gt_data FROM gs_data1.
ENDLOOP.
ENDFORM.