现在的项目任务排的都很满,节奏很快。自己备份的代码东一份,西一份的,找的费劲。这次给自己留一个LVC ALV的例子。效果图如下:
类型、全局变量、选择屏幕定义:
TYPES: BEGIN OF ts_result,
sel(1), "选择框
zconfstat(1), "交通灯
num TYPE wcocoh-num,
ext_num TYPE wcocoh-ext_num,
contract_type TYPE wcocoh-contract_type,
ct_text TYPE twcbcontrtypet-text,
category TYPE ztmm_rebatepool-category,
cat_text TYPE twcbcategoryt-text,
vend_owner TYPE wcocoh-vend_owner,
name_org1 TYPE but000-name_org1,
bukrs TYPE t001-bukrs,
ekgrp TYPE wcocoh-ekgrp,
date_from TYPE wcocoh-date_from,
date_to TYPE wcocoh-date_to,
zsrebacre TYPE ztmm_rebatepool-kwert,
zsrebconf TYPE ztmm_rebatepool-zrebconf,
zsrebrec TYPE ztmm_rebaterec-zrebrec,
cc_curr TYPE wcocoh-cc_curr,
reference TYPE wcocoh-reference,
assignment TYPE wcocoh-assignment,
zterm TYPE wcocoh-zterm,
text1 TYPE t052u-text1,
zlsch TYPE wcocoh-zlsch,
text2 TYPE t042zt-text2,
created_by TYPE wcocoh-created_by,
name_text TYPE char40,
created_on TYPE wcocoh-created_on,
ext_ref_cat TYPE wcocoh-ext_ref_cat,
ext_ref TYPE wcocoh-ext_ref,
END OF ts_result.
DATA:
gt_bukrs_rg TYPE RANGE OF wcocoh-bukrs,
gt_ekgrp_rg TYPE RANGE OF wcocoh-ekgrp.
DATA: gt_alv1_data TYPE TABLE OF ts_result.
**& 选择屏幕定义
TABLES: wcocoh.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
SELECT-OPTIONS:
s_num FOR wcocoh-num , "条件合同编号
s_contyp FOR wcocoh-contract_type, "条件合同类型
s_categ FOR wcocoh-category , "条件合同类别
s_bukrs FOR wcocoh-bukrs , "公司代码
s_vendow FOR wcocoh-vend_owner , "返利协议供应商
s_ekgrp FOR wcocoh-ekgrp. "采购组
SELECTION-SCREEN END OF BLOCK b1.
子程序的定义:
*&---------------------------------------------------------------------*
*& 包含 ZMMR032_FRM
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form frm_initialization
*&---------------------------------------------------------------------*
*& 公司代码-默认值
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_initialization .
DATA: lv_bukrs TYPE bseg-bukrs.
" 读取用户参数
GET PARAMETER ID 'BUK' FIELD lv_bukrs.
IF lv_bukrs IS NOT INITIAL.
APPEND VALUE #( sign = 'I' option = 'EQ' low = lv_bukrs ) TO s_bukrs.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_check_auth
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_check_auth .
REFRESH: gt_bukrs_rg,
gt_ekgrp_rg.
**& 获得有权限的公司代码信息
SELECT * FROM t001 INTO TABLE @DATA(lt_t001) WHERE bukrs IN @s_bukrs.
LOOP AT lt_t001 INTO DATA(ls_t001).
AUTHORITY-CHECK OBJECT 'M_MATE_BUK'
ID 'ACTVT' DUMMY
ID 'BUKRS' FIELD ls_t001-bukrs.
IF sy-subrc = 0.
APPEND VALUE #( sign = 'I' option = 'EQ' low = ls_t001-bukrs ) TO gt_bukrs_rg.
ENDIF.
ENDLOOP.
**& 获得有权限的采购组信息
SELECT * FROM t024 INTO TABLE @DATA(lt_t024) WHERE ekgrp IN @s_ekgrp.
LOOP AT lt_t024 INTO DATA(ls_t024).
AUTHORITY-CHECK OBJECT 'M_BEST_EKG'
ID 'ACTVT' DUMMY
ID 'EKGRP' FIELD ls_t024-ekgrp.
IF sy-subrc = 0.
APPEND VALUE #( sign = 'I' option = 'EQ' low = ls_t024-ekgrp ) TO gt_ekgrp_rg.
ENDIF.
ENDLOOP.
**& 无权操作时,消息提示
IF gt_bukrs_rg[] IS INITIAL.
MESSAGE '请维护访问公司的权限' TYPE 'E'.
ENDIF.
IF gt_ekgrp_rg[] IS INITIAL.
MESSAGE '请维护访问采购组的权限' TYPE 'E'.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_query_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_query_data .
CONSTANTS:
lc_red TYPE c VALUE '1',
lc_yellow TYPE c VALUE '2',
lc_green TYPE c VALUE '3'.
DATA: ls_line TYPE ts_result.
DATA: lv_green_flag TYPE c.
**& 清空ALV输出结果
REFRESH gt_alv1_data.
**& 入参检查:任何条件输入时提示信息M7-689“限制选择”
IF s_num[] IS INITIAL AND
s_contyp[] IS INITIAL AND
s_categ[] IS INITIAL AND
s_bukrs[] IS INITIAL AND
s_vendow[] IS INITIAL AND
s_ekgrp[] IS INITIAL.
MESSAGE e689(m7).
ENDIF.
**& 查询数据
SELECT wcocoh~num,
wcocoh~ext_num,
wcocoh~contract_type,
twcbcontrtypet~text AS ct_text,
wcocoh~category,
twcbcategoryt~text AS cat_text,
wcocoh~vend_owner,
but000~name_org1,
wcocoh~bukrs,
wcocoh~ekgrp,
wcocoh~date_from,
wcocoh~date_to,
wcocoh~cc_curr,
wcocoh~reference,
wcocoh~assignment,
wcocoh~zterm,
t052u~text1,
wcocoh~zlsch,
t042zt~text2,
wcocoh~created_by,
wcocoh~created_on,
wcocoh~ext_ref_cat,
wcocoh~ext_ref
FROM wcocoh
LEFT JOIN twcbcontrtypet
ON twcbcontrtypet~contract_type = wcocoh~contract_type
AND twcbcontrtypet~lang = @sy-langu
LEFT JOIN twcbcategoryt
ON wcocoh~category = twcbcategoryt~category
AND twcbcategoryt~spras = @sy-langu
LEFT JOIN but000
ON wcocoh~vend_owner = but000~partner
LEFT JOIN t052u
ON t052u~zterm = wcocoh~zterm
AND t052u~spras = @sy-langu
LEFT JOIN t042zt
ON t042zt~zlsch = wcocoh~zlsch
AND t042zt~land1 = 'CN1'
AND t042zt~spras = @sy-langu
WHERE wcocoh~num IN @s_num
AND wcocoh~contract_type IN @s_contyp
AND wcocoh~category IN @s_categ
AND wcocoh~bukrs IN @s_bukrs
AND wcocoh~vend_owner IN @s_vendow
AND wcocoh~ekgrp IN @s_ekgrp
INTO TABLE @DATA(lt_wcocoh).
IF sy-subrc = 0.
SELECT * FROM ztmm_rebatepool INTO TABLE @DATA(lt_rebatepool)
FOR ALL ENTRIES IN @lt_wcocoh
WHERE num = @lt_wcocoh-num.
SELECT * FROM ztmm_rebaterec INTO TABLE @DATA(lt_rebaterec)
FOR ALL ENTRIES IN @lt_wcocoh
WHERE num = @lt_wcocoh-num.
DELETE lt_rebaterec WHERE loekz = 'X'.
SELECT bname, name_textc
FROM user_addr INTO TABLE @DATA(lt_user)
FOR ALL ENTRIES IN @lt_wcocoh
WHERE bname = @lt_wcocoh-created_by.
ENDIF.
LOOP AT lt_wcocoh INTO DATA(ls_wcocoh).
CLEAR: ls_line.
MOVE-CORRESPONDING ls_wcocoh TO ls_line.
"合同货币
IF ls_line-cc_curr IS INITIAL.
ls_line-cc_curr = 'CNY'.
ENDIF.
" 返利协议创建人姓名
READ TABLE lt_user INTO DATA(ls_user) WITH KEY bname = ls_wcocoh-created_by.
IF sy-subrc = 0.
ls_line-name_text = ls_user-name_textc.
ENDIF.
" 指示灯确认
lv_green_flag = 'X'.
LOOP AT lt_rebatepool INTO DATA(ls_rebatepool) WHERE num = ls_wcocoh-num.
IF ls_rebatepool-zrebcons IS INITIAL.
CLEAR: lv_green_flag.
EXIT.
ENDIF.
ENDLOOP.
IF sy-subrc <> 0.
ls_line-zconfstat = lc_red.
ELSE.
IF lv_green_flag IS INITIAL.
ls_line-zconfstat = lc_yellow.
ELSE.
ls_line-zconfstat = lc_green.
ENDIF.
ENDIF.
"返利应计金额
LOOP AT lt_rebatepool INTO ls_rebatepool WHERE num = ls_wcocoh-num.
"返利应计金额
ls_line-zsrebacre = ls_line-zsrebacre + ls_rebatepool-kwert.
"返利确认金额
ls_line-zsrebconf = ls_line-zsrebconf + ls_rebatepool-zrebconf.
ENDLOOP.
LOOP AT lt_rebaterec INTO DATA(ls_rebaterec).
"返利收回金额
ls_line-zsrebrec = ls_line-zsrebrec + ls_rebaterec-zrebrec.
ENDLOOP.
APPEND ls_line TO gt_alv1_data.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_lvc_alv_display
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_lvc_alv_display .
DATA:
ls_variant TYPE disvariant.
DATA:
lt_fcat TYPE lvc_t_fcat,
ls_layout TYPE lvc_s_layo.
ls_variant-report = sy-cprog.
ls_layout-cwidth_opt = abap_true.
ls_layout-box_fname = 'SEL'.
ls_layout-excp_fname = 'ZCONFSTAT'.
PERFORM frm_set_fcat1 CHANGING lt_fcat.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-cprog
i_callback_pf_status_set = 'FRM_SET_PF_STATUS'
i_callback_user_command = 'FRM_ALV_USER_COMMAND'
is_layout_lvc = ls_layout
it_fieldcat_lvc = lt_fcat
is_variant = ls_variant
i_save = 'A'
TABLES
t_outtab = gt_alv1_data
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_set_fcat1
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& <-- LT_FCAT
*&---------------------------------------------------------------------*
FORM frm_set_fcat1 CHANGING ct_fcat TYPE lvc_t_fcat.
ct_fcat = VALUE #(
( fieldname = 'NUM' coltext = '条件合同编号' )
( fieldname = 'EXT_NUM' coltext = '描述' )
( fieldname = 'CONTRACT_TYPE' coltext = '条件合同类型' )
( fieldname = 'CT_TEXT' coltext = '类型描述' )
( fieldname = 'CATEGORY' coltext = '条件合同类别' )
( fieldname = 'CAT_TEXT' coltext = '类别描述' )
( fieldname = 'VEND_OWNER' coltext = '返利供应商' )
( fieldname = 'NAME_ORG1' coltext = '返利供应商名称' )
( fieldname = 'BUKRS' coltext = '公司代码' )
( fieldname = 'EKGRP' coltext = '采购组' )
( fieldname = 'DATE_FROM' coltext = '协议有效期自' )
( fieldname = 'DATE_TO' coltext = '协议有效期至' )
( fieldname = 'ZSREBACRE' coltext = '返利应计金额' )
( fieldname = 'ZSREBCONF' coltext = '返利确认金额' )
( fieldname = 'ZSREBREC' coltext = '返利收回金额' )
( fieldname = 'CC_CURR' coltext = '合同货币' )
( fieldname = 'REFERENCE' coltext = '参考' )
( fieldname = 'ASSIGNMENT' coltext = '分配' )
( fieldname = 'ZTERM' coltext = '付款条件' )
( fieldname = 'TEXT1' coltext = '付款条件描述' )
( fieldname = 'ZLSCH' coltext = '付款方式' )
( fieldname = 'TEXT2' coltext = '付款方式描述' )
( fieldname = 'CREATED_BY' coltext = '返利协议创建人' )
( fieldname = 'NAME_TEXT' coltext = '返利协议创建人姓名' )
( fieldname = 'CREATED_ON' coltext = '返利协议创建日期' )
( fieldname = 'EXT_REF_CAT' coltext = '外部参考类别' )
( fieldname = 'EXT_REF' coltext = '外部参考' )
).
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_set_pf_status
*&---------------------------------------------------------------------*
* alv状态
*----------------------------------------------------------------------*
FORM frm_set_pf_status USING rt_extab TYPE slis_t_extab.
SET PF-STATUS 'PF_STATUS' .
ENDFORM . "set_pf_status
*&---------------------------------------------------------------------*
*& Form FRM_ALV_USER_COMMAND
*&---------------------------------------------------------------------*
* 功能
*----------------------------------------------------------------------*
FORM frm_alv_user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
DATA: lr_grid TYPE REF TO cl_gui_alv_grid.
DATA: lt_sel_rows TYPE lvc_t_row.
**& 刷新
rs_selfield-refresh = abap_true.
rs_selfield-row_stable = abap_true.
rs_selfield-col_stable = abap_true.
**& 获得当前ALV对象
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = lr_grid.
REFRESH: lt_sel_rows.
CALL METHOD lr_grid->get_selected_rows
IMPORTING
et_index_rows = lt_sel_rows.
CASE r_ucomm.
WHEN 'CALCULATE'."返利计算确认
IF lt_sel_rows[] IS INITIAL OR lines( lt_sel_rows ) > 1.
MESSAGE '请仅勾选一行进行返利计算确认' TYPE 'S' DISPLAY LIKE 'E'.
EXIT.
ENDIF.
WHEN OTHERS.
ENDCASE.
ENDFORM.
主程序:
INITIALIZATION.
" 数据初始化
PERFORM frm_initialization.
AT SELECTION-SCREEN.
" 权限检查
PERFORM frm_check_auth.
START-OF-SELECTION.
" 查询数据
PERFORM frm_query_data.
IF gt_alv1_data[] IS NOT INITIAL.
" 数据显示
PERFORM frm_lvc_alv_display.
ENDIF.