вторник, 28 апреля 2015 г.

Field catalog for internal table in ALV

Шикарно:



FORM create_fieldcatalog
      
USING     pt_table     TYPE ANY TABLE
       
CHANGING  pt_fieldcat  TYPE lvc_t_fcat.

  
DATA:
        lr_tabdescr 
TYPE REF TO cl_abap_structdescr,
        lr_data     
TYPE REF TO data,
        lt_dfies    
TYPE ddfields,
        ls_dfies    
TYPE dfies,
        ls_fieldcat 
TYPE lvc_s_fcat.
*-----------------------------------------------------------------------
  
CLEAR pt_fieldcat.

  
CREATE DATA lr_data LIKE LINE OF pt_table.

  lr_tabdescr ?= cl_abap_structdescr
=>describe_by_data_reflr_data ).

  lt_dfies 
cl_salv_data_descr=>read_structdescrlr_tabdescr ).

  
LOOP AT lt_dfies INTO ls_dfies.

    
CLEAR ls_fieldcat.

    
MOVE-CORRESPONDING ls_dfies TO ls_fieldcat.

    
APPEND ls_fieldcat TO pt_fieldcat.

  
ENDLOOP.
ENDFORM.

Ещё вариант:

REPORT ZTST_TEST.


data:
  gv_tabname type string value `/BIC/MZCAPSID`.
data:
*  Field description
  lt_tab_descr type ddfields,
  ls_tab_descr_line like line of lt_tab_descr,
*  Field catalog descr
  lt_fields type lvc_t_fcat,
  ls_fields_line like line of lt_fields.
*

data:
*  Data reference for dynamic table and its line
  ldr_itab type ref to data,
  ldr_itab_line type ref to data.
*

field-symbols:
*  Field symbols for handling dynamic table
  <lfs_itab> type any table,
*  Field symbols for handling rows of dynamic table
  <lfs_itab_row> type any,
  <lfs_itab_row2> type any.
*

lt_tab_descr = cast cl_abap_structdescr(
  cl_abap_typedescr=>describe_by_name( gv_tabname ) "'/BIC/QZTST1' )
)->get_ddic_field_list(
    p_langu = 'R'
    p_including_substructres = abap_true
).

loop at lt_tab_descr                         "можно просто move-coresponding
  into ls_tab_descr_line.
    clear: ls_fields_line.
    move-corresponding ls_tab_descr_line to ls_fields_line.
    append ls_fields_line to lt_fields.
endloop.

cl_alv_table_create=>create_dynamic_table(
  exporting
    it_fieldcatalog = lt_fields
    i_length_in_byte = 'X'
  importing
    ep_table = ldr_itab
).

assign ldr_itab->* to <lfs_itab>.
create data ldr_itab_line like line of <lfs_itab>.
assign ldr_itab_line->* to <lfs_itab_row2>.

select *
  up to 10 rows
from (gv_tabname)
  into table @<lfs_itab>

1 комментарий: