SAP ALV编辑、CHECKBOX和添加自定义按钮

今天,对使用过的ALV做一个笔记或备忘,以便以后可以快速使用它。

ALV是SAP的列表控件,所有的列表操作都是围绕它展开,只要ALV熟悉了,可以吃掉ABAP很大一片,以后接活干也快。

我倾向于在自己的ABAP程序中轻量级的使用列表控件,要轻量级的使用,就是 FUNCTION 的方式使用ALV。

这篇文章用FUNCTION方式使用ALV实现三个功能:
1、ALV的编辑和CHECKBOX
2、添加自定义的BUTTON
3、点自定义的BUTTON执行自己的代码

功能一、使用ALV显示内表数据。(其中有一个【领用数量】字段可以编辑,另一个【选择】字段是CHECKBOX)
SAP ALV编辑、CHECKBOX和添加自定义按钮_第1张图片

"ALV 输出
  DATA: WS_FIELDCAT        TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,
        WS_FIELDGROUPS_TAB TYPE SLIS_T_SP_GROUP_ALV,
        WS_LAYOUT          TYPE SLIS_LAYOUT_ALV,
        WS_SORTFIELDS_TAB  TYPE SLIS_T_SORTINFO_ALV WITH HEADER LINE,
        WS_EVENTS          TYPE SLIS_T_EVENT.
  DATA: NN TYPE I VALUE 0.
  DATA: TITLE TYPE LVC_TITLE.
  DATA: COLOR TYPE SLIS_T_SPECIALCOL_ALV WITH HEADER LINE.
  DATA REFFI LIKE MARA-MEINS.
  DEFINE HOUT.
    NN = NN + 1.
    WS_FIELDCAT-COL_POS       =  NN.
    WS_FIELDCAT-TABNAME       = 'GT_BIG'.
    WS_FIELDCAT-KEY           = &1.
    WS_FIELDCAT-FIELDNAME     = &2.
    WS_FIELDCAT-SELTEXT_M     = &3.
    WS_FIELDCAT-DECIMALS_OUT  = &4.
    WS_FIELDCAT-EDIT_MASK     = &5.
    WS_FIELDCAT-REF_TABNAME   = &6.
    WS_FIELDCAT-REF_FIELDNAME = &7.
    WS_FIELDCAT-CHECKBOX      = &8. "搞成CHECKBOX
    WS_FIELDCAT-EDIT          = &9. "编辑选型
    APPEND WS_FIELDCAT.
    CLEAR WS_FIELDCAT.
  END-OF-DEFINITION.
  HOUT:
''    'EBELN'  '采购订单' ''  ''  ''  ''  ''  '',
''    'EBELP'  '采购订单项目' ''  ''  ''  ''  ''  '',
''    'LIFNR'  '供应商' ''  ''  ''  ''  ''  '',
''    'NAME1'  '供应商名称' ''  ''  ''  ''  ''  '',
 ''   'MATNR'  '成品物料' ''  ''  ''  ''  ''  '',
 ''   'MAKTX1'  '成品物料名称' ''  ''  ''  ''  ''  '',
''    'WERKS'   'BOM工厂' ''  ''  ''  ''  ''  '',
''    'AEDAT'   'BOM时间' ''  ''  ''  ''  ''  '',
''    'MENGE'   '成品采购订单数量' ''  ''  ''  ''  ''  '', "f1'
''    'MENGE101' '直接收货' ''  ''  ''  ''  ''  '',        "f2'
''    'MENGE103'  '质检收货' ''  ''  ''  ''  ''  '',       "f3'
 ''   'MENGE1'  '还可收货数量' ''  ''  ''  ''  ''  '',  "f=f1-f2-f3'
 'X'   'IDNRK'    '原材料' ''  ''  ''  ''  ''  '',
 'X'   'MAKTX2'   '原材料名称' ''  ''  ''  ''  ''  '',
 'X'   'MENGEBOM' '原材料BOM汇总数量' ''  ''  ''  ''  ''  '',
 ''   'MENGE2' '计划数量' ''  ''  ''  ''  ''  '',
 ''   'MENGE3' '领用数量' '3'  ''  ''  ''  ''  'X', "注意有3位小数
 ''   'MENGE4' '已打印数量' ''  ''  ''  ''  ''  '',
 ''   'BOX' '选择' ''  ''  ''  ''  'X'  'X'
