ABAP FUNCTION ALV单元格单击/双击事件

为实现打印入库单时,勾选某个行项目,则同一个单子的行项都要自动勾上。需要用到单击事件。

1.为需要响应单击按钮的列设定:

       WA_FIELDCAT-HOTSPOT 'X'.  "单击    其中 WA_FIELDCAT     TYPE LINE OF SLIS_T_FIELDCAT_ALV,

2.在ALV事件里响应:

  CASE R_UCOMM.

    WHEN '&IC1'. "单击复选框 勾选(取消)后同一预留号都自动勾选(取消)
      READ TABLE ITAB_TOTAL INDEX R_SEL-tabindex INTO DATA(WA_SEL_TEMP).  "获取单击行
      IF R_SEL-fieldname = 'SEL' AND WA_SEL_TEMP-SEL = ''.
          LOOP AT ITAB_TOTAL ASSIGNING FIELD-SYMBOL() WHERE RSNUM = WA_SEL_TEMP-RSNUM .
            -SEL = 'X'.
          ENDLOOP.
        ELSEIF R_SEL-fieldname = 'SEL' AND WA_SEL_TEMP-SEL = 'X'.
          LOOP AT ITAB_TOTAL ASSIGNING FIELD-SYMBOL() WHERE RSNUM = WA_SEL_TEMP-RSNUM  .
            -SEL = ''.
          ENDLOOP.
      ENDIF.
     3.最后注意响应事件前加GET_GLOBALS_FROM_SLVC_FULLSCR,响应后刷新 CALL  METHOD  REF ->REFRESH_TABLE_DISPLAY.

ABAP FUNCTION ALV单元格单击/双击事件_第1张图片

*********************************************************************************
*  程序名:ZWMRP008
*  程序名称:入库单打印
*-------------------------------------------------
*  创建日期       程序员    SAP版本    程序类型
*  2016-09-22     yhy     RP
*-------------------------------------------------
*  描述:
*成本中心领用,线边退料,成品工废、料废订单消耗领用,成品返工领用,
*项目领用,废品消耗领用,备件消耗领用
*=================================================
*  修改日期   版本    修改人      修改描述
*
********************************************************************************
REPORT ZWMRP008.
*------------------------------------------------------------------------------*
*                             GLOBLE-DEFINATION                                *
*------------------------------------------------------------------------------*
TABLES:RESB.
*ALV输出用变量定义
*----ALV变量
TYPE-POOLS slis.
TYPE-POOLS: icon.
DATA: WA_FIELDCAT             TYPE LINE OF SLIS_T_FIELDCAT_ALV,
      ITAB_FIELDCAT           TYPE SLIS_T_FIELDCAT_ALV,
      GT_EVENTS               TYPE SLIS_T_EVENT,
      WA_LAYOUT               TYPE SLIS_LAYOUT_ALV,
      ITAB_LAYOUT             TYPE SLIS_LAYOUT_ALV,
      I_GRID_SETTINGS         TYPE  LVC_S_GLAY.

*----ALV宏
DEFINE ADD_FIELD.
  CLEAR WA_FIELDCAT.
  WA_FIELDCAT-FIELDNAME     = &1.
  WA_FIELDCAT-TABNAME       = &2.
  WA_FIELDCAT-SELTEXT_M     = &3.
  WA_FIELDCAT-CHECKBOX      = &4.
  WA_FIELDCAT-EDIT          = &5.
  WA_FIELDCAT-NO_ZERO       = 'X'. "无前导0
  WA_FIELDCAT-REF_TABNAME   = &6.
  WA_FIELDCAT-REF_FIELDNAME = &7.
  IF WA_FIELDCAT-FIELDNAME = 'SEL'.
    WA_FIELDCAT-HOTSPOT = 'X'.  "单击
  ENDIF.

  APPEND WA_FIELDCAT TO ITAB_FIELDCAT.
END-OF-DEFINITION.

