ALV在标准按钮上增加自定义按钮,并实现文本及功能互相切换

  1. 创建实现按钮添加和按钮响应的类方法:
    *&---------------------------------------------------------------------*
    *&  包含                ZSDF011_CLASS
    *&---------------------------------------------------------------------*
    CLASS lcl_handle_cell_events DEFINITION DEFERRED.
    *---------------------------------------------------------------------*
    *       CLASS lcl_handle_events DEFINITION
    *---------------------------------------------------------------------*
    * define a local class for handling events of cl_salv_table
    *---------------------------------------------------------------------*
    CLASS lcl_handle_cell_events DEFINITION.
      PUBLIC SECTION.
        METHODS:
          on_double_cell_click FOR EVENT double_click OF cl_gui_alv_grid
            IMPORTING e_row e_column es_row_no.
        METHODS:mt_toolbar_add FOR EVENT toolbar OF cl_gui_alv_grid
            IMPORTING e_object.
        METHODS:mt_func_event FOR EVENT user_command OF cl_gui_alv_grid
            IMPORTING e_ucomm.
    ENDCLASS.
    *---------------------------------------------------------------------*
    *       CLASS lcl_handle_events IMPLEMENTATION
    *---------------------------------------------------------------------*
    * implement the events for handling the events of cl_salv_table
    *---------------------------------------------------------------------*
    CLASS lcl_handle_cell_events IMPLEMENTATION.
      METHOD on_double_cell_click.
        DATA ls_yck TYPE zsds087.
        CLEAR:gv_kunag,gv_kunrg,ls_yck,gv_mark.
        READ TABLE gt_alv_yck INTO ls_yck INDEX es_row_no-row_id. "判断行号
        gv_kunag = ls_yck-kunag.
        gv_kunrg = ls_yck-kunrg.
        PERFORM frm_get_czjldata USING gv_kunag gv_kunrg.
        PERFORM frm_get_djqddata USING gv_kunag gv_kunrg.
        PERFORM frm_diaplay_alv_refresh.
      ENDMETHOD.
      METHOD mt_toolbar_add.
        DATA ls_button TYPE stb_button.
        IF gv_mark = 'X'.
          CLEAR ls_button.
          ls_button-butn_type = '3'.
          APPEND ls_button TO e_object->mt_toolbar.
          CLEAR ls_button.
          ls_button-function = 'CHANGE'.
          ls_button-icon     = '@BB@'.
          ls_button-text     = '切换到客户记录'.
          APPEND ls_button TO e_object->mt_toolbar.
        ELSE.
          CLEAR ls_button.
          ls_button-butn_type = '3'.
          APPEND ls_button TO e_object->mt_toolbar.
          CLEAR ls_button.
          ls_button-function = 'ECHANGE'.
          ls_button-icon     = '@BB@'.
          ls_button-text     = '切换到全部记录'.
          APPEND ls_button TO e_object->mt_toolbar.
        ENDIF.
      ENDMETHOD.
      METHOD mt_func_event.
        DATA:lv_kunag TYPE kna1-kunnr,
             lv_kunrg TYPE kna1-kunnr.
        CASE e_ucomm.
          WHEN 'CHANGE'.
            CLEAR:lv_kunag,lv_kunrg,gt_alv_czjl[],gt_alv_djqd,gv_mark.
            lv_kunag = gv_kunag.
            lv_kunrg = gv_kunrg.
            PERFORM frm_get_czjldata USING lv_kunag lv_kunrg.
            PERFORM frm_get_djqddata USING lv_kunag lv_kunrg.
            PERFORM frm_diaplay_alv_refresh.
          WHEN 'ECHANGE'.
            CLEAR:lv_kunag,lv_kunrg,gt_alv_czjl[],gt_alv_djqd[],gv_mark.
            gv_mark = 'X'.
            lv_kunag = gv_kunag.
            PERFORM frm_get_czjldata USING lv_kunag lv_kunrg.
            PERFORM frm_get_djqddata USING lv_kunag lv_kunrg.
            PERFORM frm_diaplay_alv_refresh.
          WHEN OTHERS.
        ENDCASE.
      ENDMETHOD.
    ENDCLASS.
    
    DATA gr_events TYPE REF TO lcl_handle_cell_events."定义事件
  2. 定义类和方法使用的全局变量:
    DATA: gt_container TYPE REF TO cl_gui_custom_container,
          gt_container1 TYPE REF TO cl_gui_container,
          gt_container2 TYPE REF TO cl_gui_container,
          gt_container3 TYPE REF TO cl_gui_container.
    DATA: gt_alv_grid1 TYPE REF TO cl_gui_alv_grid,
          gt_alv_grid2 TYPE REF TO cl_gui_alv_grid,
          gt_alv_grid3 TYPE REF TO cl_gui_alv_grid.
    
    DATA:gv_kunag      TYPE kna1-kunnr,     "售达方
         gv_kunrg      TYPE kna1-kunnr,     "付款方
         gv_mark(1)    TYPE c.
  3. 将定义的类方法分配给类使用:
      CLEAR gwa_layout_lvc-grid_title.
      gwa_layout_lvc-grid_title = '文本描述' .
    
      CREATE OBJECT gr_events.
      SET HANDLER gr_events->mt_toolbar_add  FOR gt_alv_grid2.
      SET HANDLER gr_events->mt_func_event  FOR gt_alv_grid2.
    
      CALL METHOD gt_alv_grid2->set_table_for_first_display
        EXPORTING
          is_layout            = gwa_layout_lvc
          it_toolbar_excluding = gt_exclude
          i_save               = 'A'
        CHANGING
          it_outtab            = gt_alv_czjl
          it_fieldcatalog      = gt_fieldcat_czjllvc.
  4. 这样自定义按钮及其功能就实现了

你可能感兴趣的:(ALV报表)