.
  WS_LAYOUT-ZEBRA             = 'X'.
  WS_LAYOUT-GET_SELINFOS      = 'X'.
  WS_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
  WS_LAYOUT-DETAIL_POPUP      = 'X'.
*******排序
  WS_SORTFIELDS_TAB-FIELDNAME = 'EBELN'.
  WS_SORTFIELDS_TAB-UP = 'X'.
  APPEND WS_SORTFIELDS_TAB.
  CLEAR WS_SORTFIELDS_TAB.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      I_CALLBACK_PROGRAM      = SY-REPID
      IS_LAYOUT               = WS_LAYOUT
      IT_FIELDCAT             = WS_FIELDCAT[]
      IT_SPECIAL_GROUPS       = WS_FIELDGROUPS_TAB[]
      IT_SORT                 = WS_SORTFIELDS_TAB[]
      I_GRID_TITLE            = TITLE
      I_DEFAULT               = 'X'
      I_SAVE                  = 'A'
    TABLES
      T_OUTTAB                = GT_BIG[]
    EXCEPTIONS
      PROGRAM_ERROR           = 1
      OTHERS                  = 2.

功能二、添加自定义工具条按钮

用户在点击checkbox后,选中了几条数据,他希望把它们打印出来,或者去干一些其它事情。
我们需要在工具条上添加自己的按钮,这是怎么实现的呢?

首先,ABAP的工具条是放在STATUS里的,所以再怎么简化,创建一个跟随程序的STATUS是不能避免的了。
除了在标准的工具条上添加一个我们自己的BUTTON, 标准的工具条的其它标准按钮,我们还是需要,所以我们从COPY标准的过来。
那么标准的ALV STATUS,怎么找到呢?

首先到一个标准的ALV控件的屏幕:
SAP ALV编辑、CHECKBOX和添加自定义按钮_第2张图片
以标准的STATUS为源,拷贝一个副本到自己的程序中:
SAP ALV编辑、CHECKBOX和添加自定义按钮_第3张图片

SAP ALV编辑、CHECKBOX和添加自定义按钮_第4张图片
加上我们自己的一个BUTTON,英文名称随便取一个,最后一定要激和。
SAP ALV编辑、CHECKBOX和添加自定义按钮_第5张图片
上面,程序以外的工作就做完了。

下面在程序的ALV函数调用部分添加代码,把我们的STUTAS显示出来.

这里,要仔细说明一下,ALV控件怎么知道使用我们自己的STUTAS呢?
原来,需要把新的STATUS作为事件传给ALV,ALV就知道在显示的时候,用我们自己的STATUS了。
我们给ALV加上一个设置STATUS的事件:

DATA GS_EVENT TYPE SLIS_ALV_EVENT.
DATA GT_EVENT TYPE SLIS_T_EVENT.
GS_EVENT-NAME = 'PF_STATUS_SET'.
GS_EVENT-FORM = 'ALV_STATUS_SET'.
APPEND GS_EVENT TO GT_EVENT.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      I_CALLBACK_PROGRAM      = SY-REPID
      IS_LAYOUT               = WS_LAYOUT
      IT_FIELDCAT             = WS_FIELDCAT[]
      IT_SPECIAL_GROUPS       = WS_FIELDGROUPS_TAB[]
      IT_SORT                 = WS_SORTFIELDS_TAB[]
      IT_EVENTS               = GT_EVENT             "我们加了
      I_GRID_TITLE            = TITLE
      I_DEFAULT               = 'X'
      I_SAVE                  = 'A'
    TABLES
      T_OUTTAB                = GT_BIG[]

