当前位置:网站首页>ABAP ALV LVC模板

ABAP ALV LVC模板

2022-07-07 22:22:00 Seele_1018

模板包含选择屏幕、ALV、菜单栏事件、打印程序内容

********************************************************************
* 事务代码:                                                       *
* 程序名称:ZDEMO_SEELE01                                                *
* 使用变式:                                                       *
* 设 计 人:SEELE                                                  *
* 设计时间:                                             *
* 程序类型: REPORT                                                 *
* 应用类型: MM                                              *
* 描    述:                                   *
*(修改日志)--------------------------------------------------------*
*                                                                  *
* 日志号   修改人  修改时间       修改说明                         *
*  ----    ------   ------       -----------                       *
* 001      SEELE                  创建报表                         *
********************************************************************
REPORT zdemo_seele01.

*------------------------------------------------------------------------*
*               声明选择屏幕、图标等用到的表和类型池
*------------------------------------------------------------------------*
TYPE-POOLS: slis.
TABLES: ekko,sscrfields.

*-----------------------------------------------------------------------*
*                     声明自定义的数据类型
*-----------------------------------------------------------------------*
TYPES: BEGIN OF ty_alv,
         ebeln TYPE   ekko~ebeln,
         bukrs TYPE   ekko~bukrs,
         butxt TYPE   t001~butxt,
         bstyp TYPE   ekko~bstyp,

         sel   TYPE   c,
       END OF ty_alv.

*------------------------------------------------------------------------*
*               声明ALV显示过程中用到的数据对象
*------------------------------------------------------------------------*
DATA: gt_fieldcat TYPE lvc_t_fcat,
      gs_fieldcat TYPE lvc_s_fcat,
      gs_layout   TYPE lvc_s_layo.

*ALV回调函数
CONSTANTS: gc_callback_satus        TYPE slis_formname  VALUE 'FRM_USER_STATUS',  " 设置自定义菜单栏常量
           gc_callback_user_command TYPE slis_formname  VALUE 'FRM_USER_COMMAND'.  " 设置自定义菜单按钮事件

* ALV报表
DATA: gs_alv TYPE ty_alv,
      gt_alv TYPE TABLE OF ty_alv.

* Smartforms
DATA: tmp_control_parameters TYPE ssfctrlop,
      fm_name                TYPE rs38l_fnam.


*-----------------------------------------------------------------------*
*                      INITIALIZATION
*-----------------------------------------------------------------------*
INITIALIZATION.
* 选择屏幕
  SELECTION-SCREEN BEGIN OF BLOCK blk WITH FRAME TITLE TEXT-001.
  SELECT-OPTIONS s_matnr FOR zqmt_qylist-matnr.
  SELECTION-SCREEN END OF BLOCK blk.

* ALV格式初始化
  PERFORM layout_init.
  PERFORM fieldcat_init_all.

*-----------------------------------------------------------------------*
*                    START-OF-SELECTION
*-----------------------------------------------------------------------*
START-OF-SELECTION.

*获取数据
  PERFORM frm_get_data.

*-----------------------------------------------------------------------*
*                     END-OF-SELECTION
*-----------------------------------------------------------------------*
END-OF-SELECTION.

* 展示ALV
  PERFORM frm_display_alv.



*-----------------------------------------------------------------------*
*                     FORM子例程区
*-----------------------------------------------------------------------*
* 获取数据
FORM frm_get_data.


ENDFORM.

* 设置layout
FORM frm_set_layout.  "SET LAYOUT
  gs_layout-cwidth_opt = 'X'.  "设置为最适合的布局
  gs_layout-zebra = 'X'.  "斑马线
  gs_layout-sel_mode  = 'A'.  "选择模式
  gs_layout-box_fname  = 'SEL'.  "选择字段
ENDFORM.

* 设置field
FORM frm_field.  "USE FIELD

  REFRESH: gt_fieldcat.
  CLEAR: gs_fieldcat.

  PERFORM frm_set_field USING 0 'XUHAO' TEXT-a01 '' 'X' '' 'C' '' '' '' '' '' ''.
  PERFORM frm_set_field USING 1 'EQUNR' TEXT-a02 '' 'X' '' 'C' '' '' '' 'EQUNR' 'EQUI' ''.
  PERFORM frm_set_field USING 2 'EQKTX' TEXT-a03 '' '' '' 'C' '' '' '' 'EQKTX' 'EQKT' ''.
  PERFORM frm_set_field USING 3 'EQTYP' TEXT-a04 '' '' '' 'C' '' '' '' 'EQTYP' 'EQUI' ''.
  PERFORM frm_set_field USING 4 'SWERK' TEXT-a06 '' '' '' 'C' '' '' '' 'SWERK' 'ILOA' ''.
  PERFORM frm_set_field USING 5 'TYPBZ' TEXT-a07 '' '' '' 'C' '' '' '' 'TYPBZ' 'EQUI' ''.
  PERFORM frm_set_field USING 6 'TXT30' TEXT-a08 '' '' '' 'C' '' '' '' 'TXT30' 'TJ30T' ''.

