ABAP生成動態內表

創建步驟:

  1. 先定义动态结构體,然後用系統標準的方法:cl_alv_table_create=>create_dynamic_table,生成動態內表
  2. 赋值。遍歷动态结构,获取指定的字段,然后给指定的字段赋值。
  3. 顯示。遍歷动态结构,获取指定的字段,然后读取对应字段的值。
TABLES: ekbe.
SELECT-OPTIONS: ebeln FOR ekbe-ebeln NO INTERVALS,
                matnr FOR ekbe-matnr NO INTERVALS,
                bwart FOR ekbe-bwart NO INTERVALS.
DATA tdate LIKE TABLE OF sy-datum WITH HEADER LINE.
DATA: BEGIN OF itab OCCURS 0,
        dmbtr like ekbe-dmbtr,
        waers like ekbe-waers,
        menge LIKE ekbe-menge,
        belnr like ekbe-belnr,
        budat LIKE ekbe-budat,
      END OF itab.
DATA: dy_table TYPE REF TO data,
      dy_wa TYPE REF TO data,
      it_strtab TYPE lvc_t_fcat,
      wa_str TYPE lvc_s_fcat.
data fieldname(20).
DATA: gt_fieldcat TYPE lvc_t_fcat,
      gs_fieldcat TYPE lvc_s_fcat.
DEFINE: add_field.
  gs_fieldcat-fieldname = &1.
  gs_fieldcat-coltext = &2.
  append gs_fieldcat to gt_fieldcat.
END-OF-DEFINITION.
SELECT dmbtr
       waers
       belnr
       menge
       budat FROM ekbe
       INTO CORRESPONDING FIELDS OF TABLE itab
       WHERE ebeln IN ebeln
       AND matnr IN matnr
       AND bwart IN bwart.
SORT itab BY budat.

LOOP AT itab.
  tdate = itab-budat.
  COLLECT tdate.
ENDLOOP.

FIELD-SYMBOLS: <dy_table> TYPE table,
               <dyn_wa> TYPE ANY,
               <fs> TYPE ANY.
wa_str-fieldname = 'BELNR'.
wa_str-col_pos = 1.
APPEND wa_str TO it_strtab.
PERFORM input_fieldcat USING 'BELNR' 'BELNR'.

CLEAR: wa_str.
wa_str-fieldname = 'DMBTR'.
wa_str-col_pos = 2.
APPEND wa_str TO it_strtab.
CLEAR: wa_str.
PERFORM input_fieldcat USING 'DMBTR' 'DMBTR'.

wa_str-fieldname = 'WAERS'.
wa_str-col_pos = 3.
APPEND wa_str TO it_strtab.
CLEAR: wa_str.
PERFORM input_fieldcat USING 'WAERS' 'WAERS'.

LOOP AT tdate.
  wa_str-fieldname = tdate.
  wa_str-col_pos = 1.
  APPEND wa_str TO it_strtab.
  CLEAR wa_str.
  PERFORM input_fieldcat USING tdate tdate.
ENDLOOP.
CALL METHOD cl_alv_table_create=>create_dynamic_table//生成動態內表
  EXPORTING
    it_fieldcatalog           = it_strtab
  IMPORTING
    EP_TABLE                  = dy_table
  EXCEPTIONS
    GENERATE_SUBPOOL_DIR_FULL = 1
    others                    = 2 .
assign dy_table->* to <dy_table>.
create data dy_wa like line of <dy_table>.
assign dy_wa->* to <dyn_wa>.
loop at itab.
  assign component 'BELNR' of structure <dyn_wa> to <fs>.
  <fs> = itab-belnr.//指定字段賦指定字段的值
  assign component 'DMBTR' of structure <dyn_wa> to <fs>.
  <fs> = itab-dmbtr.
  assign component 'WAERS' of structure <dyn_wa> to <fs>.
  <fs> = itab-waers.
  fieldname = itab-budat.
  assign component fieldname of structure <dyn_wa> to <fs>.
  <fs> = itab-menge.
  at end of belnr.
    append <dyn_wa> to <dy_table>.
    clear: <dyn_wa>.
  endat.
endloop.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
 EXPORTING
   I_CALLBACK_PROGRAM                = sy-repid
   IT_FIELDCAT_LVC                   = gt_fieldcat
  TABLES
    t_outtab                          = <dy_table>
 EXCEPTIONS
   PROGRAM_ERROR                     = 1
   OTHERS                            = 2 .

*---------------------------------------------------------------------*
*       FORM input_fieldcat                                           *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
*  -->  P_TDATE1                                                      *
*  -->  P_TDATE2                                                      *
*---------------------------------------------------------------------*
FORM input_fieldcat USING p_tdate1
                          p_tdate2.
  add_field p_tdate1 p_tdate2.
ENDFORM.
REPORT ztest_work6 .
TYPE-POOLS:slis.
DATA: dyntab LIKE dntab OCCURS 0 WITH HEADER LINE,
            gt_fcat TYPE lvc_t_fcat,
            gs_fcat TYPE lvc_s_fcat,
            gt_fieldcat   TYPE slis_t_fieldcat_alv,
            gs_fieldcat   TYPE slis_fieldcat_alv,
            dref TYPE REF TO data.
FIELD-SYMBOLS:<rec_tab> TYPE STANDARD TABLE.
PARAMETERS : p_table LIKE dd02l-tabname,
             hits LIKE rseumod-tbmaxsel.
CALL FUNCTION 'NAMETAB_GET'
     EXPORTING
          langu               = sy-langu
          tabname             = p_table
     TABLES
          nametab             = dyntab
     EXCEPTIONS
          internal_error      = 1
          table_has_no_fields = 2
          table_not_activ     = 3
          no_texts_found      = 4
          OTHERS              = 5.
LOOP AT dyntab.
  gs_fcat-fieldname = dyntab-fieldname.
  gs_fcat-ref_field = dyntab-fieldname.
  gs_fcat-ref_table = dyntab-tabname.
  APPEND gs_fcat TO gt_fcat.

  gs_fieldcat-fieldname = dyntab-fieldname.
  gs_fieldcat-ref_tabname = dyntab-tabname.
  APPEND gs_fieldcat TO gt_fieldcat.
ENDLOOP.
CALL METHOD cl_alv_table_create=>create_dynamic_table
  EXPORTING
    it_fieldcatalog = gt_fcat
  IMPORTING
    ep_table        = dref.
ASSIGN dref->* TO <rec_tab>.
SELECT * FROM (dyntab-tabname)
      INTO CORRESPONDING FIELDS OF TABLE <rec_tab> UP TO hits ROWS.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
     EXPORTING
          it_fieldcat   = gt_fieldcat
     TABLES
          t_outtab      = <rec_tab>
     EXCEPTIONS
          program_error = 1
          OTHERS        = 2.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值