Submit标准程序->动态生成ALV

Posted 想发财的小夏

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Submit标准程序->动态生成ALV相关的知识,希望对你有一定的参考价值。

Submit标准程序VA05,动态生成程序

REPORT ZDEMO.

TABLES:vbak,vbap,vbep.

" 红绿灯
CONSTANTS:gc_red     LIKE icon-internal VALUE '@0A@',
          gc_yellow  LIKE icon-internal VALUE '@09@',
          gc_green   LIKE icon-internal VALUE '@08@',
          gc_status  TYPE slis_formname VALUE 'FRM_STATUS',
          gc_command TYPE slis_formname VALUE 'FRM_COMMAND'.

DATA:gv_title    TYPE text100,           " 标题 Title
     gs_lay      TYPE lvc_s_layo,        " Layout
     gt_fieldcat TYPE lvc_t_fcat,        " Fieldcat
     gs_fieldcat TYPE lvc_s_fcat.

DATA: gs_data TYPE REF TO data.

FIELD-SYMBOLS: <gt_data> TYPE table.

SELECTION-SCREEN BEGIN OF BLOCK kopf WITH FRAME TITLE TEXT-001.
  SELECT-OPTIONS:  svbeln FOR vbak-vbeln,
                   sauart FOR vbak-auart,
                   skunnr FOR vbak-kunnr,
                   saudat FOR vbak-audat,
                   smatnr FOR vbap-matnr.
  PARAMETERS:      pbstkd TYPE vbkd-bstkd.
  SELECT-OPTIONS:  serdat FOR vbak-erdat,
                   sernam FOR vbak-ernam,
                   secust FOR vbak-kunnr,
                   sedatu FOR vbep-edatu.
SELECTION-SCREEN END OF BLOCK kopf.

SELECTION-SCREEN BEGIN OF BLOCK orgdaten WITH FRAME TITLE TEXT-002.
  SELECT-OPTIONS: svkorg FOR  vbak-vkorg,
                  svtweg FOR  vbak-vtweg,
                  sspart FOR  vbak-spart,
                  svkbur FOR  vbak-vkbur,
                  svkgrp FOR  vbak-vkgrp.
SELECTION-SCREEN END OF BLOCK orgdaten.

cl_salv_bs_runtime_info=>set(
       display  = ''
       metadata = ''
       data     = 'X' ).

SUBMIT sd_sales_document_view
     WITH svbeln IN svbeln "选择屏幕参数
     WITH sauart IN sauart
     WITH skunnr IN skunnr
     WITH saudat IN saudat
     WITH smatnr IN smatnr
     WITH pbstkd =  pbstkd
     WITH serdat IN serdat
     WITH sernam IN sernam
     WITH svkorg IN svkorg
     WITH svtweg IN svtweg
     WITH sspart IN sspart
     WITH svkbur IN svkbur
     WITH svkgrp IN svkgrp AND RETURN.

TRY.
    cl_salv_bs_runtime_info=>get_data_ref( IMPORTING r_data = gs_data r_data_descr = DATA(lc_descr) ).
    ASSIGN gs_data->* TO <gt_data>.
*  CATCH cx_salv_bs_sc_runtime_info.

ENDTRY.

cl_salv_bs_runtime_info=>clear_all( ).