事件的方法是一个FORM,还要搞一个FORM出来,并在FORM中指定正真的GUI工具条控件的名字:

"------------加载STATUS的FORM--------------------
  FORM ALV_STATUS_SET USING PT_EXTAB TYPE SLIS_T_EXTAB.
    CLEAR: PT_EXTAB[].
    SET PF-STATUS 'STANDARD_FULLSCREEN'.
  ENDFORM.

到现在,执行程序,我们自定义的BOTTON稳稳的出现了。
这里写图片描述
做其它开发的同学可能要笑了,加了一个 BUTTON都这么麻烦。
是的,这就是ABAP,企业软件没有其它软件那么花哨,有用户自定义的一、两个按钮就能够满足大部分需求了。

功能三、点击自定义的按钮,实现操作

当用户点击我们的自定义按钮时,我们希望执行一些程序代码,做我们自己想做的事情。

同样,还是在ALV FUNCTION传入参数的方式来唤醒这个功能,居然还叫回调函数,同WINDOWS编程里面一样一样的。
这里,这个I_CALLBACK_USER_COMMAND参数,不需要什么事件了,直接传入一个执行FORM的名称

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      I_CALLBACK_PROGRAM      = SY-REPID
      IS_LAYOUT               = WS_LAYOUT
      IT_FIELDCAT             = WS_FIELDCAT[]
      IT_SPECIAL_GROUPS       = WS_FIELDGROUPS_TAB[]
      IT_SORT                 = WS_SORTFIELDS_TAB[]
      IT_EVENTS               = GT_EVENT             
      I_CALLBACK_USER_COMMAND = 'ALV_USER_COMMAND'   "我们加了
      I_GRID_TITLE            = TITLE
      I_DEFAULT               = 'X'
      I_SAVE                  = 'A'
    TABLES
      T_OUTTAB                = GT_BIG[]

我们自己写这个FORM的代码,B1就是我们的按钮,按进来后做的事情,执行另一个SAVELOG FORM ,前面那几十行代码是为了得到用户在CHECKBOX选了东西后,同步到内表的代码:

"------------ALV_USER_COMMAND的FORM--------------------
FORM ALV_USER_COMMAND USING P_UCOMM LIKE SY-UCOMM P_SELFIELD TYPE SLIS_SELFIELD.  "P_SELFIELD 用户鼠标选择的一个字段
"应用方法将修改ALV的数据保存到内表中
  DATA LR_GRID TYPE REF TO CL_GUI_ALV_GRID.
  DATA L_TABIX LIKE SY-TABIX.  "系统参数   行号
"此处代码将用户输入读入内表(这一步不可少)
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR' "自动刷新数据
    IMPORTING
      E_GRID = LR_GRID.
  CALL METHOD LR_GRID->CHECK_CHANGED_DATA.
  P_SELFIELD-REFRESH = 'X'. "此处代码阻止'REUSE_ALV_GRID_DISPLAY创建新的屏幕,造成屏幕多层
  P_SELFIELD-ROW_STABLE = 'X'. "保证行、列位置在自动刷新时不变
  P_SELFIELD-COL_STABLE = 'X'.
CASE P_UCOMM.
  WHEN 'B1'.
    PERFORM SAVELOG.
ENDCASE.
ENDFORM.

上面,我们实现了

功能一、ALV的编辑和CHECKBOX
功能二、添加自定义的BUTTON
功能三、点自定义的BUTTON执行自己的代码

而这些功能,没有自己创建屏幕,都写在一个程序里面简单的实现了。

程序代码全文:

