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: 

Dymanic Table

Former Member
0 Kudos

Hello.

I want to create a table in an Abap program. I like that the table has a dynamic structure on execution time. Basically I like to add columns to the table in execution time.

If it is possible, would you gave me an example?

Thanks.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Check this example..

If you give the input as 3...IT will create 3 columns..

PARAMETERS: p_input TYPE i OBLIGATORY.

START-OF-SELECTION.

DATA: v_fieldname TYPE char30.

DATA: v_char TYPE numc4.

DATA: it_fldcat TYPE lvc_t_fcat.

DATA: wa_it_fldcat LIKE LINE OF it_fldcat.

DATA: gp_table TYPE REF TO data.

FIELD-SYMBOLS: <gt_table> TYPE table.

DO p_input TIMES.

v_fieldname = 'COL'.

v_char = sy-index.

CONCATENATE v_fieldname v_char INTO v_fieldname.

CONDENSE v_fieldname.

CLEAR wa_it_fldcat.

wa_it_fldcat-fieldname = v_fieldname.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-outputlen = 5.

wa_it_fldcat-intlen = 5.

APPEND wa_it_fldcat TO it_fldcat .

ENDDO.

  • Internal table creation..

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING it_fieldcatalog = it_fldcat

IMPORTING ep_table = gp_table.

ASSIGN gp_table->* TO <gt_table>.

CHECK sy-subrc = 0.

DATA: WA TYPE REF TO DATA.

  • Work area for the dynamic internal table.

CREATE DATA WA LIKE LINE OF <gt_table>.

WRITE: / 'Dynamic internal table created'.

Thanks,

Naren

3 REPLIES 3

Former Member
0 Kudos

Hi,

Check this example..

If you give the input as 3...IT will create 3 columns..

PARAMETERS: p_input TYPE i OBLIGATORY.

START-OF-SELECTION.

DATA: v_fieldname TYPE char30.

DATA: v_char TYPE numc4.

DATA: it_fldcat TYPE lvc_t_fcat.

DATA: wa_it_fldcat LIKE LINE OF it_fldcat.

DATA: gp_table TYPE REF TO data.

FIELD-SYMBOLS: <gt_table> TYPE table.

DO p_input TIMES.

v_fieldname = 'COL'.

v_char = sy-index.

CONCATENATE v_fieldname v_char INTO v_fieldname.

CONDENSE v_fieldname.

CLEAR wa_it_fldcat.

wa_it_fldcat-fieldname = v_fieldname.

wa_it_fldcat-datatype = 'CHAR'.

wa_it_fldcat-outputlen = 5.

wa_it_fldcat-intlen = 5.

APPEND wa_it_fldcat TO it_fldcat .

ENDDO.

  • Internal table creation..

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING it_fieldcatalog = it_fldcat

IMPORTING ep_table = gp_table.

ASSIGN gp_table->* TO <gt_table>.

CHECK sy-subrc = 0.

DATA: WA TYPE REF TO DATA.

  • Work area for the dynamic internal table.

CREATE DATA WA LIKE LINE OF <gt_table>.

WRITE: / 'Dynamic internal table created'.

Thanks,

Naren

ferry_lianto
Active Contributor
0 Kudos

Hi,

Please check this Rich's sample code.


report zrich_0003
       no standard page heading.
 
type-pools: slis.
 
field-symbols: <dyn_table> type standard table,
               <dyn_wa>,
               <dyn_field>.
 
data: alv_fldcat type slis_t_fieldcat_alv,
      it_fldcat type lvc_t_fcat.
 
 
selection-screen begin of block b1 with frame title text-001.
parameters: p_check type c.
selection-screen end of block b1.
 
start-of-selection.
 
  perform build_dyn_itab.
  perform build_report.
 
* Write out your dynamic internal table - one field at a time.
  loop at <dyn_table> into <dyn_wa>.
    do.
      assign component  sy-index  of structure <dyn_wa> to <dyn_field>.
      if sy-subrc <> 0.
        exit.
      endif.
      write:/ <dyn_field>.
    enddo.
  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 5 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.
 
* Add a field that is TYPE P
  clear wa_it_fldcat.
  wa_it_fldcat-fieldname = 'FIELDP' .
  wa_it_fldcat-datatype = 'CURR'.
  wa_it_fldcat-intlen = 10.
  append wa_it_fldcat to it_fldcat .
 
* 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>.
 
* Create a record to add to table
  do 5 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.
  assign component  'FIELDP'  of structure <dyn_wa> to <fs1>.
  <fs1> =  '1234.75'.
 
* Append to the dynamic internal table
  append <dyn_wa> to <dyn_table>.
 
endform.

Regards,

Ferry Lianto

former_member583013
Active Contributor
0 Kudos

Read my weblog -:) It an example of OO ALV with Dynamic Tables -;)

<a href="/people/alvaro.tejadagalindo/blog/2006/11/27/dynamic-alv-list-display ALV List Display</a>

Greetings,

Blag.