Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

dynamic internal table and dynamic field catalog

Former Member
0 Kudos

hi

i need to decide the number of fields of the internal table at runtime

and then need to pass value to this internal table.

then i need to create the field catalog for this internal table (so here

field catalog is also dynamic) to display in alv.

how to achieve this dynamic internal table creation and dyanmic field catalog generation

1 ACCEPTED SOLUTION

I355602
Advisor
Advisor
0 Kudos

Hi,

To create dynamic internal table follow:-

https://www.sdn.sap.com/irj/scn/advancedsearch?cat=sdn_wiki&query=dynamicinternaltable&adv=false&sortby=cm_rnd_rankvalue

And to create dynamic field catalog in alv use:-

https://www.sdn.sap.com/irj/scn/wiki?path=/display/abap/dynamic%252bfield%252bcatalog%252bin%252balv

Or use:-

Interactive Editable OO ALV grid with dynamic itab,FCAT and ENTER key event trigger

https://www.sdn.sap.com/irj/scn/wiki?path=/display/abap/interactive%252beditable%252boo%252balv%252b...

Hope they help you.

Regards,

Tarun

10 REPLIES 10

former_member262988
Active Contributor
0 Kudos

Hi ,

You can try like this.......

start-of-selection.

perform build_dyn_itab.

perform build_report.

loop at <dyn_table> into <dyn_wa>.

write:/ <dyn_wa>.

endloop.

************************************************************************

  • Build_dyn_itab

************************************************************************

form build_dyn_itab.

data: index(3) type c.

data: new_table type ref to data,

new_line type ref to data,

wa_it_fldcat type lvc_s_fcat.

  • Create fields

clear index.

do 10 times.

index = sy-index.

clear wa_it_fldcat.

concatenate 'Field' index into

wa_it_fldcat-fieldname .

condense wa_it_fldcat-fieldname no-gaps.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-intlen = 5.

append wa_it_fldcat to it_fldcat .

enddo.

  • Create dynamic internal table and assign to FS

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = it_fldcat

importing

ep_table = new_table.

assign new_table->* to <dyn_table>.

  • Create dynamic work area and assign to FS

create data new_line like line of <dyn_table>.

assign new_line->* to <dyn_wa>.

endform.

*********************************************************************

  • Form build_report

*********************************************************************

form build_report.

data: fieldname(20) type c.

data: fieldvalue(5) type c.

data: index(3) type c.

field-symbols: <fs1>.

do 10 times.

index = sy-index.

  • Set up fieldname

concatenate 'FIELD' index into

fieldname .

condense fieldname no-gaps.

  • Set up fieldvalue

concatenate 'FLD' index into

fieldvalue.

condense fieldvalue no-gaps.

assign component fieldname of structure <dyn_wa> to <fs1>.

<fs1> = fieldvalue.

enddo.

  • Append to the dynamic internal table

append <dyn_wa> to <dyn_table>.

endform.

Thanks,

Shailaja Ainala.

Former Member

Former Member
0 Kudos

Hi, Use the following steps,


FIELD-SYMBOLS : <fs_final_itab> TYPE table,       " Final Dynamic Table
                <fs_wa_itab> TYPE ANY,            " Final Itab Work Area
                <l_field1> TYPE ANY,

* Build a Field Catalog with two Fields of your Internal Table.
  PERFORM f0310_field_catalog USING 'G_T_DISPLAY'  'HEADING' text-024  '1' 'X' 'L'.
  PERFORM f0310_field_catalog USING 'G_T_DISPLAY'  'ENAME'   text-025  '2' ''  'L'.
  PERFORM f0310_field_catalog USING 'G_T_DISPLAY'  'COLOR'   text-026  '3' ''  'L'.

*Calling the method to create Dynamic Internal Table
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = g_t_fieldcat
    IMPORTING
      ep_table        = d_ref.

  ASSIGN d_ref->* TO <fs_final_itab>.                        " Assigning the Final Table,

* Create Dynamic Work Area and assign to FS
  CREATE DATA dy_line LIKE LINE OF <fs_final_itab>.
  ASSIGN dy_line->* TO <fs_wa_itab>.

Former Member
0 Kudos

Hi Ajay,

[dyniamic internal table creation|]

for the filed catalog use the REUSE_ALV_FIELDCATALOG_MERGE

hope it helps you.

Thanks!

viquar_iqbal
Active Contributor
0 Kudos

Hi

check this thread for dynamic internal table creation

dynamic field catalog creation

Former Member
0 Kudos

Hi,

Refer the following program.

Use LOOP statement wherever necessary.

REPORT  z_dyn_table.

DATA: new_table TYPE REF TO data,
      new_line  TYPE REF TO data,
      is_lvc_cat TYPE lvc_s_fcat,
      it_lvc_cat TYPE lvc_t_fcat.

FIELD-SYMBOLS: <l_table> TYPE table,
               <l_line>  TYPE ANY,
               <l_field> TYPE ANY.

DATA d(3000).

START-OF-SELECTION.
  PERFORM field_catalog.
  PERFORM set_data.


*&---------------------------------------------------------------------*
*&      Form  field_catalog
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM field_catalog .

*  DO 5 TIMES.
  is_lvc_cat-fieldname = 'EBELN'.
  is_lvc_cat-ref_field = 'EBELN'.
  is_lvc_cat-ref_table = 'EKKO'.
*  is_lvc_cat-scrtext_s  = is_lvc_cat-scrtext_m =
*  is_lvc_cat-scrtext_l  = 'Company Code'.
  APPEND is_lvc_cat TO it_lvc_cat.
