欢迎来到萧静默的博客

书山有路勤为径,学海无涯苦作舟。

SAL实战练习-全选及按钮事件等

*&---------------------------------------------------------------------*
*& Report ZSDRP122
*&---------------------------------------------------------------------*
*&data of MRB query    create by Bruce King 20210607
*&---------------------------------------------------------------------*
REPORT ZSDRP122.
TABLES:ZTSD042.

SELECT-OPTIONS: S_MRB       FOR     ZTSD042-MRBNUM,
                S_KUNNR     FOR     ZTSD042-KUNNR,
                S_MATNR     FOR     ZTSD042-MATNR,
                S_WERKS     FOR     ZTSD042-WERKS,
                S_CHARG     FOR     ZTSD042-CHARG,
                S_VBELN     FOR     ZTSD042-VBELN,
                S_POSNR     FOR     ZTSD042-POSNR,
                S_USER      FOR     ZTSD042-USERNAME,
                S_ZDATE     FOR     ZTSD042-ZDATE.

CLASS SALV_DISPLAY DEFINITION.
  PUBLIC SECTION.
    METHODS:DATA_DIAPLAY.

  PRIVATE SECTION.
    TYPES:BEGIN OF TY_DATA,
            CHECKBOX TYPE CHAR1,
            MRBNUM   TYPE ZTSD042-MRBNUM,
            KUNNR    TYPE ZTSD042-KUNNR,
            MATNR    TYPE ZTSD042-MATNR,
            WERKS    TYPE ZTSD042-WERKS,
            CHARG    TYPE ZTSD042-CHARG,
            VBELN    TYPE ZTSD042-VBELN,
            POSNR    TYPE ZTSD042-POSNR,
            USERNAME TYPE ZTSD042-USERNAME,
            ZDATE    TYPE ZTSD042-ZDATE,
            ZTIME    TYPE ZTSD042-ZTIME,
            ZFLAG    TYPE ZTSD042-ZFLAG,
            REMARKS  TYPE ZTSD042-REMARKS,
          END OF TY_DATA.
    DATA:LT_DATA TYPE STANDARD TABLE OF TY_DATA,
         WA_DATA TYPE TY_DATA.
    DATA:LR_FUNCTIONS           TYPE REF TO CL_SALV_FUNCTIONS,
         LR_EVENTS              TYPE REF TO CL_SALV_EVENTS_TABLE,
         LR_COLUMNS             TYPE REF TO CL_SALV_COLUMNS_TABLE,
         LR_COLUMN              TYPE REF TO CL_SALV_COLUMN_TABLE,
         LR_DISPLAY             TYPE REF TO CL_SALV_DISPLAY_SETTINGS,
         LR_FUNCTIONAL_SETTINGS TYPE REF TO CL_SALV_FUNCTIONAL_SETTINGS.
    DATA: S_ALV TYPE REF TO CL_SALV_TABLE.
    DATA:ERR_TEXT TYPE STRING.
    DATA: LCX_ERROR TYPE REF TO CX_ROOT. "cx_sy_sql_error . "cx_sy_open_sql_db.
    METHODS:
      COLUMN_SET CHANGING P_ALV TYPE REF TO CL_SALV_TABLE,
      USER_COMMAND FOR EVENT ADDED_FUNCTION OF CL_SALV_EVENTS_TABLE"直接使用该类的function事件
        IMPORTING E_SALV_FUNCTION,
      ON_LINK_CLICK FOR EVENT LINK_CLICK OF CL_SALV_EVENTS_TABLE IMPORTING
                                                                     ROW"事件触发所在的行号
                                                                     COLUMN,"事件触发所在的列名
      HOTPOT_CLICK FOR EVENT LINK_CLICK OF CL_SALV_EVENTS_TABLE IMPORTING
                                                                    ROW "事件触发所在的行号
                                                                    COLUMN,"事件触发所在的列名
      DOUBLE_CLICK FOR EVENT DOUBLE_CLICK OF CL_SALV_EVENTS_TABLE IMPORTING
                                                                      ROW "事件触发所在的行号
                                                                      COLUMN."事件触发所在的列名

    METHODS: SET_PF_STATUS CHANGING CO_ALV TYPE REF TO CL_SALV_TABLE.
ENDCLASS.