*&---------------------------------------------------------------------*
*& Report  ZTEST7
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZTEST7.
TABLES: EKKO,EKPO.
DATA: BEGIN OF GT_ITAB OCCURS 0,
            EBELN  LIKE EKKO-EBELN, "采购订单
            EBELP  LIKE EKPO-EBELP, "采购订单项目
            LIFNR  LIKE EKKO-LIFNR, "供应商
            NAME1  LIKE  LFA1-NAME1, "供应商名称
            MATNR  LIKE  EKPO-MATNR, "成品物料
            MAKTX1 LIKE  MAKT-MAKTX, "成品物料名称
            WERKS  LIKE  EKPO-WERKS, "BOM工厂
            AEDAT  LIKE  EKKO-AEDAT, "BOM时间
            MENGE  LIKE  EKPO-MENGE, "成品采购订单数量 f1
            MENGE101  LIKE  EKPO-MENGE, "直接收货 f2
            MENGE103  LIKE  EKPO-MENGE, "质检收货 f3
            MENGE1 LIKE  EKPO-MENGE, "还可收货数量 f=f1-f2-f3
      END OF GT_ITAB.
*
*DATA: BEGIN OF GT_BIG OCCURS 0,
*
*            EBELN  LIKE EKKO-EBELN, "采购订单
*            EBELP  LIKE EKPO-EBELP, "采购订单项目
*            LIFNR  LIKE EKKO-LIFNR, "供应商
*            NAME1  LIKE  LFA1-NAME1, "供应商名称
*            MATNR  LIKE  EKPO-MATNR, "成品物料
*            MAKTX1 LIKE  MAKT-MAKTX, "成品物料名称
*            WERKS  LIKE  EKPO-WERKS, "BOM工厂
*            AEDAT  LIKE  EKKO-AEDAT, "BOM时间
*            MENGE  LIKE  EKPO-MENGE, "原采购订单数量 f1
*            MENGE101  LIKE  EKPO-MENGE, "直接收货 f2
*            MENGE103  LIKE  EKPO-MENGE, "质检收货 f3
*
*            MENGE1 LIKE  EKPO-MENGE, "还可收货数量 f=f1-f2-f3
*
*            IDNRK  LIKE   STPO-IDNRK,  "原材料
*            MAKTX2 LIKE  MAKT-MAKTX,  "原材料名称
*            MENGEBOM LIKE  EKPO-MENGE, "原材料BOM汇总数量
*
*            MENGE2 LIKE  EKPO-MENGE, """"计划数量
*            MENGE3 LIKE  EKPO-MENGE, """"领用数量
*            MENGE4 LIKE  EKPO-MENGE, """"已打印数量
*            BOX    TYPE C,
*
*      END OF GT_BIG.
DATA GT_BIG LIKE TABLE OF ZMMS_IF_045 WITH HEADER LINE.
DATA GT_BIGX LIKE TABLE OF ZMMS_IF_045 WITH HEADER LINE.
 DATA GT_STPO LIKE TABLE OF STPO_API02 WITH HEADER LINE.
 DATA: BEGIN OF GT_TEMP OCCURS 0,
            MATNR(18)  TYPE C, "子物料
            AMOUNT TYPE i,    "数量
      END OF GT_TEMP.
  DATA: BEGIN OF GT_SUM OCCURS 0,
            MATNR(18)  TYPE C, "子物料
            AMOUNT TYPE i,    "数量
      END OF GT_SUM.
DATA STR_DATE LIKE CSAP_MBOM-DATUV.
DATA GS_ZMMT_LOG027 LIKE ZMMT_LOG027.
"选择屏幕
SELECT-OPTIONS GS_LIFNR FOR EKKO-LIFNR.  "供应商
SELECT-OPTIONS GS_EBELN FOR EKKO-EBELN.  "采购订单号
SELECT-OPTIONS GS_EBELP FOR EKPO-EBELP.  "采购订单项目
SELECT-OPTIONS GS_MATNR FOR EKPO-MATNR.  "成品物料
PARAMETERS GV_PRINT AS CHECKBOX.  "已打印
PARAMETERS GV_NO AS CHECKBOX.  "未打印
   SELECT *
   INTO CORRESPONDING FIELDS OF TABLE GT_ITAB
   FROM EKKO AS A
   INNER JOIN EKPO AS P
   ON A~EBELN = P~EBELN
   WHERE A~BSART = 'PO05'
     AND A~FRGKE = 'N'
     AND A~LOEKZ = ''
     AND P~LOEKZ = ''
     AND P~ELIKZ = ''
     AND A~LIFNR IN GS_LIFNR
     AND A~EBELN IN GS_EBELN
     AND P~EBELP IN GS_EBELP
     AND P~MATNR IN GS_MATNR
     .
"计算成品物料,可收货数量
LOOP AT GT_ITAB.
         "--------------------------------
         "计算AVAILABLE字段,计算【采购订单】【行项目】中还可以收货数!
         "--------------------------------
        DATA  GV_101   LIKE EKPO-MENGE.
        DATA  GV_102   LIKE EKPO-MENGE.
        DATA  GV_103   LIKE EKPO-MENGE.
        DATA  GV_104   LIKE EKPO-MENGE.
        SELECT SUM( MENGE ) INTO GV_101 FROM EKBE WHERE BWART = '101' AND EBELN = GT_ITAB-EBELN AND EBELP = GT_ITAB-EBELP.     "采购订单收货数 101
        SELECT SUM( MENGE ) INTO GV_102 FROM EKBE WHERE BWART = '102' AND EBELN = GT_ITAB-EBELN AND EBELP = GT_ITAB-EBELP.     "采购订单收货数 102
        SELECT SUM( WESBS ) INTO GV_103 FROM EKBE WHERE BWART = '103' AND EBELN = GT_ITAB-EBELN AND EBELP = GT_ITAB-EBELP.     "采购订单收货数 103
        SELECT SUM( WESBS ) INTO GV_104 FROM EKBE WHERE BWART = '104' AND EBELN = GT_ITAB-EBELN AND EBELP = GT_ITAB-EBELP.     "采购订单收货数 104
        GT_ITAB-MENGE101 =  GV_101 - GV_102.
        GT_ITAB-MENGE103 =  GV_103 - GV_104.
        GT_ITAB-MENGE1 = GT_ITAB-MENGE - GT_ITAB-MENGE101 - GT_ITAB-MENGE103.
        CLEAR: GV_101,GV_102,GV_103,GV_104.
  "物料描述
        SELECT SINGLE MAKTX INTO GT_ITAB-MAKTX1 FROM MAKT AS A WHERE A~MATNR = GT_ITAB-MATNR.
 "供应商描述
        SELECT SINGLE NAME1 INTO GT_ITAB-NAME1 FROM LFA1 AS A WHERE A~LIFNR = GT_ITAB-LIFNR.
        MODIFY GT_ITAB.
ENDLOOP.
"循环GT_ITAB
LOOP AT GT_ITAB.
        CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL' "加点
        EXPORTING
        DATE_INTERNAL = GT_ITAB-AEDAT
        IMPORTING
        DATE_EXTERNAL = STR_DATE.
        "物料号补零
        CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
          EXPORTING
           INPUT              = GT_ITAB-MATNR
          IMPORTING
           OUTPUT             = GT_ITAB-MATNR.
      "1.查BOM组件
      CALL FUNCTION 'CSAP_MAT_BOM_READ'
        EXPORTING
         MATERIAL             = GT_ITAB-MATNR
         PLANT                = GT_ITAB-WERKS
         BOM_USAGE            = '1'
         VALID_FROM           =  STR_DATE
         VALID_TO             =  STR_DATE
       TABLES
         T_STPO               = GT_STPO
       EXCEPTIONS
         ERROR                = 1
         OTHERS               = 2.
      "2.合计相同物料的数量
      LOOP AT GT_STPO.
            GT_TEMP-MATNR  = GT_STPO-COMPONENT.
            GT_TEMP-AMOUNT = GT_STPO-COMP_QTY.
            APPEND GT_TEMP.
      ENDLOOP.
      SORT GT_TEMP BY MATNR.
      LOOP AT GT_TEMP.
      COLLECT GT_TEMP INTO GT_SUM.
      ENDLOOP.
      "3.填充到大表中
      LOOP AT GT_SUM.
        MOVE-CORRESPONDING GT_ITAB TO GT_BIG.
        GT_BIG-IDNRK = GT_SUM-MATNR.
        GT_BIG-MENGEBOM = GT_SUM-AMOUNT.
        APPEND GT_BIG.
      ENDLOOP.
      CLEAR:GT_STPO,GT_STPO[],GT_TEMP,GT_TEMP[],GT_SUM,GT_SUM[].
      CLEAR:GT_ITAB.
ENDLOOP.
LOOP AT GT_BIG.
         "物料号补零
        CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
              EXPORTING
               INPUT              = GT_BIG-IDNRK
              IMPORTING
               OUTPUT             = GT_BIG-IDNRK.
        "子物料描述
        SELECT SINGLE MAKTX INTO GT_BIG-MAKTX2 FROM MAKT AS A WHERE A~MATNR = GT_BIG-IDNRK.
          "子物料号去0
         CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT'
           EXPORTING
             INPUT         = GT_BIG-IDNRK
          IMPORTING
             OUTPUT        = GT_BIG-IDNRK.
          "成品物料号去0
         CALL FUNCTION 'CONVERSION_EXIT_MATN1_OUTPUT'
           EXPORTING
             INPUT         = GT_BIG-MATNR
          IMPORTING
             OUTPUT        = GT_BIG-MATNR.
         "供应商去0
         CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
           EXPORTING
             INPUT         = GT_BIG-LIFNR
          IMPORTING
             OUTPUT        = GT_BIG-LIFNR.
            "填充数量(计划,领用)
         GT_BIG-MENGE2 =  GT_BIG-MENGE1 * GT_BIG-MENGEBOM.
         GT_BIG-MENGE3 =  GT_BIG-MENGE1 * GT_BIG-MENGEBOM.
         MODIFY GT_BIG.
         CLEAR GT_BIG.
ENDLOOP.
"填充【打印数量】
LOOP AT GT_BIG.
    SELECT SUM( MENGE ) INTO GT_BIG-MENGE4 FROM ZMMT_LOG027 WHERE EBELN = GT_BIG-EBELN AND EBELP = GT_BIG-EBELP AND MATNR2 = GT_BIG-IDNRK.
    MODIFY GT_BIG.
    CLEAR GT_BIG.
ENDLOOP.
"ALV 输出
  DATA: WS_FIELDCAT        TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,
        WS_FIELDGROUPS_TAB TYPE SLIS_T_SP_GROUP_ALV,
        WS_LAYOUT          TYPE SLIS_LAYOUT_ALV,
        WS_SORTFIELDS_TAB  TYPE SLIS_T_SORTINFO_ALV WITH HEADER LINE,
        WS_EVENTS          TYPE SLIS_T_EVENT.
  DATA: NN TYPE I VALUE 0.
  DATA: TITLE TYPE LVC_TITLE.
  DATA: COLOR TYPE SLIS_T_SPECIALCOL_ALV WITH HEADER LINE.
  DATA REFFI LIKE MARA-MEINS.
  DEFINE HOUT.
    NN = NN + 1.
    WS_FIELDCAT-COL_POS       =  NN.
    WS_FIELDCAT-TABNAME       = 'GT_BIG'.
    WS_FIELDCAT-KEY           = &1.
    WS_FIELDCAT-FIELDNAME     = &2.
    WS_FIELDCAT-SELTEXT_M     = &3.
    WS_FIELDCAT-DECIMALS_OUT  = &4.
    WS_FIELDCAT-EDIT_MASK      = &5.
    WS_FIELDCAT-REF_TABNAME   = &6.
    WS_FIELDCAT-REF_FIELDNAME = &7.
    WS_FIELDCAT-CHECKBOX      = &8.
    WS_FIELDCAT-EDIT          = &9.
    APPEND WS_FIELDCAT.
    CLEAR WS_FIELDCAT.
  END-OF-DEFINITION.
  HOUT:
''    'LIFNR'  '供应商' ''  ''  ''  ''  ''  '',
''    'NAME1'  '供应商名称' ''  ''  ''  ''  ''  '',
''    'EBELN'  '采购订单' ''  ''  ''  ''  ''  '',
''    'EBELP'  '采购订单项目' ''  ''  ''  ''  ''  '',
 ''   'MATNR'  '成品物料' ''  ''  ''  ''  ''  '',
 ''   'MAKTX1'  '成品物料名称' ''  ''  ''  ''  ''  '',
''    'WERKS'   'BOM工厂' ''  ''  ''  ''  ''  '',
''    'AEDAT'   'BOM时间' ''  ''  ''  ''  ''  '',
''    'MENGE'   '成品采购订单数量' ''  ''  ''  ''  ''  '', "f1'
''    'MENGE101' '直接收货' ''  ''  ''  ''  ''  '',        "f2'
''    'MENGE103'  '质检收货' ''  ''  ''  ''  ''  '',       "f3'
 ''   'MENGE1'  '还可收货数量' ''  ''  ''  ''  ''  '',  "f=f1-f2-f3'
 'X'   'IDNRK'    '原材料' ''  ''  ''  ''  ''  '',
 'X'   'MAKTX2'   '原材料名称' ''  ''  ''  ''  ''  '',
 'X'   'MENGEBOM' '原材料BOM汇总数量' ''  ''  ''  ''  ''  '',
 ''   'MENGE2' '计划数量' ''  ''  ''  ''  ''  '',
 ''   'MENGE3' '领用数量' '3'  ''  ''  ''  ''  'X', "注意有3位小数
 ''   'MENGE4' '已打印数量' ''  ''  ''  ''  ''  '',
 ''   'BOX' '选择' ''  ''  ''  ''  'X'  'X'
.
  WS_LAYOUT-ZEBRA             = 'X'.
  WS_LAYOUT-GET_SELINFOS      = 'X'.
  WS_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
  WS_LAYOUT-DETAIL_POPUP      = 'X'.
*******排序
  WS_SORTFIELDS_TAB-FIELDNAME = 'LIFNR'.
  WS_SORTFIELDS_TAB-UP = 'X'.
  APPEND WS_SORTFIELDS_TAB.
  CLEAR WS_SORTFIELDS_TAB.
DATA GS_EVENT TYPE SLIS_ALV_EVENT.
DATA GT_EVENT TYPE SLIS_T_EVENT.
GS_EVENT-NAME = 'PF_STATUS_SET'.
GS_EVENT-FORM = 'ALV_STATUS_SET'.
APPEND GS_EVENT TO GT_EVENT.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      I_CALLBACK_PROGRAM      = SY-REPID
      IS_LAYOUT               = WS_LAYOUT
      IT_FIELDCAT             = WS_FIELDCAT[]
      IT_SPECIAL_GROUPS       = WS_FIELDGROUPS_TAB[]
      IT_SORT                 = WS_SORTFIELDS_TAB[]
      IT_EVENTS               = GT_EVENT             "我们加了
      I_CALLBACK_USER_COMMAND = 'ALV_USER_COMMAND'   "我们加了
      I_GRID_TITLE            = TITLE
      I_DEFAULT               = 'X'
      I_SAVE                  = 'A'
    TABLES
      T_OUTTAB                = GT_BIG[]
    EXCEPTIONS
      PROGRAM_ERROR           = 1
      OTHERS                  = 2.
"------------SAVELOG的FORM--------------------
FORM SAVELOG .
"保存到LOG表
LOOP AT GT_BIG.
  IF GT_BIG-BOX = 'X'.
        GS_ZMMT_LOG027-USRID = SY-UNAME. "用户名
        GS_ZMMT_LOG027-ERDAT = SY-DATUM. "日期
        GS_ZMMT_LOG027-ANUZT = SY-UZEIT. "时间
        CALL FUNCTION 'NUMBER_GET_NEXT'
           EXPORTING
             NR_RANGE_NR                   = 'A1'
             OBJECT                        = 'ZSDFM020'
          IMPORTING
             NUMBER                         = GS_ZMMT_LOG027-AUTONUMBER.
        CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'  "去掉前导零
          EXPORTING
            INPUT         = GS_ZMMT_LOG027-AUTONUMBER
         IMPORTING
           OUTPUT        =  GS_ZMMT_LOG027-AUTONUMBER.
        GS_ZMMT_LOG027-EBELN  = GT_BIG-EBELN. "
        GS_ZMMT_LOG027-EBELP  = GT_BIG-EBELP. "
        GS_ZMMT_LOG027-MATNR1 = GT_BIG-MATNR. "
        GS_ZMMT_LOG027-MATNR2 = GT_BIG-IDNRK. "
        GS_ZMMT_LOG027-MENGE  = GT_BIG-MENGE3. "
        INSERT ZMMT_LOG027 FROM GS_ZMMT_LOG027.
        CLEAR  GS_ZMMT_LOG027.
  ENDIF.
        COMMIT WORK .
ENDLOOP.
"调用SMARTFORM
  MOVE GT_BIG[] TO GT_BIGX[].
  DELETE GT_BIGX WHERE BOX =''.
  DATA  GV_FORMNAME TYPE RS38L_FNAM .
  DATA GS_CONTROL_PARAMS TYPE SSFCTRLOP.
      GS_CONTROL_PARAMS-NO_DIALOG = 'X'.
      GS_CONTROL_PARAMS-PREVIEW = 'X'.
  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
    EXPORTING
      FORMNAME           = 'ZMMR0089'
    IMPORTING
      FM_NAME            = GV_FORMNAME.
 CALL FUNCTION GV_FORMNAME
        EXPORTING
        CONTROL_PARAMETERS = GS_CONTROL_PARAMS
        USER_SETTINGS      = SPACE
     TABLES
       GT_BIG             = GT_BIGX
     EXCEPTIONS
        FORMATTING_ERROR   = 1
        INTERNAL_ERROR     = 2
        SEND_ERROR         = 3
        USER_CANCELED      = 4
        OTHERS             = 5.
ENDFORM.
"------------ALV_USER_COMMAND的FORM--------------------
FORM ALV_USER_COMMAND USING P_UCOMM LIKE SY-UCOMM P_SELFIELD TYPE SLIS_SELFIELD.  "P_SELFIELD 用户鼠标选择的一个字段
"应用方法将修改ALV的数据保存到内表中
  DATA LR_GRID TYPE REF TO CL_GUI_ALV_GRID.
  DATA L_TABIX LIKE SY-TABIX.  "系统参数   行号
"此处代码将用户输入读入内表(这一步不可少)
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR' "自动刷新数据
    IMPORTING
      E_GRID = LR_GRID.
  CALL METHOD LR_GRID->CHECK_CHANGED_DATA.
  P_SELFIELD-REFRESH = 'X'. "此处代码阻止'REUSE_ALV_GRID_DISPLAY创建新的屏幕,造成屏幕多层
  P_SELFIELD-ROW_STABLE = 'X'. "保证行、列位置在自动刷新时不变
  P_SELFIELD-COL_STABLE = 'X'.
CASE P_UCOMM.
  WHEN 'B1'.
    PERFORM SAVELOG.
ENDCASE.
ENDFORM.
"------------加载STATUS的FORM--------------------
  FORM ALV_STATUS_SET USING PT_EXTAB TYPE SLIS_T_EXTAB.
    CLEAR: PT_EXTAB[].
    SET PF-STATUS 'STANDARD_FULLSCREEN'.
  ENDFORM.

你可能感兴趣的:(驾驭,SAP,ABAP)