TYPES: BEGIN OF TY_TOTAL,  "
      SEL TYPE C,
      RSNUM	TYPE RESB-RSNUM,"预留号
      RSPOS	TYPE RESB-RSPOS,"预留行项目
      WERKS	TYPE RESB-WERKS ,"工厂
      LGORT	TYPE RESB-LGORT ,"库存地
      MATNR	TYPE RESB-MATNR ,"物料代码
      BDTER	TYPE RESB-BDTER ,"需求日期
      BDMNG	TYPE RESB-BDMNG ,"数量
      MEINS	TYPE RESB-MEINS ,"单位
      BWART	TYPE RESB-BWART ,"移动类型
      BWART_C TYPE T156T-BTEXT,"移动类型文本
      UMWRK	TYPE RESB-UMWRK ,"接收工厂
      UMLGO	TYPE RESB-UMLGO ,"接收库存地
      AUFNR	TYPE RESB-AUFNR ,"订单
      PSPEL	TYPE RESB-PSPEL ,"WBS元素

      TPLNR TYPE VIAFKOS-TPLNR, "功能位置
      PLTXT TYPE IFLOTX-PLTXT,  "功能位置描述
      EQUNR TYPE VIAFKOS-EQUNR, "设备编号
      EQKTX  TYPE EQKT-EQKTX,    "设备描述
      MAKTX TYPE MAKT-MAKTX,"物料描述
      VERAK TYPE CSKS-VERAK,"成本中心描述---使用部门

 END OF TY_TOTAL.

DATA: ITAB_TOTAL TYPE TABLE OF TY_TOTAL,
      WA_TOTAL   TYPE TY_TOTAL.
DATA: ITAB_SF TYPE TABLE OF TY_TOTAL.
*-----------------------------------------------------------------------
* TYPE-POOLS
*-----------------------------------------------------------------------


*------------------------------------------------------------------------------*
*                             SELECTION-SCREEN                                 *
*------------------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE TEXT-001.
PARAMETERS: P_BWART TYPE RESB-BWART OBLIGATORY,
            P_WERKS TYPE RESB-WERKS OBLIGATORY DEFAULT '2000'.

SELECT-OPTIONS: S_MATNR FOR RESB-MATNR,
                S_RESNUM FOR RESB-RSNUM NO-EXTENSION NO INTERVALS ,
*                S_.                    NO-EXTENSION NO INTERVALS ,  "领用单号-自建表
                S_BDTER FOR RESB-BDTER.
SELECTION-SCREEN END OF BLOCK blk1.

*------------------------------------------------------------------------------*
*                             INITIALIZATION                                 *
*------------------------------------------------------------------------------*

*------------------------------------------------------------------------------*
*                             AT SELECTION-SCREEN                              *
*------------------------------------------------------------------------------*

*------------------------------------------------------------------------------*
*                             START-OF-SELECTION                               *
*------------------------------------------------------------------------------*
  PERFORM P_GET_DATA.
  PERFORM P_DISPLAY.

*------------------------------------------------------------------------------*
*                             SUBROUTINES                                      *
*------------------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Form  P_GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM P_GET_DATA .

  CLEAR: ITAB_TOTAL,WA_TOTAL.
   "备件消耗领用--PM
  IF P_BWART = 'S61'.
     SELECT
         RESB~RSNUM	"预留号
         RESB~RSPOS	"预留行项目
         RESB~WERKS	"工厂
         RESB~LGORT	"库存地
         RESB~MATNR	"物料代码
         RESB~BDTER	"需求日期
         RESB~BDMNG	"数量
         RESB~MEINS	"单位
         RESB~BWART	"移动类型
         RESB~UMWRK	"接收工厂
         RESB~UMLGO	"接收库存地
         RESB~AUFNR	"订单
         RESB~PSPEL	"WBS元素
         VIAFKOS~TPLNR  "功能位置
         IFLOTX~PLTXT "功能位置描述
         VIAFKOS~EQUNR "设备编号
         EQKT~EQKTX    "设备描述
       INTO CORRESPONDING FIELDS OF TABLE ITAB_TOTAL
       FROM RESB INNER JOIN VIAFKOS ON RESB~AUFNR = VIAFKOS~AUFNR
       INNER JOIN IFLOTX ON VIAFKOS~TPLNR = IFLOTX~TPLNR AND IFLOTX~SPRAS = 1
       INNER JOIN EQKT ON VIAFKOS~EQUNR = EQKT~EQUNR  AND EQKT~SPRAS = 1
       WHERE  RESB~WERKS = P_WERKS
       AND RESB~BWART = P_BWART
       AND RESB~MATNR IN S_MATNR
       AND RESB~RSNUM IN S_RESNUM
       AND RESB~BDTER IN S_BDTER.

    ELSE.

     SELECT
        RSNUM	"预留号
        RSPOS	"预留行项目
        WERKS	"工厂
        LGORT	"库存地
        MATNR	"物料代码
        BDTER	"需求日期
        BDMNG	"数量
        MEINS	"单位
        BWART	"移动类型
        UMWRK	"接收工厂
        UMLGO	"接收库存地
        AUFNR	"订单
        PSPEL	"WBS元素
      INTO CORRESPONDING FIELDS OF TABLE ITAB_TOTAL
      FROM RESB
      WHERE  WERKS = P_WERKS
      AND BWART = P_BWART
      AND MATNR IN S_MATNR
      AND RSNUM IN S_RESNUM
      AND BDTER IN S_BDTER.