*  ENDDO.

  is_lvc_cat-fieldname = 'EBELP'.
  is_lvc_cat-ref_field = 'EBELP'.
  is_lvc_cat-ref_table = 'EKPO'.
*  is_lvc_cat-scrtext_s  = is_lvc_cat-scrtext_m =
*  is_lvc_cat-scrtext_l  = 'Company Code'.
  APPEND is_lvc_cat TO it_lvc_cat.

ENDFORM.                    "field_catalog

*&---------------------------------------------------------------------*
*&      Form  set_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM set_data.
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = it_lvc_cat
    IMPORTING
      ep_table        = new_table.

  ASSIGN new_table->* TO <l_table>.
  CREATE DATA new_line LIKE LINE OF <l_table>.
  ASSIGN new_line->* TO <l_line>.

  ASSIGN COMPONENT 'EBELN' OF STRUCTURE <l_line> TO <l_field>.
  <l_field> = '800001'.

   ASSIGN COMPONENT 'EBELN' OF STRUCTURE <l_line> TO <l_field>.
  <l_field> = '800002'.

  ASSIGN COMPONENT 'EBELP' OF STRUCTURE <l_line> TO <l_field>.
  <l_field> = '00001'.

  INSERT <l_line> INTO TABLE <l_table>.

Regards,

Prem

Former Member
0 Kudos

Hi Ajay

For dynamic internal table you can use tis method.

PARAMETERS P_TAB TYPE RSRD1-TABMA-VAL.

DATA:
 T_TAB TYPE RSRD1-TABMA-VAL.

MOVE P_TAB TO T_TAB.

Now pass this internal table name to your method

SET_TABLE_FOR_FIRST_DISPLAY.

For dynamic field catalog see this WIKI.This explains with screen shote and example.

[https://wiki.sdn.sap.com/wiki/display/ABAP/DYNAMICFIELDCATALOGINALV]

Regards

Hareesh Menon

I355602
Advisor
Advisor
0 Kudos

Hi,

To create dynamic internal table follow:-

https://www.sdn.sap.com/irj/scn/advancedsearch?cat=sdn_wiki&query=dynamicinternaltable&adv=false&sortby=cm_rnd_rankvalue

And to create dynamic field catalog in alv use:-

https://www.sdn.sap.com/irj/scn/wiki?path=/display/abap/dynamic%252bfield%252bcatalog%252bin%252balv

Or use:-

Interactive Editable OO ALV grid with dynamic itab,FCAT and ENTER key event trigger

https://www.sdn.sap.com/irj/scn/wiki?path=/display/abap/interactive%252beditable%252boo%252balv%252b...

Hope they help you.

Regards,

Tarun

Former Member
0 Kudos

Hi Ajay,

U can use the below code to create a dynamic internal table.

*adding the field names only once for the dynamic table .

MOVE 'PRCTR' TO gw_component-name.

gw_component-type ?= cl_abap_elemdescr=>get_string( ).

INSERT gw_component INTO TABLE gt_components.

MOVE 'RCNTR' TO gw_component-name.

gw_component-type ?= cl_abap_elemdescr=>get_string( ).

INSERT gw_component INTO TABLE gt_components.

MOVE 'RACCT' TO gw_component-name.

gw_component-type ?= cl_abap_elemdescr=>get_string( ).

INSERT gw_component INTO TABLE gt_components.

MOVE 'RYEAR' TO gw_component-name.

gw_component-type ?= cl_abap_elemdescr=>get_string( ).

INSERT gw_component INTO TABLE gt_components.

MOVE 'YTDBAL' TO gw_component-name.

gw_component-type ?= cl_abap_elemdescr=>get_string( ).

INSERT gw_component INTO TABLE gt_components.

MOVE 'OBAL' TO gw_component-name.

gw_component-type ?= cl_abap_elemdescr=>get_string( ).

INSERT gw_component INTO TABLE gt_components.

*get structure descriptor -> GR_STRUCTDESCR

gr_structdescr ?= cl_abap_structdescr=>create( gt_components ).

  • create work area of structure GR_STRUCTDESCR -> GR_WA

CREATE DATA gr_wa TYPE HANDLE gr_structdescr.

ASSIGN gr_wa->* TO <gw_wa>.

  • determine key components -> GT_KEYS

MOVE lv_value1 TO gw_key-name.

INSERT gw_key INTO TABLE gt_keys.

  • create descriptor for internal table -> GR_TABLEDESCR

gr_tabledescr ?= cl_abap_tabledescr=>create( p_line_type = gr_structdescr

p_table_kind = cl_abap_tabledescr=>tablekind_hashed

p_unique = abap_true

p_key = gt_keys

p_key_kind = cl_abap_tabledescr=>keydefkind_user ).

  • create internal table -> GR_ITAB

CREATE DATA gr_itab TYPE HANDLE gr_tabledescr.

ASSIGN gr_itab->* TO <gt_itab>.

CREATE DATA gr_itab LIKE STANDARD TABLE OF <gw_wa>.

ASSIGN gr_itab->* TO <gt_sttab>.

Now u r internal table named <gt_sttab> has been created with fields like RCNTR, PRCTR,RACCT, RYEAR etc whatever the field u need u can go ahead and create dynamically.

then by using the table <gt_sttab> u can create u r field catalog.

Regards,

Rose.

0 Kudos

Thanks, very good documentation.