ENDFORM.

FORM frm_set_field USING i_col_pos TYPE any  "SET FIELD
                         i_fieldname TYPE any
                         i_coltext TYPE any
                         i_qfieldname TYPE any
                         i_key TYPE any
                         i_checkbox TYPE any
                         i_just TYPE any
                         i_emphasize TYPE any
                         i_datatype TYPE any
                         i_inttype TYPE any
                         i_ref_field TYPE any
                         i_ref_table TYPE any
                         i_edit TYPE any.

  gs_fieldcat-col_pos    = i_col_pos.
  gs_fieldcat-fieldname  = i_fieldname.
  gs_fieldcat-coltext    = i_coltext.
  gs_fieldcat-qfieldname = i_qfieldname.
  gs_fieldcat-key        = i_key.
  gs_fieldcat-checkbox   = i_checkbox.
  gs_fieldcat-just       = i_just.
  gs_fieldcat-emphasize  = i_emphasize.
  gs_fieldcat-datatype   = i_datatype.
  gs_fieldcat-inttype    = i_inttype.
  gs_fieldcat-ref_field  = i_ref_field.
  gs_fieldcat-ref_table  = i_ref_table.
  gs_fieldcat-edit       = i_edit.
  APPEND gs_fieldcat TO gt_fieldcat.
  CLEAR gs_fieldcat.

ENDFORM.

FORM frm_display_alv.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       = sy-repid
      i_callback_pf_status_set = 'FRM_USER_STATUS'
      i_callback_user_command  = 'FRM_USER_COMMAND'
*     it_events                = gv_events
      is_layout_lvc            = gs_layout
      it_fieldcat_lvc          = gt_fieldcat
      i_save                   = 'A'
    TABLES
      t_outtab                 = gt_data
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.
  IF sy-subrc NE 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.

FORM frm_user_status USING pt_extab TYPE slis_t_extab.  "GUI
  SET PF-STATUS 'STATUS_1000' EXCLUDING pt_extab.
ENDFORM.

FORM frm_user_command USING i_ucomm LIKE sy-ucomm  "user_command
                            i_selfield TYPE slis_selfield.

  DATA: lo_grid TYPE REF TO cl_gui_alv_grid.

  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lo_grid.

  CALL METHOD lo_grid->check_changed_data. "将更新后的数据传到alv所对应的内表

  i_selfield-refresh = 'X'. "刷新数据

  CASE i_ucomm.
    WHEN 'PRINT'.
      READ TABLE gt_data INTO gs_data WITH KEY sel = 'X'.  "CHECK SEL
      IF sy-subrc NE 0.
        MESSAGE s003(zpm01) DISPLAY LIKE 'E'.
        RETURN.
      ELSE.
        CLEAR:gs_tab,gt_tab.
        LOOP AT gt_data INTO gs_data WHERE sel = 'X'.
          MOVE-CORRESPONDING gs_data TO gs_tab.
          APPEND gs_tab TO gt_tab.
        ENDLOOP.

        PERFORM frm_print.

      ENDIF.
  ENDCASE.

ENDFORM.

FORM frm_print.
  DATA: ls_control_parameters TYPE ssfctrlop,
        ls_output_options     TYPE ssfcompop,
        ls_outopt             TYPE ssfcresop,
        ls_output_info        TYPE ssfcrescl.

  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
    EXPORTING
      formname           = 'ZPMF_WORKORDER01'
    IMPORTING
      fm_name            = lv_name01
    EXCEPTIONS
      no_form            = 1
      no_function_module = 2
      OTHERS             = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  CALL FUNCTION 'SSF_OPEN'
    EXPORTING
      control_parameters = ls_control_parameters
      output_options     = ls_output_options
    IMPORTING
      job_output_options = ls_outopt
    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.
    RETURN.
  ENDIF.

  ls_control_parameters-no_open = 'X'.
  ls_control_parameters-no_close = 'X'.

  LOOP AT gt_sel INTO gw_alv.

    CALL FUNCTION ls_pm-sfname
      EXPORTING
        control_parameters = ls_control_parameters
        output_options     = ls_output_options
        gs_head            = ls_pm
      IMPORTING
        job_output_info    = ls_output_info
      TABLES
        gt_item            = lt_pmi
      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.

  ENDLOOP.

  CALL FUNCTION 'SSF_CLOSE'
    IMPORTING
      job_output_info  = ls_output_info
    EXCEPTIONS
      formatting_error = 1
      internal_error   = 2
      send_error       = 3
      OTHERS           = 4.
  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.
原网站

版权声明
本文为[Seele_1018]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_44826887/article/details/125605981