tables:itab.
TYPE-POOLS: slis. "alv
"ALV显示标准定义
DATA: g_repid LIKE sy-repid,
it_fieldcat TYPE lvc_t_fcat WITH HEADER LINE,
is_layout TYPE lvc_s_layo.
DATA: gwk_grid_setting LIKE lvc_s_glay. "标记选中列
CONSTANTS pf_status TYPE slis_formname VALUE ‘ALV_PF_STATUS‘. "定义状态
CONSTANTS user_command TYPE slis_formname VALUE ‘ALV_USER_COMMAND‘. "定义按钮
"以上的都是标准的定义,直接复制。
"定义内表
DATA : BEGIN OF wa_itab,
"alv显示字段
matnr TYPE zewmt026-matnr,
END OF wa_itab.
DATA lt_itab LIKE TABLE OF wa_itab.
"可以自己定义一个小的屏幕,也可以不需要 ,看自己需不需要
SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-901.
SELECTION-SCREEN FUNCTION KEY 1.
PARAMETERS : p1 TYPE char1 RADIOBUTTON GROUP g1 .
SELECT-OPTIONS:
s_a FOR table-a ,
s_b FOR table-b .
PARAMETERS : p2 TYPE char1 RADIOBUTTON GROUP g1.
SELECTION-SCREEN: END OF BLOCK b1.
"判断自己是否需要选择屏幕,根据选择条件显示的内容
PARAMETERS d LIKE table-d.
SELECT-OPTIONS:
a FOR table-a,
b FOR table-b.
INITIALIZATION. "加载前的事件,不一定会有。
"用FORM的形式可以避免整个程序出错,还可以更快找到不正确的地方。
START-OF-SELECTION.
PERFORM get_data. "获取数据
PERFORM alv_layout_build. "alv格式设置
PERFORM alv_fieldcat. "样式
PERFORM alv_show. "ALV显示
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_data ." 获取需要显示的数据
SELECT *
FROM table
INTO CORRESPONDING FIELDS OF TABLE lt_itab
WHERE a IN a
AND b IN b.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form ALV_LAYOUT_BUILD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM alv_layout_build ."自己可以添加需要的设置
is_layout-zebra = ‘X‘. "明亮显示
is_layout-box_fname = ‘SEL‘. "可选
is_layout-cwidth_opt = ‘X‘. "自动宽度
gwk_grid_setting-edt_cll_cb = ‘X‘."刷新
ENDFORM.
*&---------------------------------------------------------------------*
*& Form ALV_FIELDCAT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM alv_fieldcat .
CLEAR it_fieldcat.REFRESH it_fieldcat.
DEFINE lit_fieldcat. "自己可以添加需要的设置
it_fieldcat-tabname = ‘IT_OUT‘.
it_fieldcat-fieldname = &1. "字段名字
it_fieldcat-coltext = &2. "字段描述
it_fieldcat-just = &3. "对齐
it_fieldcat-edit = &4. "可以更改
" ls_fieldcat-decimals_out = &5. "去掉小数点后面的零填0
.
.
.
.
APPEND it_fieldcat.
CLEAR it_fieldcat.
END-OF-DEFINITION. "lit_fieldcat
lit_fieldcat :
‘MATNR‘ ‘物料编码‘ ‘L‘ ‘‘,
‘ZBTYPE‘ ‘箱型‘ ‘L‘ ‘‘,
‘ZCDATE‘ ‘创建日期‘ ‘L‘ ‘‘,
‘ZCTIME‘ ‘创建时间‘ ‘L‘ ‘‘,
‘ZCUSER‘ ‘创建用户‘ ‘L‘ ‘‘,
‘ZQTY‘ ‘申请数量‘ ‘L‘ ‘X‘,
‘MEINS‘ ‘ 单位‘ ‘L‘ ‘X‘.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form ALV_SHOW
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM alv_show . " 选择自己需要显示的内容。
g_repid = sy-repid.
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY_LVC‘
EXPORTING
* I_INTERFACE_CHECK = ‘ ‘
* I_BYPASSING_BUFFER =
* I_BUFFER_ACTIVE =
i_callback_program = g_repid "本程序
i_callback_pf_status_set = pf_status
i_callback_user_command = user_command
* I_CALLBACK_TOP_OF_PAGE = ‘ ‘
* I_CALLBACK_HTML_TOP_OF_PAGE = ‘ ‘
* I_CALLBACK_HTML_END_OF_LIST = ‘ ‘
* I_STRUCTURE_NAME =
* I_BACKGROUND_ID = ‘ ‘
* I_GRID_TITLE =
i_grid_settings = gwk_grid_setting "按钮后刷新
is_layout_lvc = is_layout
it_fieldcat_lvc = it_fieldcat[]
* IT_EXCLUDING =
* IT_SPECIAL_GROUPS_LVC =
* IT_SORT_LVC =
* IT_FILTER_LVC =
* IT_HYPERLINK =
* IS_SEL_HIDE =
* I_DEFAULT = ‘X‘
i_save = ‘A‘
* IS_VARIANT =
* IT_EVENTS =
* IT_EVENT_EXIT =
* IS_PRINT_LVC =
* IS_REPREP_ID_LVC =
* I_SCREEN_START_COLUMN = 0
* I_SCREEN_START_LINE = 0
* I_SCREEN_END_COLUMN = 0
* I_SCREEN_END_LINE = 0
* I_HTML_HEIGHT_TOP =
* I_HTML_HEIGHT_END =
* IT_ALV_GRAPHICS =
* IT_EXCEPT_QINFO_LVC =
* IR_SALV_FULLSCREEN_ADAPTER =
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER =
* ES_EXIT_CAUSED_BY_USER =
TABLES
t_outtab = lt_itab "数据存放表
EXCEPTIONS
program_error = 1 "异常
OTHERS = 2.
IF sy-subrc <> 0. "异常
* Implement suitable error handling here
ENDIF.
ENDFORM.
FORM alv_pf_status USING rt_extab TYPE slis_t_extab.
SET PF-STATUS‘、状态名、‘ EXCLUDING rt_extab. "用自己的GUI状态,不用的话就把关于他的都删掉,用标准的
ENDFORM.
FORM alv_user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
CASE r_ucomm.
WHEN ‘XX‘ .
WHEN ‘YY‘.
WHEN OTHERS.
ENDCASE.
rs_selfield-refresh = ‘X‘. " REFESH ALV ,ALV被修改时会自动刷新,没有修改不进行刷新
ENDFORM.
原文地址:https://www.cnblogs.com/go-online-me/p/11736142.html