IF gs_data IS BOUND.

  DATA: dref_str    TYPE REF TO data,
        dref_tab    TYPE REF TO data,
        dref_i      TYPE REF TO data,
        itab_type   TYPE REF TO cl_abap_tabledescr,
        struct_type TYPE REF TO cl_abap_structdescr,
        elem_type   TYPE REF TO cl_abap_elemdescr,
        table_type  TYPE REF TO cl_abap_tabledescr,
        gt_comp_tab TYPE cl_abap_structdescr=>component_table WITH HEADER LINE.

  DATA: dyn_wa    TYPE REF TO data,
        dyn_table TYPE REF TO data.

  FIELD-SYMBOLS: <lt_table> TYPE STANDARD TABLE,
                 <ls_wa>    TYPE any.

  table_type ?= cl_abap_tabledescr=>describe_by_data( <gt_data> ).
  struct_type ?= table_type->get_table_line_type( ).
  gt_comp_tab[] = struct_type->get_components( ).

  " Set Table Struct
  PERFORM frm_set_table.
  " Compose Struct
  CALL METHOD cl_abap_structdescr=>create
    EXPORTING
      p_components = gt_comp_tab[]
    RECEIVING
      p_result     = DATA(lr_struc).

  CALL METHOD cl_abap_tabledescr=>create
    EXPORTING
      p_line_type = lr_struc
    RECEIVING
      p_result    = DATA(lr_table).

  CREATE DATA dyn_wa    TYPE HANDLE lr_struc.
  CREATE DATA dyn_table TYPE HANDLE lr_table.
  ASSIGN dyn_wa->* TO <ls_wa>.
  ASSIGN dyn_table->* TO <lt_table>.
  " Get Data
  PERFORM frm_get_data.
  " Create Fieldcat
  TRY.
      cl_salv_table=>factory( IMPORTING
                                r_salv_table   = DATA(salv_table)
                              CHANGING
                                t_table        = <lt_table>  ).

      DATA(lt_fieldcat) = cl_salv_controller_metadata=>get_lvc_fieldcatalog(
           r_columns      = salv_table->get_columns( )
           r_aggregations = salv_table->get_aggregations( ) )." ALV Aggregations
    CATCH cx_root.
  ENDTRY.
  " Set Fieldcat
  PERFORM frm_set_fieldcat.
  " Layout
  PERFORM frm_layout_set.
  " Display ALV
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       = sy-repid
      i_callback_pf_status_set = gc_status
      i_callback_user_command  = gc_command
      is_layout_lvc            = gs_lay
      it_fieldcat_lvc          = lt_fieldcat
      i_save                   = 'A'
    TABLES
      t_outtab                 = <lt_table>
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.

  IF sy-subrc <> 0.
* IMPLEMENT SUITABLE ERROR HANDLING HERE
  ENDIF.

ENDIF.
*&---------------------------------------------------------------------*
*& Form frm_layout_set
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_layout_set .

  gs_lay-zebra = 'X'.
  gs_lay-box_fname = 'SEL'.
  gs_lay-cwidth_opt = 'X'.

  gv_title = | 销售订单清单: lines( <lt_table> ) 条目 |.

ENDFORM.
FORM frm_status USING p_extab TYPE slis_t_extab..
  DATA lt_fcode TYPE TABLE OF sy-ucomm.

*  APPEND 'CRE' TO lt_fcode.   "隐藏功能按钮

  SET PF-STATUS 'ST1' EXCLUDING lt_fcode.
  SET TITLEBAR  'TI1' WITH gv_title.

ENDFORM.
FORM frm_command USING p_ucomm TYPE sy-ucomm
                       ps_selfield  TYPE slis_selfield.
  DATA: lo_grid TYPE REF TO cl_gui_alv_grid,
        lv_code LIKE sy-ucomm.

  lv_code = p_ucomm.
  CLEAR:p_ucomm.

  ps_selfield-refresh = 'X'.