CLASS SALV_DISPLAY IMPLEMENTATION.
  METHOD DATA_DIAPLAY.
    SELECT * FROM ZTSD042 INTO CORRESPONDING FIELDS OF TABLE LT_DATA
    WHERE MRBNUM IN S_MRB
    AND   KUNNR  IN S_KUNNR
    AND   MATNR  IN S_MATNR
    AND   WERKS  IN S_WERKS
    AND   CHARG  IN S_CHARG
    AND   VBELN  IN S_VBELN
    AND   POSNR  IN S_POSNR
    AND   USERNAME   IN S_USER
    AND   ZDATE  IN S_ZDATE.
    LOOP AT LT_DATA ASSIGNING FIELD-SYMBOL(<FS_DATA>) WHERE ZFLAG = 'X'.
      <FS_DATA>-ZFLAG = ''.
    ENDLOOP.
    SORT LT_DATA BY MRBNUM KUNNR MATNR CHARG.

    TRY .
        CL_SALV_TABLE=>FACTORY( IMPORTING R_SALV_TABLE = S_ALV CHANGING T_TABLE = LT_DATA ).
      CATCH CX_SALV_MSG.
    ENDTRY.

    CALL METHOD SET_PF_STATUS
      CHANGING
        CO_ALV = S_ALV.

*    LR_FUNCTIONS = S_ALV->GET_FUNCTIONS( ).
*    LR_FUNCTIONS->SET_ALL( ABAP_TRUE ).
    LR_DISPLAY = S_ALV->GET_DISPLAY_SETTINGS( ).
    LR_DISPLAY->SET_LIST_HEADER( 'MRB数据查询' ). "设置ALV的抬头
    LR_DISPLAY->SET_FIT_COLUMN_TO_TABLE_SIZE( 'X')."列自适应表格宽度

    ME->COLUMN_SET( CHANGING P_ALV = S_ALV ).

    "=====事件注册
*    DATA:HANDLER TYPE REF TO LCL_HANDLE_EVENTS.
    LR_EVENTS = S_ALV->GET_EVENT( )."注册事件至类
*    CREATE OBJECT HANDLER.
    SET HANDLER ME->HOTPOT_CLICK FOR LR_EVENTS.
    SET HANDLER ME->DOUBLE_CLICK FOR LR_EVENTS.
    SET HANDLER ME->ON_LINK_CLICK FOR LR_EVENTS.
    SET HANDLER ME->USER_COMMAND FOR LR_EVENTS.
    S_ALV->DISPLAY( ).
  ENDMETHOD.
  METHOD COLUMN_SET.
    LR_COLUMNS = S_ALV->GET_COLUMNS( ).
    LR_COLUMNS->SET_OPTIMIZE( 'X' ).
    LR_FUNCTIONAL_SETTINGS = S_ALV->GET_FUNCTIONAL_SETTINGS( ).
    TRY .
        "设置复选框栏位
        LR_COLUMN ?= LR_COLUMNS->GET_COLUMN( 'CHECKBOX' ).
        LR_COLUMN->SET_CELL_TYPE( IF_SALV_C_CELL_TYPE=>CHECKBOX_HOTSPOT ).
        LR_COLUMN->SET_LONG_TEXT( '选择' ).
        LR_COLUMN ?= LR_COLUMNS->GET_COLUMN( 'ZFLAG' ).
        LR_COLUMN->SET_LONG_TEXT( 'MRB号是否使用' )."修改列名
        LR_COLUMN ?= LR_COLUMNS->GET_COLUMN( 'REMARKS' ).
        LR_COLUMN->SET_LONG_TEXT( '备注' )."修改列名
        LR_COLUMN ?= LR_COLUMNS->GET_COLUMN( 'MRBNUM' ).
        LR_COLUMN->SET_LONG_TEXT( 'MRB流水号' )."修改列名
      CATCH CX_SALV_NOT_FOUND.
      CATCH CX_SALV_DATA_ERROR.
      CATCH CX_SALV_EXISTING.
    ENDTRY.
  ENDMETHOD.
  METHOD USER_COMMAND.
