ABAP 动态内表 动态ALV

  DATA: DY_TABLE TYPE REF TO DATA,
        DY_WA    TYPE REF TO DATA.

  FIELD-SYMBOLS: <DYN_TABLE> TYPE TABLE,  "是一个标志,可以理解为一个指针,将来创建的内表就要通过它们来访问
                 <DYN_WA> TYPE ANY,
                 <FS> TYPE ANY.
  DATA: L_SCRFNAME  TYPE SCRFNAME,
        LS_LAYOUT   TYPE LVC_S_LAYO,
        LT_FIELDCAT TYPE LVC_T_FCAT,
        LT_EXCLUDE  TYPE UI_FUNCTIONS.
*  此方法用于构建动态内表,输入=构建的结构,输出=dy_table
  CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
    EXPORTING
      IT_FIELDCATALOG = LT_FIELDCAT
    IMPORTING
      EP_TABLE        = DY_TABLE.

  ASSIGN DY_TABLE->* TO <DYN_TABLE>.
  CREATE DATA DY_WA LIKE LINE OF <DYN_TABLE>.
  ASSIGN DY_WA->* TO <DYN_WA>.

   DATA FIELDNAME(20).
   SORT IT_DAT_KY[] BY MATNR.
   LOOP AT IT_DAT_KY.
      "这里把日期类型定义为char型,因为用DATS报错。
      FIELDNAME = IT_DAT_KY-DATUM.
      ASSIGN COMPONENT FIELDNAME OF STRUCTURE <dyn_wa> TO <FS>.
      <FS> = IT_DAT_KY-MENGE.
      ASSIGN COMPONENT 'MGSUM' OF STRUCTURE <dyn_wa> TO <FS>.
      <FS> = <FS> + IT_DAT_KY-MENGE.
      AT END OF MATNR.
        "分配结构里的物料号,用FS字段 指向它
        ASSIGN COMPONENT 'MATNR' OF STRUCTURE <dyn_wa> TO <FS>.
        <FS> = IT_DAT_KY-MATNR.
        ASSIGN COMPONENT 'MAKTX' OF STRUCTURE <dyn_wa> TO <FS>.
        SELECT SINGLE MAKTX INTO @DATA(L_MAKTX) FROM MAKT
         WHERE MATNR = @IT_DAT_KY-MATNR AND SPRAS = @SY-LANGU.
        <FS> = L_MAKTX.
         APPEND <dyn_wa> TO <dyn_table>.
         CLEAR <dyn_wa>.
      ENDAT.
   ENDLOOP.
*& 102
  DEFINE  FCAT.
    CLEAR : LS_FIELDCAT.
    LS_FIELDCAT-FIELDNAME = &1.
    LS_FIELDCAT-COLTEXT   = &2.
    LS_FIELDCAT-SELTEXT   = &2.
    LS_FIELDCAT-SCRTEXT_L = &2.
    LS_FIELDCAT-INTLEN    = &3.
    LS_FIELDCAT-INTTYPE = 'C'.
    LS_FIELDCAT-OUTPUTLEN  = 10.
    IF &1 = 'MATNR'.
      LS_FIELDCAT-KEY = 'X'.
      LS_FIELDCAT-REF_TABLE = 'MARA'.
      LS_FIELDCAT-REF_FIELD = 'MATNR'.
    ENDIF.
    IF &1 = 'MAKTX'.
      LS_FIELDCAT-KEY = 'X'.
      LS_FIELDCAT-OUTPUTLEN  = 20.
    ENDIF.
    IF &1 = 'MGSUM'.
      LS_FIELDCAT-JUST  = 'R' .
      LS_FIELDCAT-EMPHASIZE = 'C310' .
    ENDIF.
    APPEND LS_FIELDCAT TO P_LT_FIELDCAT.
  END-OF-DEFINITION.


    CLEAR P_LS_LAYOUT.
    P_LS_LAYOUT-BOX_FNAME  = 'SEL'.
    P_LS_LAYOUT-ZEBRA      = 'X'.

    CLEAR   P_LT_FIELDCAT.
    LS_FIELDCAT-CHECKBOX  = 'X'.
    LS_FIELDCAT-FIELDNAME = 'SEL'.
    LS_FIELDCAT-SCRTEXT_L = '选择'.
    LS_FIELDCAT-NO_OUT    = 'X'.
    LS_FIELDCAT-EDIT      = 'X'.
    APPEND LS_FIELDCAT TO P_LT_FIELDCAT.

    SORT TDATE[].
    LOOP AT TDATE.
      CLEAR L_DATUM.
      CALL FUNCTION 'FORMAT_DATE_4_OUTPUT'
        EXPORTING
          DATIN         = TDATE
          FORMAT        = 'YYYY.MM.DD'
        IMPORTING
          DATEX         = L_DATUM .
      FCAT TDATE L_DATUM '20'.
    ENDLOOP.

 

posted @ 2017-09-07 15:13  明大叔  阅读(5056)  评论(0编辑  收藏  举报