ABAP动态内表行转列

执行效果如下

直接上码

*&---------------------------------------------------------------------*
*& Report  ZTEXT_HZL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

report ztext_hzl.

types :begin of typ_date,
       fldate type sflight-fldate,
       end of typ_date.

data: gt_date type table of typ_date,
      gs_date type typ_date,
      gt_sflight type table of sflight,
      gs_sflight type sflight.
data:gt_components type abap_component_tab,
     gs_component  type abap_componentdescr,
     gr_structdesc type ref to cl_abap_structdescr,
     gr_tabledesc  type ref to cl_abap_tabledescr.


field-symbols: <fs_table> type standard table ,
               <fs_line>  type any,
               <fs_value> type any.

start-of-selection.
  "创建动态内表
  perform frm_create_dynamic.

  "填充数据
  perform frm_move_data.

  "输出alv
  perform frm_display_alv.

*&---------------------------------------------------------------------*
*&      Form  FRM_CREATE_DYNAMIC
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form frm_create_dynamic .
  data:lr_table type ref to data,
       lr_line  type ref to data.
  data: lv_field type string.
  select *
     from sflight
     into corresponding fields of table gt_sflight.
  sort gt_sflight by carrid.

  loop at  gt_sflight into gs_sflight.
    move-corresponding gs_sflight to gs_date.
    collect gs_date into gt_date.
  endloop.

  sort gt_date by fldate.

  perform frm_com_tab using 'CARRID' 'S_CARR_ID' changing gt_components.

  loop at gt_date into gs_date.
    lv_field = 'Z' && gs_date-fldate.
    perform frm_special_char_conver using 'X' changing lv_field. "字符替换  这里没有用,但是动态内表字段不支持一些特殊字符,保留这个字符替换
    perform frm_com_tab using lv_field 'STRING' changing gt_components.

  endloop.

  if gt_components is not initial.
    gr_structdesc = cl_abap_structdescr=>create( gt_components ).
    gr_tabledesc  = cl_abap_tabledescr=>create( p_line_type = gr_structdesc
                                                p_table_kind = cl_abap_tabledescr=>tablekind_std ).
  endif.

  create data lr_table type handle gr_tabledesc.
  assign lr_table->* to <fs_table>.

  create data lr_line type handle gr_structdesc.
  assign lr_line->* to <fs_line>.
endform.                    " FRM_CREATE_DYNAMIC
*&---------------------------------------------------------------------*
*&      Form  FRM_COM_TAB
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_0066   text
*      -->P_0067   text
*      <--P_LT_COM_TAB  text
*----------------------------------------------------------------------*
form frm_com_tab  using  u_name
                         u_type
                  changing ct_com_tab type abap_component_tab.
  data:ls_com_tab type abap_componentdescr.

  ls_com_tab-name = u_name.
  ls_com_tab-type ?= cl_abap_datadescr=>describe_by_name( u_type ).
  append ls_com_tab to ct_com_tab.

endform.                    " FRM_COM_TAB
*&---------------------------------------------------------------------*
*&      Form  FRM_SPECIAL_CHAR_CONVER
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_0088   text
*      <--P_LV_FIELD  text
*----------------------------------------------------------------------*
form frm_special_char_conver  using   uv_flag
                              changing cv_field.

  case  uv_flag.
    when  'X'.
      replace all occurrences of '/' in cv_field with '_'.
    when ''.
      replace all occurrences of '_' in cv_field with '/'.
    when others.
  endcase.

endform.                    " FRM_SPECIAL_CHAR_CONVER
*&---------------------------------------------------------------------*
*&      Form  FRM_MOVE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form frm_move_data .
  data: lv_field type string.

  loop at gt_sflight into gs_sflight.
    assign component 'CARRID' of structure <fs_line> to <fs_value>.
    if  <fs_value> is assigned.
      <fs_value> = gs_sflight-carrid.
      unassign <fs_value>.
    endif.

    lv_field = 'Z'&& gs_sflight-fldate.
    assign component lv_field of structure <fs_line> to <fs_value>.
    if  <fs_value> is assigned.
      <fs_value> = gs_sflight-price.
      unassign <fs_value>.
    endif.

    "如果是最后一个此航班号出现才附加进表里
    at end of carrid.
      append <fs_line> to <fs_table>.
      clear:<fs_line>.
    endat.
  endloop.


endform.                    " FRM_MOVE_DATA
*&---------------------------------------------------------------------*
*&      Form  FRM_DISPLAY_ALV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form frm_display_alv .
  data:lr_alv type ref to cl_salv_table,
       lx_msg type ref to cx_salv_msg,
       lr_columns type ref to cl_salv_columns_table,
       lr_column  type ref to cl_salv_column_table,
      lt_cols    type salv_t_column_ref,
      ls_cols    type salv_s_column_ref,
      lv_short_text type scrtext_s,
       lv_text type string.

  try .
      cl_salv_table=>factory(
      importing
        r_salv_table = lr_alv
       changing
         t_table = <fs_table>
      ).
      lr_alv->get_columns( )->set_optimize( abap_true ).
      lr_alv->get_functions( )->set_all( abap_true ).
      lr_alv->get_display_settings( )->set_striped_pattern( abap_true ).

      "修改列名
      lr_columns = lr_alv->get_columns( ).
      lt_cols = lr_columns->get( ).

      loop at lt_cols into ls_cols.
        case  ls_cols-columnname.
          when 'CARRID'.
            ls_cols-r_column->set_short_text( '航班编号' ).
          when others.
            lv_short_text = ls_cols-columnname.
            ls_cols-r_column->set_short_text( lv_short_text ).
        endcase.
      endloop.

      lr_alv->display( ).

    catch cx_salv_msg into lx_msg.
      lv_text = lx_msg->get_text( ).
      message lv_text type 'E'.
  endtry.

endform.                    " FRM_DISPLAY_ALV

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值