*    PERFORM USER_CLICK USING E_SALV_FUNCTION.
    IF E_SALV_FUNCTION = '&CLICK'.
      READ TABLE ME->LT_DATA ASSIGNING FIELD-SYMBOL(<FS_DATA>) WITH KEY CHECKBOX = 'X'."读当选中行
      IF SY-SUBRC NE 0.
        MESSAGE '你没有选中任何数据!' TYPE 'I'.
      ENDIF.

      LOOP AT LT_DATA INTO DATA(WA_DATA) WHERE CHECKBOX = 'X' AND VBELN IS NOT INITIAL.
        TRY .

            UPDATE ZTSD042 SET VBELN = '' POSNR = '' ZFLAG = '' WHERE VBELN = WA_DATA-VBELN AND POSNR = WA_DATA-POSNR AND ZFLAG = 'X'.
            WA_DATA-VBELN = ''.
            WA_DATA-POSNR = ''.
            WA_DATA-ZFLAG = ''.
            MODIFY LT_DATA FROM WA_DATA TRANSPORTING VBELN POSNR ZFLAG.
            IF SY-SUBRC = 0.
              COMMIT WORK.
            ELSE.
              ROLLBACK WORK.
            ENDIF.
          CATCH CX_SY_OPEN_SQL_DB INTO LCX_ERROR.
            ERR_TEXT = LCX_ERROR->GET_TEXT( ). "得到错误信息
        ENDTRY.
        ME->S_ALV->REFRESH( ).
        CLEAR: WA_DATA.
      ENDLOOP.
    ELSEIF E_SALV_FUNCTION = '&SELALL'.
      LOOP AT LT_DATA ASSIGNING FIELD-SYMBOL(<FS_DATA_SELALL>) WHERE CHECKBOX NE 'X'.
        <FS_DATA_SELALL>-CHECKBOX = 'X'.
      ENDLOOP.
      ME->S_ALV->REFRESH( ).
    ELSEIF E_SALV_FUNCTION = '&CANCEL'.
      LOOP AT LT_DATA ASSIGNING FIELD-SYMBOL(<FS_DATA_CANCEL>) WHERE CHECKBOX EQ 'X'.
        <FS_DATA_CANCEL>-CHECKBOX = ''.
        ME->S_ALV->REFRESH( ).
      ENDLOOP.
    ENDIF.
*    ME->S_ALV->REFRESH( ).
  ENDMETHOD.
  METHOD HOTPOT_CLICK.
*    MESSAGE '你单击了我' TYPE 'I'.
*    READ TABLE ME->LT_DATA INTO WA_DATA INDEX ROW."读当前行
*    CASE COLUMN."取列名
*      WHEN 'VBELN'.
*        SET PARAMETER ID 'AUN'  FIELD WA_DATA-VBELN.
*        CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN.
*    ENDCASE.
  ENDMETHOD.
  METHOD DOUBLE_CLICK.
    MESSAGE '你双击了我' TYPE 'I'.
*    READ TABLE ME->LT_DATA INTO WA_DATA INDEX ROW."读当前行
*    CASE COLUMN."取列名
*      WHEN 'ERNAM'.
*        SET PARAMETER ID 'XUS'  FIELD WA_DATA-ERNAM.
*        CALL TRANSACTION 'ZSD053' AND SKIP FIRST SCREEN.
*    ENDCASE.
  ENDMETHOD.
  METHOD ON_LINK_CLICK."复选框选中操作
    READ TABLE ME->LT_DATA ASSIGNING FIELD-SYMBOL(<FS_DATA>) INDEX ROW."读当前行
    IF <FS_DATA>-CHECKBOX EQ ''.
      <FS_DATA>-CHECKBOX = 'X'.
    ELSE.
      <FS_DATA>-CHECKBOX = ''.
    ENDIF.
    ME->S_ALV->REFRESH( ).
  ENDMETHOD.
  METHOD SET_PF_STATUS.
    "设置ALV按钮功能
    S_ALV->SET_SCREEN_STATUS(
    PFSTATUS      =  'SALV_STANDARD1'
    REPORT        =  SY-REPID
    SET_FUNCTIONS = S_ALV->C_FUNCTIONS_ALL ).
  ENDMETHOD.
ENDCLASS.


START-OF-SELECTION.
  DATA:LR_DISPLAY TYPE REF TO SALV_DISPLAY.
  CREATE OBJECT LR_DISPLAY.
  LR_DISPLAY->DATA_DIAPLAY( ).

 

posted @ 2021-06-23 16:06  萧静默  阅读(244)  评论(0编辑  收藏  举报