SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Как динамически создать внутреннюю таблицу



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ABAP
View previous topic :: View next topic  
Author Message
Лазарев
Участник
Участник



Joined: 26 Sep 2007
Posts: 22

PostPosted: Wed Sep 26, 2007 8:59 am    Post subject: Как динамически создать внутреннюю таблицу Reply with quote

Привет!
Скиньте, пожалуйста, пример динамического определения внутренней таблицы? Изначально тип и количество полей неизвестно.
Back to top
View user's profile Send private message
Lord
Профессионал
Профессионал



Joined: 10 Sep 2007
Posts: 168

PostPosted: Wed Sep 26, 2007 6:10 pm    Post subject: Reply with quote

Воспользуйся методом:
cl_alv_table_create=>create_dynamic_table

Вот несколько примеров:
Создаете и заполянете fieldcatalog
Code:

* structure for new LVC (ALV grid container) field catalog
DATA: ls_lvc_fieldcat TYPE lvc_s_fcat,
      gt_lvc_fieldcatalog TYPE TABLE OF lvc_s_fcat,
* internal table for old (standard) ALV field catalog
* (without header line - exactly as in function
* 'REUSE_ALV_FIELDCATALOG_MERGE')
lt_alv_fieldcatalog TYPE slis_t_fieldcat_alv.

FIELD-SYMBOLS: <ls_alv_fieldcat> TYPE slis_fieldcat_alv.

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
  EXPORTING
    i_program_name = g_lvc_program_name
    i_internal_tabname = c_outtab_structure_input
    i_inclname = c_data_declar_include_name
  CHANGING
    ct_fieldcat = lt_alv_fieldcatalog
  EXCEPTIONS
    inconsistent_interface = 1
    program_error = 2
    OTHERS = 3.

  IF NOT sy-subrc IS INITIAL.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  REFRESH gt_lvc_fieldcatalog.

*** map old fieldcatalog fields on new:
LOOP AT lt_alv_fieldcatalog ASSIGNING <ls_alv_fieldcat>.
  CLEAR ls_lvc_fieldcat.
  MOVE-CORRESPONDING <ls_alv_fieldcat> TO ls_lvc_fieldcat.
* specific fieldnames caused by length restriction for basis objects
  ls_lvc_fieldcat-ref_field = <ls_alv_fieldcat>-ref_fieldname.
  ls_lvc_fieldcat-ref_table = <ls_alv_fieldcat>-ref_tabname.
  ls_lvc_fieldcat-roundfield = <ls_alv_fieldcat>-roundfieldname.
  ls_lvc_fieldcat-decmlfield = <ls_alv_fieldcat>-decimalsfieldname.
  ls_lvc_fieldcat-decimals_o = <ls_alv_fieldcat>-decimals_out.
  ls_lvc_fieldcat-dd_outlen = <ls_alv_fieldcat>-ddic_outputlen.
* for fields without reference to the Data Dictionary
  IF ls_lvc_fieldcat-ref_table IS INITIAL.
    ls_lvc_fieldcat-coltext = ls_lvc_fieldcat-fieldname.
  ENDIF.
  APPEND ls_lvc_fieldcat TO gt_lvc_fieldcatalog.
ENDLOOP.


2) На основе созданного FieldCatalog создете динамическую таблицу.
Code:

DATA: lr_table TYPE REF TO data.
FIELD-SYMBOLS: <ls_lvc_outtab> TYPE ANY,
               <l_field> TYPE ANY,
               <gt_lvc_outtab> TYPE TABLE.

* create dynamic table (according to fieldcatalog)
CALL METHOD cl_alv_table_create=>create_dynamic_table
  EXPORTING it_fieldcatalog = gt_lvc_fieldcatalog
  IMPORTING ep_table = lr_table.

ASSIGN lr_table->* TO <gt_lvc_outtab>.


3) Заполним отдельные поля и добавим запись к динамической таблице.
Code:

ASSIGN LOCAL COPY OF INITIAL LINE OF <gt_lvc_outtab>
                                                        TO <ls_lvc_outtab>.

...
ASSIGN COMPONENT l_index OF STRUCTURE <ls_lvc_outtab>
TO <l_field>.
<l_field> = l_value.

...
APPEND <ls_lvc_outtab> TO <gt_lvc_outtab>.
CLEAR <ls_lvc_outtab>.


И еще пример:
Code:

REPORT Z_DYN_ALV message-id 00.

data : t_field_catalog type lvc_t_fcat,
t_table type ref to data.

data : datavalue type ref to data.

data : wtype type c.
data : wcomponents type i.

field-symbols: <fs_t> type table,
                      <fs_w> type any,
                      <fs_f> type any.

parameters:  p_table type dd02l-tabname.

start-of-selection.
  perform validate_table_name.
  perform build_field_catalog.
  perform get_table_data.

form validate_table_name.
  data : l_dd02l like dd02l.

  select single * into l_dd02l from dd02l where tabname eq p_table.

  IF sy-subrc ne 0.
    message s398 with text-001 text-002.
    stop.
  ENDIF.
endform.

form build_field_catalog.
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name = p_table
    CHANGING
      ct_fieldcat = t_field_catalog.
endform.

form get_table_data.
  call method cl_alv_table_create=>create_dynamic_table
    exporting it_fieldcatalog = t_field_catalog
    importing ep_table = t_table.

  assign t_table->* to <fs_t>.

  select * into corresponding fields of table <fs_t>
  from (p_table) up to 10 rows.

  if <fs_t> is initial.
    message s398 with text-003 p_table.
    stop.
  endif.

  create data datavalue type (p_table).

  assign datavalue->* to <fs_w>.

  describe field <fs_w> type wtype components wcomponents.

  loop at <fs_t> assigning <fs_w>.
    do wcomponents times.
      assign component syst-index of structure <fs_w> to <fs_f>.
      write : / <fs_f>.
    enddo.
  endloop.
endform.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ABAP All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.