*      AND 领用单号-自建表
  ENDIF.

   IF ITAB_TOTAL IS INITIAL.
     MESSAGE '无数据,请确认查询条件!' TYPE 'S'.
     STOP.
   ENDIF.



   LOOP AT ITAB_TOTAL INTO WA_TOTAL.
     SELECT SINGLE MAKTX
       INTO WA_TOTAL-MAKTX
       FROM MAKT
       WHERE MATNR = WA_TOTAL-MATNR.

     SELECT SINGLE
       BTEXT
       INTO WA_TOTAL-BWART_C
       FROM T156T
       WHERE SPRAS = 1
       AND BWART = WA_TOTAL-BWART.
*取成本中心描述
     SELECT SINGLE
       CSKS~VERAK
       INTO WA_TOTAL-VERAK
       FROM RKPF INNER JOIN CSKS ON RKPF~KOSTL = CSKS~KOSTL
       WHERE RKPF~RSNUM = WA_TOTAL-RSNUM.

     MODIFY ITAB_TOTAL FROM WA_TOTAL.
     CLEAR WA_TOTAL.
   ENDLOOP.

ENDFORM.












*&---------------------------------------------------------------------*
*&      Form  P_DISPLAY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM P_DISPLAY .
*        SEL TYPE C,

  ADD_FIELD   'SEL' 'ITAB_TOTAL' '选择' 'X' 'X' SPACE SPACE  .
*  ADD_FIELD   ''  'ITAB_TOTAL' '领用单号' SPACE SPACE .
  ADD_FIELD   'BWART_C' 'ITAB_TOTAL' '单据类型' SPACE SPACE SPACE SPACE.
  ADD_FIELD   'MATNR'  'ITAB_TOTAL' '物料代码' SPACE SPACE SPACE SPACE.
  ADD_FIELD   'MAKTX' 'ITAB_TOTAL' '物料描述' SPACE SPACE SPACE SPACE.
  ADD_FIELD   'BDTER'  'ITAB_TOTAL' '日期' SPACE SPACE SPACE SPACE.
  ADD_FIELD   'AUFNR' 'ITAB_TOTAL' '订单' SPACE SPACE SPACE SPACE.
  IF P_BWART = 'S61'.
    ADD_FIELD   'TPLNR' 'ITAB_TOTAL' '功能位置' SPACE SPACE SPACE SPACE.
    ADD_FIELD   'PLTXT' 'ITAB_TOTAL' '功能位置描述' SPACE SPACE SPACE SPACE.
    ADD_FIELD   'EQUNR' 'ITAB_TOTAL' '设备编号' SPACE SPACE SPACE SPACE.
    ADD_FIELD   'EQKTX' 'ITAB_TOTAL' '设备描述' SPACE SPACE SPACE SPACE.
  ENDIF.
  ADD_FIELD   'PSPEL'  'ITAB_TOTAL' 'WBS元素' SPACE SPACE SPACE SPACE.
  ADD_FIELD   'RSNUM'  'ITAB_TOTAL' '预留号' SPACE SPACE SPACE SPACE.
  ADD_FIELD   'RSPOS' 'ITAB_TOTAL' '行项号' SPACE SPACE SPACE SPACE.
  ADD_FIELD   'BDMNG'  'ITAB_TOTAL' '数量' SPACE SPACE SPACE SPACE.
  ADD_FIELD   'MEINS'  'ITAB_TOTAL' '单位' SPACE SPACE SPACE SPACE.
  ADD_FIELD   'LGORT'  'ITAB_TOTAL' '发出库存地' SPACE SPACE 'RESB' 'LGORT'.
  ADD_FIELD   'UMLGO'  'ITAB_TOTAL' '接收库存地' SPACE SPACE 'RESB' 'UMLGO'.