*刷新alv屏幕数据
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lo_grid.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_set_table
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_set_table .

  PERFORM frm_set_comptab USING 'ZSSD011-SEL'            'SEL'.
  PERFORM frm_set_comptab USING 'VBAK-KVGR1'             'KVGR1'.
  PERFORM frm_set_comptab USING 'KNA1-SORTL'             'SORTL'.
  PERFORM frm_set_comptab USING 'VBAP-ZZECUST'           'ZZECUST'.
  PERFORM frm_set_comptab USING 'VBAP-ZZECNAME'          'ZZECNAME'.
  PERFORM frm_set_comptab USING 'VBAP-ZZPWIRE'           'ZZPWIRE'.
  PERFORM frm_set_comptab USING 'VBAP-ZZRMAID'           'ZZRMAID'.
  PERFORM frm_set_comptab USING 'VBAP-ZZOACPJY'          'ZZOACPJY'.
  PERFORM frm_set_comptab USING 'VBAP-ZZOADDXG'          'ZZOADDXG'.
  PERFORM frm_set_comptab USING 'VBAP-ZZSMPID'           'ZZSMPID'.
  PERFORM frm_set_comptab USING 'VBAP-ZZQSPID'           'ZZQSPID'.
  PERFORM frm_set_comptab USING 'VBAP-ZZNCNRID'          'ZZNCNRID'.
  PERFORM frm_set_comptab USING 'VBAP-ZZRESCID'          'ZZRESCID'.
  PERFORM frm_set_comptab USING 'VBAP-ZZPOSMARK'         'ZZPOSMARK'.
  PERFORM frm_set_comptab USING 'VBEP-EDATU'             'EDATU1'.
  PERFORM frm_set_comptab USING 'VBEP-ZZFCD'             'ZZFCD'.
  PERFORM frm_set_comptab USING 'VBEP-DLVQTY_BU'         'DLVQTY_BU'.
  PERFORM frm_set_comptab USING 'LIPS-LFIMG'             'LFIMG1'.
  PERFORM frm_set_comptab USING 'LIPS-LFIMG'             'LFIMG2'.
  PERFORM frm_set_comptab USING 'VBRP-FKIMG'             'FKIMG'.
  PERFORM frm_set_comptab USING 'VBAP-VGPOS'             'VGPOS'.
  PERFORM frm_set_comptab USING 'VBAP-VGBEL'             'VGBEL'.
  PERFORM frm_set_comptab USING 'VBAP-ZUEPOS'            'ZUEPOS'.
  PERFORM frm_set_comptab USING 'ZSSD011-REMARK'         'REMARK'.
  PERFORM frm_set_comptab USING 'PRCD_ELEMENTS-KBETR'    'KBETR_ZPV1'.
  PERFORM frm_set_comptab USING 'PRCD_ELEMENTS-KBETR'    'KBETR_ZRV0'.
  PERFORM frm_set_comptab USING 'PRCD_ELEMENTS-KBETR'    'KBETR_ZRV1'.
  PERFORM frm_set_comptab USING 'PRCD_ELEMENTS-KBETR'    'KBETR_RA00'.
  PERFORM frm_set_comptab USING 'PRCD_ELEMENTS-KBETR'    'KBETR_RB00'.
  PERFORM frm_set_comptab USING 'PRCD_ELEMENTS-KBETR'    'KBETR_ZRA0'.
  PERFORM frm_set_comptab USING 'PRCD_ELEMENTS-KBETR'    'KBETR_MWST'.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_set_comptab
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> P_
*&      --> P_
*&---------------------------------------------------------------------*
FORM frm_set_comptab  USING    lv_txt1 lv_txt2.

  DATA: lv_tabname(30),
        ls_comp_tab LIKE LINE OF gt_comp_tab.

  lv_tabname = lv_txt1.

  cl_abap_datadescr=>describe_by_name(
     EXPORTING p_name         = lv_tabname
     RECEIVING p_descr_ref    = DATA(lr_type) ).

  ls_comp_tab-type ?= lr_type.
  ls_comp_tab-name = lv_txt2.
  APPEND ls_comp_tab TO gt_comp_tab.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_get_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_get_data .

  DATA:r_vbeln TYPE rseloption,
       r_posnr TYPE rseloption.

  LOOP AT <gt_data> ASSIGNING FIELD-SYMBOL(<gs_data>).

    ASSIGN COMPONENT 'VBELN' OF STRUCTURE <gs_data> TO FIELD-SYMBOL(<fs_vbeln>).
    ASSIGN COMPONENT 'POSNR' OF STRUCTURE <gs_data> TO FIELD-SYMBOL(<fs_posnr>).

    r_vbeln = VALUE #( BASE r_vbeln ( sign = 'I' option = 'EQ' low = <fs_vbeln>  ) ).
    r_posnr = VALUE #( BASE r_posnr ( sign = 'I' option = 'EQ' low = <fs_posnr>  ) ).

  ENDLOOP.

  SELECT a~vbeln,
         a~posnr,
         a~zzecust,
         a~zzecname,
         a~zzpwire,
         a~zzrmaid,
         a~zzoacpjy,
         a~zzoaddxg,
         a~zzsmpid,
         a~zzqspid,
         a~zzncnrid,
         a~zzrescid,
         a~zzposmark,
         a~vgbel,
         a~vgpos,
         a~zuepos,
         b~knumv,
         c~edatu AS edatu1,
         c~zzfcd,
         c~dlvqty_bu,
         d~sortl
    FROM vbap AS a
    LEFT OUTER JOIN vbak AS b ON a~vbeln = b~vbeln
    LEFT OUTER JOIN vbep AS c ON a~vbeln = c~vbeln AND a~posnr = c~posnr AND c~wmeng > 0
    LEFT OUTER JOIN kna1 AS d ON b~kunnr = d~kunnr
    INTO TABLE @DATA(lt_vbap)
   WHERE a~vbeln IN @r_vbeln
     AND a~posnr IN @r_posnr.
  SORT lt_vbap BY vbeln posnr.

  IF lt_vbap IS NOT INITIAL.

    SELECT *
      FROM prcd_elements
      INTO TABLE @DATA(lt_elements)
       FOR ALL ENTRIES IN @lt_vbap
     WHERE knumv = @lt_vbap-knumv
       AND kposn = @lt_vbap-posnr.
    SORT lt_elements BY knumv kposn kschl.

  ENDIF.

  SELECT vbeln,posnr,vgbel,vgpos,lfimg,pdsta
    FROM lips
    INTO TABLE @DATA(lt_lips)
   WHERE vgbel IN @r_vbeln
     AND vgpos IN @r_posnr.
  SORT lt_lips BY vgbel vgpos.

  SELECT vbeln,posnr,aubel,aupos,fkimg
    FROM vbrp
    INTO TABLE @DATA(lt_vbrp)
   WHERE aubel IN @r_vbeln
     AND aupos IN @r_posnr.
  SORT lt_vbrp BY aubel aupos.

  DATA:lv_sum  TYPE menge_d,
       lv_sum1 TYPE menge_d.

  LOOP AT <gt_data> ASSIGNING <gs_data>.

    APPEND INITIAL LINE TO <lt_table> ASSIGNING FIELD-SYMBOL(<fs_data>).
    MOVE-CORRESPONDING <gs_data> TO <fs_data>.
    ASSIGN COMPONENT 'VBELN' OF STRUCTURE <gs_data> TO <fs_vbeln>.
    ASSIGN COMPONENT 'POSNR' OF STRUCTURE <gs_data> TO <fs_posnr>.

    READ TABLE lt_vbap INTO DATA(ls_vbap) WITH KEY vbeln = <fs_vbeln> posnr = <fs_posnr> BINARY SEARCH.
    MOVE-CORRESPONDING ls_vbap TO <fs_data>.

    CLEAR:lv_sum,lv_sum1.

    READ TABLE lt_lips INTO DATA(ls_lips) WITH KEY vgbel = <fs_vbeln> vgpos = <fs_posnr> BINARY SEARCH.
    IF sy-subrc EQ 0.
      DATA(lv_tabix) = sy-tabix.
      LOOP AT lt_lips INTO ls_lips FROM lv_tabix WHERE vgbel = <fs_vbeln> AND vgpos = <fs_posnr>.
        lv_sum = lv_sum + ls_lips-lfimg.
        IF ls_lips-pdsta = 'C'.
          lv_sum1 = lv_sum1 + ls_lips-lfimg.
        ENDIF.
      ENDLOOP.

      ASSIGN COMPONENT 'LFIMG1' OF STRUCTURE <fs_data> TO FIELD-SYMBOL(<fs_lfimg1>).
      <fs_lfimg1> = lv_sum.

      ASSIGN COMPONENT 'LFIMG2' OF STRUCTURE <fs_data> TO FIELD-SYMBOL(<fs_lfimg2>).
      <fs_lfimg2> = lv_sum1.

    ENDIF.

    CLEAR:lv_sum.
    READ TABLE lt_vbrp INTO DATA(ls_vbrp) WITH KEY aubel = <fs_vbeln> aupos = <fs_posnr> BINARY SEARCH.
    IF sy-subrc EQ 0.
      lv_tabix = sy-tabix.
      LOOP AT lt_vbrp INTO ls_vbrp FROM lv_tabix WHERE aubel = <fs_vbeln> AND aupos = <fs_posnr>.
        lv_sum = lv_sum + ls_vbrp-fkimg.
      ENDLOOP.

      ASSIGN COMPONENT 'FKIMG' OF STRUCTURE <fs_data> TO FIELD-SYMBOL(<fs_fkimg>).
      <fs_fkimg> = lv_sum.

    ENDIF.

    ASSIGN COMPONENT 'KBETR_ZPV1' OF STRUCTURE <fs_data> TO FIELD-SYMBOL(<fs_zpv1>).
    ASSIGN COMPONENT 'KBETR_ZRV0' OF STRUCTURE <fs_data> TO FIELD-SYMBOL(<fs_zrv0>).
    ASSIGN COMPONENT 'KBETR_ZRV1' OF STRUCTURE <fs_data> TO FIELD-SYMBOL(<fs_zrv1>).
    ASSIGN COMPONENT 'KBETR_RA00' OF STRUCTURE <fs_data> TO FIELD-SYMBOL(<fs_ra00>).
    ASSIGN COMPONENT 'KBETR_RB00' OF STRUCTURE <fs_data> TO FIELD-SYMBOL(<fs_rb00>).
    ASSIGN COMPONENT 'KBETR_ZRA0' OF STRUCTURE <fs_data> TO FIELD-SYMBOL(<fs_zra0>).
    ASSIGN COMPONENT 'KBETR_MWST' OF STRUCTURE <fs_data> TO FIELD-SYMBOL(<fs_mwst>).

    READ TABLE lt_elements INTO DATA(ls_elements) WITH KEY knumv = ls_vbap-knumv kposn = <fs_posnr> kschl = 'ZPV1' BINARY SEARCH.
    IF sy-subrc EQ 0.
      <fs_zpv1> = ls_elements-kbetr.
    ELSE.
      READ TABLE lt_elements INTO ls_elements WITH KEY knumv = ls_vbap-knumv kposn = <fs_posnr> kschl = 'ZPV0' BINARY SEARCH.
      IF sy-subrc EQ 0.
        <fs_zpv1> = ls_elements-kbetr.
      ENDIF.
    ENDIF.

    READ TABLE lt_elements INTO ls_elements WITH KEY knumv = ls_vbap-knumv kposn = <fs_posnr> kschl = 'ZRV1' BINARY SEARCH.
    IF sy-subrc EQ 0.
      <fs_zrv1> = ls_elements-kbetr.
    ENDIF.

    READ TABLE lt_elements INTO ls_elements WITH KEY knumv = ls_vbap-knumv kposn = <fs_posnr> kschl = 'RA00' BINARY SEARCH.
    IF sy-subrc EQ 0.
      <fs_ra00> = ls_elements-kbetr.
    ENDIF.

    READ TABLE lt_elements INTO ls_elements WITH KEY knumv = ls_vbap-knumv kposn = <fs_posnr> kschl = 'RB00' BINARY SEARCH.
    IF sy-subrc EQ 0.
      <fs_rb00> = ls_elements-kbetr.
    ENDIF.

    READ TABLE lt_elements INTO ls_elements WITH KEY knumv = ls_vbap-knumv kposn = <fs_posnr> kschl = 'ZRA0' BINARY SEARCH.
    IF sy-subrc EQ 0.
      <fs_zra0> = ls_elements-kbetr.
    ENDIF.

    READ TABLE lt_elements INTO ls_elements WITH KEY knumv = ls_vbap-knumv kposn = <fs_posnr> kschl = 'ZRV0' BINARY SEARCH.
    IF sy-subrc EQ 0.
      <fs_zrv0> = ls_elements-kbetr.
    ENDIF.

    READ TABLE lt_elements INTO ls_elements WITH KEY knumv = ls_vbap-knumv kposn = <fs_posnr> kschl = 'MWST' BINARY SEARCH.
    IF sy-subrc EQ 0.
      <fs_mwst> = ls_elements-kbetr.
    ENDIF.

    CLEAR ls_vbap.

  ENDLOOP.

  DELETE <lt_table> WHERE ('ZZECUST NOT IN SECUST[]').
  DELETE <lt_table> WHERE ('EDATU1 NOT IN SEDATU[]').

ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_set_fieldcat
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM frm_set_fieldcat .

  LOOP AT lt_fieldcat ASSIGNING FIELD-SYMBOL(<fs_fieldcat>).

    PERFORM frm_set_fieldcat_value USING 'KBETR_ZPV1' '销售单价'       '==QNTY1' '' '' '' '' CHANGING <fs_fieldcat>.
    PERFORM frm_set_fieldcat_value USING 'KBETR_ZRV0' 'Rebate固定金额' '==QNTY1' '' '' '' '' CHANGING <fs_fieldcat>.
    PERFORM frm_set_fieldcat_value USING 'KBETR_ZRV1' 'Rebate百分比'   '==QNTY1' '' '' '' '' CHANGING <fs_fieldcat>.
    PERFORM frm_set_fieldcat_value USING 'KBETR_RA00' '%净价折扣'      '==QNTY1' '' '' '' '' CHANGING <fs_fieldcat>.
    PERFORM frm_set_fieldcat_value USING 'KBETR_RB00' '折扣(值)'     '==QNTY1' '' '' '' '' CHANGING <fs_fieldcat>.
    PERFORM frm_set_fieldcat_value USING 'KBETR_ZRA0' '%特殊价格折扣'  '==QNTY1' '' '' '' '' CHANGING <fs_fieldcat>.
    PERFORM frm_set_fieldcat_value USING 'KBETR_MWST' '销项税'         '==QNTY1' '' '' '' '' CHANGING <fs_fieldcat>.

  ENDLOOP.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_set_fieldcat_value
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> P_
*&      --> P_
*&      --> P_
*&      --> P_
*&      --> P_
*&      --> P_
*&      --> P_
*&      <-- <FS_FIELDCAT>
*&---------------------------------------------------------------------*
FORM frm_set_fieldcat_value  USING    lv_fieldname
                                      lv_scrtext
                                      lv_edit_mask
                                      lv_ref_table
                                      lv_ref_field
                                      lv_outputlen
                                      lv_value01
                             CHANGING ps_fieldcat LIKE lvc_s_fcat.

  CHECK ps_fieldcat-fieldname = lv_fieldname.

  ps_fieldcat-fieldname = lv_fieldname.
  ps_fieldcat-scrtext_l = lv_scrtext.
  ps_fieldcat-scrtext_m = lv_scrtext.
  ps_fieldcat-scrtext_s = lv_scrtext.
  ps_fieldcat-coltext   = lv_scrtext.
  ps_fieldcat-edit_mask = lv_edit_mask.
  ps_fieldcat-ref_table = lv_ref_table.
  ps_fieldcat-ref_field = lv_ref_field.
  ps_fieldcat-col_opt   = lv_outputlen.

ENDFORM.

以上是关于Submit标准程序->动态生成ALV的主要内容,如果未能解决你的问题,请参考以下文章

SUBMIT标准程序取ALV数据

WDA中动态ALV的实现

2020.01.11 ABAP随笔获取标准报表数据(MB52)数据进行客制ALV

动态内表用ALV输出时能不能再定义每个FIELD的颜色呢?

为什麽要用ABAP动态内表

ABAP ALV 动态显示列