zoukankan      html  css  js  c++  java
  • ALV报表——复选框、点击事件(二)

    目录 

    PS:请自行忽略gif的水印,使用Function REUSE_ALV_FIELDCATALOG_MERGE后注意单行代码不要超过72字节

    一、复选框

    ALV的复选框有两种方式实现,一种是使用Layout属性,另一种是使用Fieldcat属性

    1、Layout实现方式(最简单的实现方式,这种方式能直接使用ALV报表自带的全选和反全选按钮,多选的话需要按住Ctrl再进行鼠标点选)

    代码:

    ************************************************************************
    * Program Name      : ZMMR999
    * Descriptions      :
    * Updates Tables    :
    * Input  Parameters :
    * Output Parameters :
    * Return Codes      :
    * Special Logic     :
    * Includes          :
    ************************************************************************
    * Modification Log
    ************************************************************************
    *   Date   Ver. Programmer   Descriptions
    * -------- ---- ------------ -------------------------------------------
    * 20171223 1.0 xxx Create
    *
    ************************************************************************
    REPORT ZMMR999.
    * Tables Definitions
    ************************************************************************
    *TABLES:.
    TABLES: marc,ekpo.
    ************************************************************************
    * Data Definitions
    ************************************************************************
    DATA: BEGIN OF gt_data OCCURS 0,
        checkbox  TYPE c ,   "复选框
        ebeln LIKE ekpo-ebeln,  "PO
        ebelp LIKE ekpo-ebelp,  "PO项次
    END OF gt_data.
    
    
    TYPE-POOLS: slis.
    DATA: gs_layout TYPE slis_layout_alv,
          gt_fieldcat TYPE slis_t_fieldcat_alv,
          wa_fieldcat TYPE slis_fieldcat_alv.
    
    ************************************************************************
    * Includes Module
    ************************************************************************
    
    ************************************************************************
    * Selection Screen
    ************************************************************************
    SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-b01.
    PARAMETERS: p_werks LIKE marc-werks OBLIGATORY DEFAULT '2000'.
    
    SELECT-OPTIONS: s_ebeln FOR ekpo-ebeln,
                              s_ebelp FOR ekpo-ebelp.
    SELECTION-SCREEN END OF BLOCK b1.
    
    ************************************************************************
    * Initialization
    ************************************************************************
    INITIALIZATION.
    
    ************************************************************************
    * At Selection Screen
    ************************************************************************
    AT SELECTION-SCREEN.
    
    ************************************************************************
    * At Selection Screen Output
    ************************************************************************
    AT SELECTION-SCREEN OUTPUT.
    
    ************************************************************************
    * Report Format
    ************************************************************************
    TOP-OF-PAGE.
    
    END-OF-PAGE.
    ************************************************************************
    * Main Process
    ************************************************************************
    START-OF-SELECTION.
      PERFORM get_data.
      PERFORM display_data.
    END-OF-SELECTION.
    
    FORM get_data.
      DATA: l_index LIKE sy-tabix.
    
      SELECT ebeln ebelp INTO CORRESPONDING FIELDS OF TABLE gt_data
                FROM ekpo
                WHERE bukrs = p_werks
                AND ebeln IN s_ebeln
                AND ebelp IN s_ebelp.
    
    
    ENDFORM.
    
    *&---------------------------------------------------------------------*
    *&      Form  DISPLAY_DATA
    *&---------------------------------------------------------------------*
    FORM display_data.
      DATA: l_repid TYPE sy-repid,
            l_inclname TYPE trdir-name,
            l_index LIKE sy-tabix.
    
      CLEAR: gt_fieldcat[],wa_fieldcat.
    
      l_repid = l_inclname = sy-cprog.
    
      "栏位最适宽度
      gs_layout-colwidth_optimize = 'X'.
      "复选框
      gs_layout-box_fieldname     = 'CHECKBOX'.
    
      CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
        EXPORTING
          I_PROGRAM_NAME         = l_repid
          I_INTERNAL_TABNAME     = 'GT_DATA'
          I_INCLNAME             = l_inclname
        CHANGING
          CT_FIELDCAT            = gt_fieldcat
        EXCEPTIONS
          INCONSISTENT_INTERFACE = 1
          PROGRAM_ERROR          = 2
          OTHERS                 = 3.
    
      LOOP AT gt_fieldcat INTO wa_fieldcat.
        l_index = sy-tabix.
    
        CASE wa_fieldcat-fieldname.
          WHEN 'CHECKBOX'.
            "技术字段,设置’X’后,该字段不会显示,也不能输出,如同MANDT字段一样的效果,不然会多出空列
            wa_fieldcat-tech = 'X'.
        ENDCASE.
    
        MODIFY gt_fieldcat FROM wa_fieldcat INDEX l_index.
      ENDLOOP.
    
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
          I_CALLBACK_PROGRAM       = sy-repid
          IS_LAYOUT                = gs_layout
          IT_FIELDCAT              = gt_fieldcat[]
          I_SAVE                   = 'A'
        TABLES
          T_OUTTAB            =  gt_data
        EXCEPTIONS
          PROGRAM_ERROR            = 1
          OTHERS                   = 2.
    ENDFORM.
    View Code

    运行效果:

    2、Fieldcat实现方式(设置edit属性的时,会多出Layout方式的选择框效果,此复选框在此处无任何作用,未设置edit属性时,复选框在Fiori页面上又显示灰色,感觉不友好;不过Fieldcat实现方式不可使用ALV报表自带的全选和反全选按钮,需要自定义全选和反全选的点击按钮事件)

    代码:

    ************************************************************************
    * Program Name      : ZMMR999
    * Descriptions      :
    * Updates Tables    :
    * Input  Parameters :
    * Output Parameters :
    * Return Codes      :
    * Special Logic     :
    * Includes          :
    ************************************************************************
    * Modification Log
    ************************************************************************
    *   Date   Ver. Programmer   Descriptions
    * -------- ---- ------------ -------------------------------------------
    * 20171223 1.0 xxx Create
    *
    ************************************************************************
    REPORT ZMMR999.
    * Tables Definitions
    ************************************************************************
    *TABLES:.
    TABLES: marc,ekpo.
    ************************************************************************
    * Data Definitions
    ************************************************************************
    DATA: BEGIN OF gt_data OCCURS 0,
        checkbox  TYPE c ,   "复选框
        ebeln LIKE ekpo-ebeln,  "PO
        ebelp LIKE ekpo-ebelp,  "PO项次
    END OF gt_data.
    
    
    TYPE-POOLS: slis.
    DATA: gs_layout TYPE slis_layout_alv,
          gt_fieldcat TYPE slis_t_fieldcat_alv,
          wa_fieldcat TYPE slis_fieldcat_alv.
    
    "ALV自定義按鈕
    CONSTANTS alv_pf_status TYPE slis_formname  VALUE 'ALV_PF_STATUS'.
    *ALV自定義按鈕事件
    CONSTANTS alv_user_command TYPE slis_formname
      VALUE 'ALV_USER_COMMAND'.
    ************************************************************************
    * Includes Module
    ************************************************************************
    
    ************************************************************************
    * Selection Screen
    ************************************************************************
    SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-b01.
    PARAMETERS: p_werks LIKE marc-werks OBLIGATORY DEFAULT '2000'.
    
    SELECT-OPTIONS: s_ebeln FOR ekpo-ebeln,
                              s_ebelp FOR ekpo-ebelp.
    SELECTION-SCREEN END OF BLOCK b1.
    
    ************************************************************************
    * Initialization
    ************************************************************************
    INITIALIZATION.
    
    ************************************************************************
    * At Selection Screen
    ************************************************************************
    AT SELECTION-SCREEN.
    
    ************************************************************************
    * At Selection Screen Output
    ************************************************************************
    AT SELECTION-SCREEN OUTPUT.
    
    ************************************************************************
    * Report Format
    ************************************************************************
    TOP-OF-PAGE.
    
    END-OF-PAGE.
    ************************************************************************
    * Main Process
    ************************************************************************
    START-OF-SELECTION.
      PERFORM get_data.
      PERFORM display_data.
    END-OF-SELECTION.
    
    FORM get_data.
      DATA: l_index LIKE sy-tabix.
    
      SELECT ebeln ebelp INTO CORRESPONDING FIELDS OF TABLE gt_data
                FROM ekpo
                WHERE bukrs = p_werks
                AND ebeln IN s_ebeln
                AND ebelp IN s_ebelp.
    
    
    ENDFORM.
    
    *&---------------------------------------------------------------------*
    *&      Form  DISPLAY_DATA
    *&---------------------------------------------------------------------*
    FORM display_data.
      DATA: l_repid TYPE sy-repid,
            l_inclname TYPE trdir-name,
            l_index LIKE sy-tabix.
    
      CLEAR: gt_fieldcat[],wa_fieldcat.
    
      l_repid = l_inclname = sy-cprog.
    
      "栏位最适宽度
      gs_layout-colwidth_optimize = 'X'.
      "复选框
      "gs_layout-box_fieldname     = 'CHECKBOX'.
    
      CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
        EXPORTING
          i_program_name         = l_repid
          i_internal_tabname     = 'GT_DATA'
          i_inclname             = l_inclname
        CHANGING
          CT_FIELDCAT            = gt_fieldcat
        EXCEPTIONS
          inconsistent_interface = 1
          program_error          = 2
          OTHERS                 = 3.
    
      LOOP AT gt_fieldcat INTO wa_fieldcat.
        l_index = sy-tabix.
    
        CASE wa_fieldcat-fieldname.
          WHEN 'CHECKBOX'.
            "技术字段,设置’X’后,该字段不会显示,也不能输出,如同MANDT字段一样的效果
            "wa_fieldcat-tech = 'X'.
    
            wa_fieldcat-seltext_s = wa_fieldcat-seltext_m =
            wa_fieldcat-seltext_l = '复选框'.
            wa_fieldcat-fix_column = 'X'.
            wa_fieldcat-checkbox = 'X'.
            wa_fieldcat-hotspot = 'X'.
        ENDCASE.
    
        MODIFY gt_fieldcat FROM wa_fieldcat INDEX l_index.
      ENDLOOP.
    
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
          i_callback_program       = sy-repid
          is_layout                = gs_layout
          it_fieldcat              = gt_fieldcat[]
          i_callback_pf_status_set = 'ALV_PF_STATUS'
          i_callback_user_command  = 'ALV_USER_COMMAND'
          i_save                   = 'A'
        TABLES
          t_outtab            =  gt_data
        EXCEPTIONS
          program_error            = 1
          OTHERS                   = 2.
    ENDFORM.
    
    FORM alv_pf_status USING rt_extab TYPE slis_t_extab.
      SET  PF-STATUS  'STATUS_POST' .
    ENDFORM.                    " ALV_PF_STATUS
    
    FORM alv_user_command USING in_ucomm LIKE sy-ucomm
          in_selfield TYPE slis_selfield.
    
      DATA: ref_grid TYPE REF TO cl_gui_alv_grid,
                ls_stbl TYPE lvc_s_stbl,
                wa_data LIKE gt_data.
    
      DATA: l_index LIKE sy-index.
    
      CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
        IMPORTING
          e_grid = ref_grid.
    
      CALL METHOD ref_grid->check_changed_data.
    
      "稳定刷新,保持滚动条位置
      ls_stbl-row = ls_stbl-col = 'X'.
    
      CASE in_ucomm.
        WHEN '&ZALL'. "全选
          LOOP AT gt_data.
            l_index = sy-tabix.
            gt_data-checkbox = 'X'.
            MODIFY gt_data INDEX l_index.
          ENDLOOP.
          CALL METHOD ref_grid->refresh_table_display "刷新ALV data
            EXPORTING
              is_stable = ls_stbl.
    
        WHEN '&ZSAL'. "反选
          LOOP AT gt_data.
            l_index = sy-tabix.
            gt_data-checkbox = space.
            MODIFY gt_data INDEX l_index.
          ENDLOOP.
          CALL METHOD ref_grid->refresh_table_display "刷新ALV data
            EXPORTING
              is_stable = ls_stbl.
    
        WHEN '&IC1'.  "单选,如果未设置 wa_fieldcat-hotspot,则为行双击
          READ TABLE gt_data INTO wa_data INDEX in_selfield-tabindex.
          LOOP AT gt_data.
            l_index = sy-tabix.
            IF l_index = in_selfield-tabindex.
              IF in_selfield-fieldname = 'CHECKBOX'
                AND wa_data-checkbox = space.
                gt_data-checkbox = 'X'.
              ELSEIF in_selfield-fieldname = 'CHECKBOX'
                 AND wa_data-checkbox = 'X'.
                gt_data-checkbox = space.
              ENDIF.
              MODIFY gt_data INDEX l_index.
              EXIT.
            ENDIF.
          ENDLOOP.
          CALL METHOD ref_grid->refresh_table_display
            EXPORTING
              is_stable = ls_stbl.
         " cl_demo_output=>write( in_selfield-tabindex ).
          "cl_demo_output=>display(  ).
    
        WHEN '&DATA'.
          cl_demo_output=>write( gt_data[] ).
          cl_demo_output=>display(  ).
      ENDCASE.
    ENDFORM.
    View Code

    自定义按钮:

    运行效果:

    3、拓展(单选某张PO的项次则改PO所有的项次全选)

    代码:

    ************************************************************************
    * Program Name      : ZMMR999
    * Descriptions      :
    * Updates Tables    :
    * Input  Parameters :
    * Output Parameters :
    * Return Codes      :
    * Special Logic     :
    * Includes          :
    ************************************************************************
    * Modification Log
    ************************************************************************
    *   Date   Ver. Programmer   Descriptions
    * -------- ---- ------------ -------------------------------------------
    * 20171223 1.0 xxx Create
    *
    ************************************************************************
    REPORT ZMMR999.
    * Tables Definitions
    ************************************************************************
    *TABLES:.
    TABLES: marc,ekpo.
    ************************************************************************
    * Data Definitions
    ************************************************************************
    DATA: BEGIN OF gt_data OCCURS 0,
        checkbox  TYPE c ,   "复选框
        ebeln LIKE ekpo-ebeln,  "PO
        ebelp LIKE ekpo-ebelp,  "PO项次
    END OF gt_data.
    
    
    TYPE-POOLS: slis.
    DATA: gs_layout TYPE slis_layout_alv,
          gt_fieldcat TYPE slis_t_fieldcat_alv,
          wa_fieldcat TYPE slis_fieldcat_alv.
    
    "ALV自定義按鈕
    CONSTANTS alv_pf_status TYPE slis_formname  VALUE 'ALV_PF_STATUS'.
    *ALV自定義按鈕事件
    CONSTANTS alv_user_command TYPE slis_formname
      VALUE 'ALV_USER_COMMAND'.
    ************************************************************************
    * Includes Module
    ************************************************************************
    
    ************************************************************************
    * Selection Screen
    ************************************************************************
    SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-b01.
    PARAMETERS: p_werks LIKE marc-werks OBLIGATORY DEFAULT '2000'.
    
    SELECT-OPTIONS: s_ebeln FOR ekpo-ebeln,
                              s_ebelp FOR ekpo-ebelp.
    SELECTION-SCREEN END OF BLOCK b1.
    
    ************************************************************************
    * Initialization
    ************************************************************************
    INITIALIZATION.
    
    ************************************************************************
    * At Selection Screen
    ************************************************************************
    AT SELECTION-SCREEN.
    
    ************************************************************************
    * At Selection Screen Output
    ************************************************************************
    AT SELECTION-SCREEN OUTPUT.
    
    ************************************************************************
    * Report Format
    ************************************************************************
    TOP-OF-PAGE.
    
    END-OF-PAGE.
    ************************************************************************
    * Main Process
    ************************************************************************
    START-OF-SELECTION.
      PERFORM get_data.
      PERFORM display_data.
    END-OF-SELECTION.
    
    FORM get_data.
      DATA: l_index LIKE sy-tabix.
    
      SELECT ebeln ebelp INTO CORRESPONDING FIELDS OF TABLE gt_data
                FROM ekpo
                WHERE bukrs = p_werks
                AND ebeln IN s_ebeln
                AND ebelp IN s_ebelp.
    
      SORT gt_data BY ebeln ebelp.
    ENDFORM.
    
    *&---------------------------------------------------------------------*
    *&      Form  DISPLAY_DATA
    *&---------------------------------------------------------------------*
    FORM display_data.
      DATA: l_repid TYPE sy-repid,
            l_inclname TYPE trdir-name,
            l_index LIKE sy-tabix.
    
      CLEAR: gt_fieldcat[],wa_fieldcat.
    
      l_repid = l_inclname = sy-cprog.
    
      "栏位最适宽度
      gs_layout-colwidth_optimize = 'X'.
      "复选框
      "gs_layout-box_fieldname     = 'CHECKBOX'.
    
      CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
        EXPORTING
          i_program_name         = l_repid
          i_internal_tabname     = 'GT_DATA'
          i_inclname             = l_inclname
        CHANGING
          CT_FIELDCAT            = gt_fieldcat
        EXCEPTIONS
          inconsistent_interface = 1
          program_error          = 2
          OTHERS                 = 3.
    
      LOOP AT gt_fieldcat INTO wa_fieldcat.
        l_index = sy-tabix.
    
        CASE wa_fieldcat-fieldname.
          WHEN 'CHECKBOX'.
            "技术字段,设置’X’后,该字段不会显示,也不能输出,如同MANDT字段一样的效果
            "wa_fieldcat-tech = 'X'.
    
            wa_fieldcat-seltext_s = wa_fieldcat-seltext_m =
            wa_fieldcat-seltext_l = '复选框'.
            wa_fieldcat-fix_column = 'X'.
            wa_fieldcat-checkbox = 'X'.
            wa_fieldcat-hotspot = 'X'.
        ENDCASE.
    
        MODIFY gt_fieldcat FROM wa_fieldcat INDEX l_index.
      ENDLOOP.
    
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
          i_callback_program       = sy-repid
          is_layout                = gs_layout
          it_fieldcat              = gt_fieldcat[]
          i_callback_pf_status_set = 'ALV_PF_STATUS'
          i_callback_user_command  = 'ALV_USER_COMMAND'
          i_save                   = 'A'
        TABLES
          t_outtab            =  gt_data
        EXCEPTIONS
          program_error            = 1
          OTHERS                   = 2.
    ENDFORM.
    
    FORM alv_pf_status USING rt_extab TYPE slis_t_extab.
      SET  PF-STATUS  'STATUS_POST' .
    ENDFORM.                    " ALV_PF_STATUS
    
    FORM alv_user_command USING in_ucomm LIKE sy-ucomm
          in_selfield TYPE slis_selfield.
    
      DATA: ref_grid TYPE REF TO cl_gui_alv_grid,
                ls_stbl TYPE lvc_s_stbl,
                wa_data LIKE gt_data.
    
      DATA: l_index LIKE sy-index.
    
      CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
        IMPORTING
          e_grid = ref_grid.
      CALL METHOD ref_grid->check_changed_data.
    
      "稳定刷新,保持滚动条位置
      ls_stbl-row = ls_stbl-col = 'X'.
    
      CASE in_ucomm.
        WHEN '&ZALL'. "全选
          LOOP AT gt_data.
            l_index = sy-tabix.
            gt_data-checkbox = 'X'.
            MODIFY gt_data INDEX l_index.
          ENDLOOP.
          CALL METHOD ref_grid->refresh_table_display "刷新ALV data
            EXPORTING
              is_stable = ls_stbl.
    
        WHEN '&ZSAL'. "反选
          LOOP AT gt_data.
            l_index = sy-tabix.
            gt_data-checkbox = space.
            MODIFY gt_data INDEX l_index.
          ENDLOOP.
          CALL METHOD ref_grid->refresh_table_display "刷新ALV data
            EXPORTING
              is_stable = ls_stbl.
    
        WHEN '&IC1'.  "单选,如果未设置 wa_fieldcat-hotspot,则为行双击
          READ TABLE gt_data INTO wa_data INDEX in_selfield-tabindex.
          IF in_selfield-fieldname = 'CHECKBOX'
            AND wa_data-checkbox = space.
            LOOP AT gt_data ASSIGNING FIELD-SYMBOL(<fs_temp>)
              WHERE ebeln = wa_data-ebeln.
              <fs_temp>-checkbox = 'X'.
            ENDLOOP.
          ELSEIF in_selfield-fieldname = 'CHECKBOX'
            AND wa_data-checkbox = 'X'.
            LOOP AT gt_data ASSIGNING FIELD-SYMBOL(<fs_temp2>)
              WHERE ebeln = wa_data-ebeln.
              <fs_temp2>-checkbox = space.
            ENDLOOP.
          ENDIF.
          CALL METHOD ref_grid->refresh_table_display "刷新ALV data
            EXPORTING
              is_stable = ls_stbl.
         " cl_demo_output=>write( in_selfield-tabindex ).
          "cl_demo_output=>display(  ).
    
        WHEN '&DATA'.
          cl_demo_output=>write( gt_data[] ).
          cl_demo_output=>display(  ).
      ENDCASE.
    ENDFORM.
    View Code

    运行效果:

    二、点击事件

    1、行双击:

    代码:

    ************************************************************************
    * Program Name      : ZMMR999
    * Descriptions      :
    * Updates Tables    :
    * Input  Parameters :
    * Output Parameters :
    * Return Codes      :
    * Special Logic     :
    * Includes          :
    ************************************************************************
    * Modification Log
    ************************************************************************
    *   Date   Ver. Programmer   Descriptions
    * -------- ---- ------------ -------------------------------------------
    * 20171223 1.0 xxx Create
    *
    ************************************************************************
    REPORT ZMMR999.
    * Tables Definitions
    ************************************************************************
    *TABLES:.
    TABLES: marc,ekpo.
    ************************************************************************
    * Data Definitions
    ************************************************************************
    DATA: BEGIN OF gt_data OCCURS 0,
    *    checkbox  TYPE c ,   "复选框
        ebeln LIKE ekpo-ebeln,  "PO
        ebelp LIKE ekpo-ebelp,  "PO项次
    END OF gt_data.
    
    
    TYPE-POOLS: slis.
    DATA: gs_layout TYPE slis_layout_alv,
          gt_fieldcat TYPE slis_t_fieldcat_alv,
          wa_fieldcat TYPE slis_fieldcat_alv.
    
    *ALV自定義按鈕事件
    CONSTANTS alv_user_command TYPE slis_formname
      VALUE 'ALV_USER_COMMAND'.
    
    ************************************************************************
    * Includes Module
    ************************************************************************
    
    ************************************************************************
    * Selection Screen
    ************************************************************************
    SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-b01.
    PARAMETERS: p_werks LIKE marc-werks OBLIGATORY DEFAULT '2000'.
    
    SELECT-OPTIONS: s_ebeln FOR ekpo-ebeln,
                              s_ebelp FOR ekpo-ebelp.
    SELECTION-SCREEN END OF BLOCK b1.
    
    ************************************************************************
    * Initialization
    ************************************************************************
    INITIALIZATION.
    
    ************************************************************************
    * At Selection Screen
    ************************************************************************
    AT SELECTION-SCREEN.
    
    ************************************************************************
    * At Selection Screen Output
    ************************************************************************
    AT SELECTION-SCREEN OUTPUT.
    
    ************************************************************************
    * Report Format
    ************************************************************************
    TOP-OF-PAGE.
    
    END-OF-PAGE.
    ************************************************************************
    * Main Process
    ************************************************************************
    START-OF-SELECTION.
      PERFORM get_data.
      PERFORM display_data.
    END-OF-SELECTION.
    
    FORM get_data.
      DATA: l_index LIKE sy-tabix.
    
      SELECT ebeln ebelp INTO CORRESPONDING FIELDS OF TABLE gt_data
                FROM ekpo
                WHERE bukrs = p_werks
                AND ebeln IN s_ebeln
                AND ebelp IN s_ebelp.
    
      SORT gt_data BY ebeln ebelp.
    ENDFORM.
    
    *&---------------------------------------------------------------------*
    *&      Form  DISPLAY_DATA
    *&---------------------------------------------------------------------*
    FORM display_data.
      DATA: l_repid TYPE sy-repid,
            l_inclname TYPE trdir-name,
            l_index LIKE sy-tabix.
    
      CLEAR: gt_fieldcat[],wa_fieldcat.
    
      l_repid = l_inclname = sy-cprog.
    
      "栏位最适宽度
      gs_layout-colwidth_optimize = 'X'.
      "复选框
    *  gs_layout-box_fieldname     = 'CHECKBOX'.
    
      CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
        EXPORTING
          I_PROGRAM_NAME         = l_repid
          I_INTERNAL_TABNAME     = 'GT_DATA'
          I_INCLNAME             = l_inclname
        CHANGING
          CT_FIELDCAT            = gt_fieldcat
        EXCEPTIONS
          INCONSISTENT_INTERFACE = 1
          PROGRAM_ERROR          = 2
          OTHERS                 = 3.
    
      LOOP AT gt_fieldcat INTO wa_fieldcat.
        l_index = sy-tabix.
    
        CASE wa_fieldcat-fieldname.
    *      WHEN 'CHECKBOX'.
    *        "技术字段,设置’X’后,该字段不会显示,也不能输出,如同MANDT字段一样的效果,不然会多出空列
    *        wa_fieldcat-tech = 'X'.
        ENDCASE.
    
        MODIFY gt_fieldcat FROM wa_fieldcat INDEX l_index.
      ENDLOOP.
    
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
          I_CALLBACK_PROGRAM       = sy-repid
          IS_LAYOUT                = gs_layout
          IT_FIELDCAT              = gt_fieldcat[]
          I_CALLBACK_USER_COMMAND  = 'ALV_USER_COMMAND'
          I_SAVE                   = 'A'
        TABLES
          T_OUTTAB            =  gt_data
        EXCEPTIONS
          PROGRAM_ERROR            = 1
          OTHERS                   = 2.
    ENDFORM.
    
    FORM alv_user_command USING in_ucomm LIKE sy-ucomm
          in_selfield TYPE slis_selfield.
      DATA: wa_data LIKE gt_data.
      CASE in_ucomm.
        WHEN '&IC1'.  "行双击
          READ TABLE gt_data INTO wa_data INDEX in_selfield-tabindex.
          cl_demo_output=>write( wa_data ).
          cl_demo_output=>display(  ).
      ENDCASE.
    ENDFORM.
    View Code

    运行效果:

    2、单元格双击事件(双击PO进入ME23N显示PO,双击物料进入MM03显示物料)

    代码:

    ************************************************************************
    * Program Name      : ZMMR999
    * Descriptions      :
    * Updates Tables    :
    * Input  Parameters :
    * Output Parameters :
    * Return Codes      :
    * Special Logic     :
    * Includes          :
    ************************************************************************
    * Modification Log
    ************************************************************************
    *   Date   Ver. Programmer   Descriptions
    * -------- ---- ------------ -------------------------------------------
    * 20171223 1.0 xxx Create
    *
    ************************************************************************
    REPORT ZMMR999.
    * Tables Definitions
    ************************************************************************
    *TABLES:.
    TABLES: marc,ekpo.
    ************************************************************************
    * Data Definitions
    ************************************************************************
    DATA: BEGIN OF gt_data OCCURS 0,
    *    checkbox  TYPE c ,   "复选框
        ebeln LIKE ekpo-ebeln,  "PO
        ebelp LIKE ekpo-ebelp,  "PO项次
    END OF gt_data.
    
    
    TYPE-POOLS: slis.
    DATA: gs_layout TYPE slis_layout_alv,
          gt_fieldcat TYPE slis_t_fieldcat_alv,
          wa_fieldcat TYPE slis_fieldcat_alv.
    
    *ALV自定義按鈕事件
    CONSTANTS alv_user_command TYPE slis_formname
      VALUE 'ALV_USER_COMMAND'.
    
    ************************************************************************
    * Includes Module
    ************************************************************************
    
    ************************************************************************
    * Selection Screen
    ************************************************************************
    SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-b01.
    PARAMETERS: p_werks LIKE marc-werks OBLIGATORY DEFAULT '2000'.
    
    SELECT-OPTIONS: s_ebeln FOR ekpo-ebeln,
                              s_ebelp FOR ekpo-ebelp.
    SELECTION-SCREEN END OF BLOCK b1.
    
    ************************************************************************
    * Initialization
    ************************************************************************
    INITIALIZATION.
    
    ************************************************************************
    * At Selection Screen
    ************************************************************************
    AT SELECTION-SCREEN.
    
    ************************************************************************
    * At Selection Screen Output
    ************************************************************************
    AT SELECTION-SCREEN OUTPUT.
    
    ************************************************************************
    * Report Format
    ************************************************************************
    TOP-OF-PAGE.
    
    END-OF-PAGE.
    ************************************************************************
    * Main Process
    ************************************************************************
    START-OF-SELECTION.
      PERFORM get_data.
      PERFORM display_data.
    END-OF-SELECTION.
    
    FORM get_data.
      DATA: l_index LIKE sy-tabix.
    
      SELECT ebeln ebelp INTO CORRESPONDING FIELDS OF TABLE gt_data
                FROM ekpo
                WHERE bukrs = p_werks
                AND ebeln IN s_ebeln
                AND ebelp IN s_ebelp.
    
      SORT gt_data BY ebeln ebelp.
    ENDFORM.
    
    *&---------------------------------------------------------------------*
    *&      Form  DISPLAY_DATA
    *&---------------------------------------------------------------------*
    FORM display_data.
      DATA: l_repid TYPE sy-repid,
            l_inclname TYPE trdir-name,
            l_index LIKE sy-tabix.
    
      CLEAR: gt_fieldcat[],wa_fieldcat.
    
      l_repid = l_inclname = sy-cprog.
    
      "栏位最适宽度
      gs_layout-colwidth_optimize = 'X'.
      "复选框
    *  gs_layout-box_fieldname     = 'CHECKBOX'.
    
      CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
        EXPORTING
          I_PROGRAM_NAME         = l_repid
          I_INTERNAL_TABNAME     = 'GT_DATA'
          I_INCLNAME             = l_inclname
        CHANGING
          CT_FIELDCAT            = gt_fieldcat
        EXCEPTIONS
          INCONSISTENT_INTERFACE = 1
          PROGRAM_ERROR          = 2
          OTHERS                 = 3.
    
      LOOP AT gt_fieldcat INTO wa_fieldcat.
        l_index = sy-tabix.
    
        CASE wa_fieldcat-fieldname.
    *      WHEN 'CHECKBOX'.
    *        "技术字段,设置’X’后,该字段不会显示,也不能输出,如同MANDT字段一样的效果,不然会多出空列
    *        wa_fieldcat-tech = 'X'.
        ENDCASE.
    
        MODIFY gt_fieldcat FROM wa_fieldcat INDEX l_index.
      ENDLOOP.
    
      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
          I_CALLBACK_PROGRAM       = sy-repid
          IS_LAYOUT                = gs_layout
          IT_FIELDCAT              = gt_fieldcat[]
          I_CALLBACK_USER_COMMAND  = 'ALV_USER_COMMAND'
          I_SAVE                   = 'A'
        TABLES
          T_OUTTAB            =  gt_data
        EXCEPTIONS
          PROGRAM_ERROR            = 1
          OTHERS                   = 2.
    ENDFORM.
    
    FORM alv_user_command USING in_ucomm LIKE sy-ucomm
          in_selfield TYPE slis_selfield.
      DATA: wa_data LIKE gt_data.
      CASE in_ucomm.
        WHEN '&IC1'.  "行双击
          CASE in_selfield-fieldname.  "双击的栏位名
            WHEN 'EBELN'.
              "PARAMETER ID通过F1->technical information->Parameter id可以获得
              SET PARAMETER ID: 'BES' FIELD in_selfield-value.
              CALL TRANSACTION 'ME23N' AND SKIP FIRST SCREEN.
          ENDCASE.
      ENDCASE.
    ENDFORM.
    View Code 

    运行效果:

    木叶飞舞之处,火亦生生不息
  • 相关阅读:
    Spiral Matrix
    Merge Intervals
    Edit Distance
    Insertion Sort List
    Add Binary
    Partition List
    Binary Tree Postorder Traversal
    单向链表反转
    Facebook Hacker cup Qualification round Problem 1
    判断二叉树是否为平衡二叉树
  • 原文地址:https://www.cnblogs.com/StephenAmell/p/8093289.html
Copyright © 2011-2022 走看看