******build layout
  CLEAR ITAB_LAYOUT.
  WA_LAYOUT-ZEBRA             = 'X'.
*  LW_LAYOUT-INFO_FIELDNAME    = 'COLOR'.
*  WA_LAYOUT-BOX_FIELDNAME = 'SEL'.
  WA_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
  MOVE-CORRESPONDING WA_LAYOUT TO ITAB_LAYOUT.

*****ALV DISPLY
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      I_CALLBACK_PROGRAM       = SY-REPID
      IS_LAYOUT                = ITAB_LAYOUT
      IT_FIELDCAT              = ITAB_FIELDCAT[]
*     IT_EVENTS                = GT_EVENTS[]
      I_CALLBACK_PF_STATUS_SET = 'P_SET_STATUS'
*      I_SAVE                   = 'A'
      I_CALLBACK_USER_COMMAND  = 'P_USER_COMMAND'
    TABLES
      T_OUTTAB                 = ITAB_TOTAL[]
    EXCEPTIONS
      PROGRAM_ERROR            = 1
      OTHERS                   = 2.
  IF SY-SUBRC <> 0.
    MESSAGE '采用ALV显示报表出错' TYPE 'S'.
    EXIT.

  ENDIF.
ENDFORM.

FORM P_SET_STATUS USING extab TYPE slis_t_extab.
   SET  PF-STATUS  'Z_ST08' .
ENDFORM.

FORM P_USER_COMMAND USING R_UCOMM TYPE SY-UCOMM
                             R_SEL   TYPE SLIS_SELFIELD.  "响应ALV点击自定义按钮之后的事件
  DATA: LV_ANSWER        TYPE C.
  DATA: REF TYPE REF TO CL_GUI_ALV_GRID.
  DATA : LV_STABLE TYPE LVC_S_STBL.
  LV_STABLE-ROW = '1'.
  LV_STABLE-COL = '1'.

  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      E_GRID = REF.
  CALL METHOD REF->CHECK_CHANGED_DATA.

  CASE R_UCOMM.

    WHEN '&IC1'. "单击复选框 勾选(取消)后同一预留号都自动勾选(取消)
      READ TABLE ITAB_TOTAL INDEX R_SEL-tabindex INTO DATA(WA_SEL_TEMP).  "获取单击行
      IF R_SEL-fieldname = 'SEL' AND WA_SEL_TEMP-SEL = ''.
          LOOP AT ITAB_TOTAL ASSIGNING FIELD-SYMBOL() WHERE RSNUM = WA_SEL_TEMP-RSNUM .
            -SEL = 'X'.
          ENDLOOP.
        ELSEIF R_SEL-fieldname = 'SEL' AND WA_SEL_TEMP-SEL = 'X'.
          LOOP AT ITAB_TOTAL ASSIGNING FIELD-SYMBOL() WHERE RSNUM = WA_SEL_TEMP-RSNUM  .
            -SEL = ''.
          ENDLOOP.
      ENDIF.



    WHEN 'BACK'.
      LEAVE TO SCREEN 0.
    WHEN '&F12'.
    WHEN 'SEL_ALL'.
      CLEAR WA_TOTAL.
      LOOP AT ITAB_TOTAL INTO WA_TOTAL WHERE SEL = ''.
        WA_TOTAL-SEL = 'X'.
        MODIFY ITAB_TOTAL FROM WA_TOTAL.
      ENDLOOP.
      CALL METHOD REF->REFRESH_TABLE_DISPLAY
      EXPORTING IS_STABLE = LV_STABLE ..
    WHEN 'DESEL_ALL'.
      CLEAR WA_TOTAL.
      LOOP AT ITAB_TOTAL INTO WA_TOTAL WHERE SEL = 'X'.
        WA_TOTAL-SEL = ''.
        MODIFY ITAB_TOTAL FROM WA_TOTAL.
      ENDLOOP.
      CALL METHOD REF->REFRESH_TABLE_DISPLAY
      EXPORTING IS_STABLE = LV_STABLE .
    WHEN 'PRINT'.
      PERFORM P_PRINT.
   ENDCASE.
      CALL METHOD REF->REFRESH_TABLE_DISPLAY
      EXPORTING IS_STABLE = LV_STABLE .
