动态生成内表

动态生成内表的code

REPORT ztest_dynamic.

TYPE-POOLS:slis.

PARAMETERS:p_col TYPE i DEFAULT 10 OBLIGATORY, "动态列
           p_rec TYPE i DEFAULT 15 OBLIGATORY. "记录数-行数

DATA:gv_index TYPE char2.

DATA:BEGIN OF gt_data OCCURS 0,
      index TYPE char4,
      fskey TYPE char5,
      price TYPE netpr,
     END OF gt_data.
DATA:gs_data LIKE gt_data.

DATA:gt_alv_cat  TYPE TABLE OF lvc_s_fcat,
     gs_alv_cat  LIKE LINE OF gt_alv_cat,
     gs_layout   TYPE slis_layout_alv,
     gt_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE.

FIELD-SYMBOLS: <dr_table> TYPE table.

START-OF-SELECTION.

  CHECK p_col LT 100.

* 行数
  DO p_rec TIMES.
    CLEAR gt_data.
    gt_data-index = sy-index.
* 动态列数
    DO p_col TIMES.
      gv_index = sy-index.
      CONCATENATE 'COL' gv_index INTO gt_data-fskey.
      gt_data-price = sy-index.
      APPEND gt_data.
    ENDDO.
  ENDDO.

  PERFORM frm_create_dynamic_table.

END-OF-SELECTION.

  CHECK <dr_table> IS ASSIGNED.

  PERFORM frm_alv_output.

*&---------------------------------------------------------------------*
*&      Form  FRM_CREATE_DYNAMIC_TABLE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_create_dynamic_table.

  DATA: ls_alv_cat LIKE LINE OF gt_alv_cat.
  DATA: d_ref TYPE REF TO data.

  FIELD-SYMBOLS: <d_ref> TYPE table.
  DATA: dr_line  TYPE REF TO data.
  FIELD-SYMBOLS: <dr_line>  TYPE ANY.
  FIELD-SYMBOLS: <dr_field> TYPE ANY.

  ls_alv_cat-fieldname = 'KEY'.
  ls_alv_cat-intlen    = 4.
  APPEND ls_alv_cat TO gt_alv_cat.

  LOOP AT gt_data WHERE index EQ 1.
    ls_alv_cat-fieldname = gt_data-fskey.
    ls_alv_cat-intlen    = 11.
    ls_alv_cat-decimals  = 2.
    ls_alv_cat-inttype   = 'P'.
    APPEND ls_alv_cat TO gt_alv_cat.
  ENDLOOP.

* internal table build
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = gt_alv_cat
    IMPORTING
      ep_table        = d_ref.

  ASSIGN d_ref->* TO <d_ref>.

* Create dynamic work area and assign to FS
  CREATE DATA dr_line LIKE LINE OF <d_ref>.
  ASSIGN dr_line->* TO <dr_line>.

  LOOP AT gt_data.
    MOVE gt_data TO gs_data.
    ASSIGN COMPONENT gs_data-fskey OF STRUCTURE <dr_line> TO <dr_field>.
    <dr_field> = gt_data-price.

    AT END OF index.
      ASSIGN COMPONENT 'KEY' OF STRUCTURE <dr_line> TO <dr_field>.
      <dr_field> = gs_data-index.
      APPEND <dr_line> TO <d_ref>.
      CLEAR <dr_line>.
    ENDAT.

  ENDLOOP.

  ASSIGN <d_ref> TO <dr_table>.

ENDFORM.                    " FRM_CREATE_DYNAMIC_TABLE
*&---------------------------------------------------------------------*
*&      Form  FRM_ALV_OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_alv_output.

  PERFORM fieldcat_append_proc: USING 'X'
                                      'KEY'
                                      '固定列'
                                      space
                                      space.

  LOOP AT gt_alv_cat INTO gs_alv_cat FROM 2.
    PERFORM fieldcat_append_proc USING space
                                       gs_alv_cat-fieldname
                                       gs_alv_cat-fieldname
                                       'R'
                                       'X'.
  ENDLOOP.

  gs_layout-zebra = 'X'.
  gs_layout-colwidth_optimize = 'X'.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_background_id         = 'ALV_BACKGROUND'
*      i_callback_user_command = 'USER_COMMAND'
      i_callback_program      = sy-repid
      is_layout               = gs_layout
      it_fieldcat             = gt_fieldcat[]
    TABLES
      t_outtab                = <dr_table>.

ENDFORM.                    " FRM_ALV_OUTPUT
*&---------------------------------------------------------------------*
*&      Form  fieldcat_append_proc
*&---------------------------------------------------------------------*
*       add the fieldcat
*----------------------------------------------------------------------*
*      -->P_FIELDNAME  field name
*      -->P_DDIC       field description
*----------------------------------------------------------------------*
FORM fieldcat_append_proc  USING p_key
                                 p_fieldname
                                 p_ddic
                                 p_just
                                 p_hot.
  CLEAR gt_fieldcat.
  gt_fieldcat-key           =   p_key.
  gt_fieldcat-fieldname     =   p_fieldname.
  gt_fieldcat-reptext_ddic  =   p_ddic.
  gt_fieldcat-just          =   p_just.
  gt_fieldcat-cfieldname    =   space.
  gt_fieldcat-do_sum        =   p_hot.
  APPEND gt_fieldcat.

ENDFORM.                    " fieldcat_append

你可能感兴趣的:(职场,SAP,休闲,动态生成内表)