Skip to Content
-1
Aug 25, 2020 at 08:47 AM

optimize program

58 Views

My ALV program is ok. But I think I will optimize it . Perhaps the usage is obsolete or issue else .

TYPE-POOLS:slis.
" define an internal table
TYPES: BEGIN OF TY_ITEM,
  LGORT TYPE MSEG-LGORT,     " 库存地点
  BUDAT_MKPF TYPE MSEG-BUDAT_MKPF, " 入库日期
  MBLNR TYPE MSEG-MBLNR,     " 物料凭证号
  MATNR TYPE  MSEG-MATNR,     " 物料号
  MAKTX TYPE  MAKT-MAKTX,     " 物料描述
  MENGE TYPE MSEG-MENGE,     " 数量
  USNAM_MKPF TYPE  MSEG-USNAM_MKPF, "过账人
  LIFNR TYPE  MSEG-LIFNR,     " 供应商编码
  NAME1 TYPE  LFA1-NAME1, " 供应商
  LGOBE TYPE T001L-LGOBE, "库存地点编码
  BWART TYPE MSEG-BWART,     " 移动类型
END OF TY_ITEM.
DATA ITEM TYPE TABLE OF TY_ITEM.
DATA WA_ITEM TYPE TY_ITEM.
DATA: DUMMY_MSEG TYPE MSEG,
      DUMMY_MAKT TYPE MAKT,
      DUMMY_LFA1 TYPE LFA1,
      DUMMY_T001L TYPE T001L.


" define data type


  DATA: gd_repid TYPE sy-repid.
  DATA: i_grid_settings TYPE lvc_s_glay.
  DATA: afield TYPE slis_fieldcat_alv.
  DATA: fieldcat TYPE TABLE OF slis_fieldcat_alv .
  DATA: layout TYPE slis_layout_alv.
  DATA: l_pos TYPE i VALUE 1.
  DATA: lt_h TYPE TABLE OF item .
  DATA: lft_h TYPE TABLE OF item .
  DATA: lft_i TYPE TABLE OF item .
  DATA: p_i TYPE i,p_h TYPE i , l_msg(70),l_kzwi1 TYPE vbap-kzwi1,l_zzye TYPE bsid-dmbtr.
  " define macro
     DEFINE  fill.
    afield-col_pos   = l_pos.
    afield-fieldname = &1.
    afield-seltext_l = &2.
    afield-no_zero   = &3.
    afield-key       = &4.
    afield-edit      = &5.
     append afield to fieldcat.
    clear afield.
    l_pos = l_pos + 1.
  END-OF-DEFINITION.
" Call screen and define select-option
SELECTION-SCREEN BEGIN OF BLOCK BLA WITH FRAME TITLE  text-001.
  SELECT-OPTIONS:
                  S_MKPF FOR DUMMY_MSEG-BUDAT_MKPF, " 入库日期
                  S_LIFNR FOR DUMMY_LFA1-LIFNR, " 供应商
                  S_MATNR FOR DUMMY_MSEG-MATNR,     " 物料号
                  S_ORDER FOR DUMMY_T001L-LGOBE, " 入库单号
                  S_LGORT FOR DUMMY_T001L-LGORT. "库存地点编码
  SELECTION-SCREEN END OF BLOCK BLA.
   START-OF-SELECTION.
    PERFORM frm_get_data.
    IF item[] IS NOT INITIAL.
      PERFORM frm_show_data.
    ELSE.
      MESSAGE s001(00) WITH '没有查询到数据'.
    ENDIF.
" insert data to internal table
FORM frm_get_data.
   SELECT MSEG~LGORT MSEG~BUDAT_MKPF MSEG~MBLNR MSEG~MATNR   MSEG~MENGE  MSEG~USNAM_MKPF  MSEG~LIFNR   MSEG~BWART


    INTO CORRESPONDING FIELDS OF TABLE ITEM
    FROM MSEG
   WHERE MSEG~BUDAT_MKPF IN S_MKPF
    AND MSEG~LIFNR  IN  S_LIFNR
    AND MSEG~MATNR IN S_MATNR
    AND MSEG~LGORT IN S_LGORT .
LOOP AT ITEM  INTO WA_ITEM.
        SELECT SINGLE MAKTX INTO  WA_ITEM-MAKTX
        FROM MAKT WHERE MATNR = WA_ITEM-MATNR.


        SELECT SINGLE NAME1 INTO  WA_ITEM-NAME1
        FROM LFA1 WHERE LIFNR = WA_ITEM-LIFNR.


        SELECT SINGLE LGOBE INTO  WA_ITEM-LGOBE
        FROM T001L WHERE LGORT = WA_ITEM-LGORT.


      MODIFY ITEM FROM WA_ITEM.
     ENDLOOP.
ENDFORM.
" show data with alv
    FORM frm_show_data .
      i_grid_settings-edt_cll_cb = 'X'. "显示界面可编辑字段上修改了数据,回车后立即更新内表的数据
      layout-colwidth_optimize   = 'X'.   "设置字段列宽自适应
      layout-zebra               = 'X'.


      CLEAR afield.
      REFRESH fieldcat."clear,refresh,free
      fill      'MBLNR'  '物料凭证号' '' '' ''.
      fill      'MATNR'  '物料号' '' '' ''.
      fill      'MAKTX' '物料描述' '' '' ''.
      fill      'MENGE'  '数量' '' '' ''.
      fill      'LGORT'  '库存地点编码' '' '' ''.
      fill      'LGOBE'  '库存地点' '' '' ''.
      fill      'BUDAT_MKPF' '入库日期'  '' '' ''.
      fill      'LIFNR'  '供应商编码' '' '' ''.
      fill      'NAME1' '供应商' '' '' ''.
      fill      'BWART '  '移动类型'   '' '' ''.


     cl_salv_table=>factory(
        IMPORTING
          r_salv_table = DATA(salv)
        CHANGING
          t_table      = item ).
  salv->display( ).


    ENDFORM.                    " FRM_SHOW_DATA


    FORM set_status USING rt_extab TYPE slis_t_extab.
      SET PF-STATUS 'STANDARD'."工具栏的图标可以从SAPLKKBL程序的接口standard复制
    ENDFORM.


    FORM user_command USING rf_ucomm  LIKE sy-ucomm
                            rs        TYPE slis_selfield .
      rs-refresh = 'X'."屏幕事件触发后自动刷新屏幕


      CASE rf_ucomm.


        WHEN '&IC1'.
                READ TABLE item INDEX rs-tabindex TRANSPORTING NO FIELDS.




        WHEN  '&BACK' OR '&QUIT' OR '&CANCEL'.
          LEAVE PROGRAM.


      ENDCASE.


    ENDFORM.