ENDFORM.


*&---------------------------------------------------------------------*
*&      Form  P_PRINT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM P_PRINT .
  DATA: FM_NAME TYPE FPNAME.
 DATA:L_CONTROL_PARAMETERS TYPE SSFCTRLOP.
  CLEAR WA_TOTAL.

*  ---------------------------------------------------待完成-根据分类调用不同类型表单
  REFRESH ITAB_SF.
  DATA ITAB_SF_TEMP TYPE TABLE OF TY_TOTAL.
  LOOP AT ITAB_TOTAL INTO WA_TOTAL WHERE SEL = 'X'.
    APPEND WA_TOTAL TO ITAB_SF_TEMP.
  ENDLOOP.
  IF ITAB_SF_TEMP IS INITIAL.
    MESSAGE '请勾选需要打印的项目' TYPE 'E'.
  ENDIF.
  DESCRIBE TABLE ITAB_SF_TEMP LINES DATA(SF_LINES).
  LOOP AT ITAB_SF_TEMP INTO DATA(WA_SF_TEMP).
    APPEND WA_SF_TEMP TO ITAB_SF.

    AT END OF RSNUM.
*        IF SY-TABIX = 1.
*
*           L_CONTROL_PARAMETERS-NO_OPEN = SPACE."首次运行时打开打印对话框
*           L_CONTROL_PARAMETERS-NO_CLOSE = 'X'."并且不关闭假脱机请求
*
*          ELSEIF SY-TABIX = SF_LINES .
*
*           L_CONTROL_PARAMETERS-NO_OPEN = 'X'.
*           L_CONTROL_PARAMETERS-NO_CLOSE = SPACE."最后关闭假脱机准备打印
*
*          ELSE.
*           L_CONTROL_PARAMETERS-NO_OPEN = 'X'.
*           L_CONTROL_PARAMETERS-NO_CLOSE = 'X'."
*        ENDIF.


       CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
        EXPORTING
          FORMNAME = 'ZWMFM005'
        IMPORTING
          FM_NAME  = FM_NAME.
        IF SY-SUBRC <> 0.
          MESSAGE '无法找到FUNCTION NAME' TYPE 'S' DISPLAY LIKE 'E'.
          EXIT.
        ENDIF.

        "CALL THE FUNCTION
        CALL FUNCTION FM_NAME
*        EXPORTING
*            CONTROL_PARAMETERS = L_CONTROL_PARAMETERS
          EXCEPTIONS
            FORMATTING_ERROR = 1
            INTERNAL_ERROR   = 2
            SEND_ERROR       = 3
            USER_CANCELED    = 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.

          CLEAR ITAB_SF.

      ENDAT.


  ENDLOOP.

*  LOOP AT ITAB_TOTAL INTO WA_TOTAL WHERE SEL = 'X'.
*    APPEND WA_TOTAL TO ITAB_SF.
*  ENDLOOP.
*
*  IF ITAB_SF IS INITIAL.
*    MESSAGE '请勾选需要打印的项目' TYPE 'E'.
*  ENDIF.
*
*  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
*  EXPORTING
*    FORMNAME = 'ZWMFM005'
*  IMPORTING
*    FM_NAME  = FM_NAME.
*  IF SY-SUBRC <> 0.
*    MESSAGE '无法找到function name' TYPE 'S' DISPLAY LIKE 'E'.
*    EXIT.
*  ENDIF.
*
*  "call the function
*  CALL FUNCTION FM_NAME
*    EXCEPTIONS
*      FORMATTING_ERROR = 1
*      INTERNAL_ERROR   = 2
*      SEND_ERROR       = 3
*      USER_CANCELED    = 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.

ENDFORM.



你可能感兴趣的:(ABAP)