Application Development and Automation 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: 
Read only

Dynamic Table in ALV

Former Member
0 Likes
7,133

Hello experts,

is it possible to call an ALV via ( class cl_salv or fm REUSE_ALV_GRID_DISPLAY) with a dynamic table?

This dynamic table has for example one fix column for the material number and dynamic colums for additional data.

One material has 1 additional column, the other material has two additional columns.

So i need an ALV with 3 columns material add_data1 add_data2 (add_data2 of material one is empty this is ok).

To build an itab which can handle this is possible i know.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
4,553

Hello Benjamin,

Maybe you already found a solution for this, but if not (or for others looking for a solution), here is a sample program that creates, fills, and displays a dynamic table:

report ztpar_dynamic_salv.

parameters: p_colnr type i default 3.

start-of-selection.
  perform execute.

form execute.

  data t_table type ref to data.

** create dynamic table
  perform create_dynamic_table using p_colnr
                            changing t_table.

** fill dynamic table
  perform fill_dynamic_table changing t_table.

** display dynamic table
  perform display_table using t_table.

endform.

form create_dynamic_table using colnr type i
                       changing table type ref to data.
  data: lo_field type ref to cl_abap_typedescr,
        lo_struct type ref to cl_abap_structdescr,
        lo_table type ref to cl_abap_tabledescr.
  data: t_comp type cl_abap_structdescr=>component_table,
        l_comp like line of t_comp.

  lo_field ?= cl_abap_typedescr=>describe_by_name( 'CHAR10' ).
  do p_colnr times.
    move sy-index to l_comp-name.
    concatenate 'COLUMN' l_comp-name into l_comp-name.
    condense l_comp-name no-gaps.
    l_comp-type ?= lo_field.
    append l_comp to t_comp.
  enddo.

  lo_struct = cl_abap_structdescr=>create( p_components = t_comp p_strict = space ).
  lo_table = cl_abap_tabledescr=>create( lo_struct ).

  create data table type handle lo_table.
endform.

form fill_dynamic_table changing table type ref to data.
  field-symbols: <fs_table> type standard table,
                 <fs_line> type any,
                 <fs_field> type any.

  assign table->* to <fs_table>.
  do 5 times.
    append initial line to <fs_table> assigning <fs_line>.
    do.
      assign component sy-index of structure <fs_line> to <fs_field>.
      if sy-subrc ne 0.
        exit.
      endif.
      <fs_field> = sy-index.
    enddo.
  enddo.
endform.

form display_table using i_table type ref to data.
  data lo_alv type ref to cl_salv_table.

  field-symbols <fs_tab> type any table.

  assign i_table->* to <fs_tab>.
  try.
      cl_salv_table=>factory(
        importing
          r_salv_table = lo_alv
        changing
          t_table      = <fs_tab> ).
    catch cx_salv_msg.
      message 'Cannot display result!' type 'E'.
  endtry.

  lo_alv->display( ).
endform.

Best regards,

Tanguy

Hello experts,

is it possible to call an ALV via ( class cl_salv or fm REUSE_ALV_GRID_DISPLAY) with a dynamic table?

This dynamic table has for example one fix column for the material number and dynamic colums for additional data.

One material has 1 additional column, the other material has two additional columns.

So i need an ALV with 3 columns material add_data1 add_data2 (add_data2 of material one is empty this is ok).

To build an itab which can handle this is possible i know.

5 REPLIES 5
Read only

Former Member
0 Likes
4,553

Hi,

Yes it is possible,

you can create a dynamic internal table using cl_salv_table_create=>create_dynamic_table.

Thanks,

Anmol.

Read only

0 Likes
4,553

Hi,

i can not find class cl_salv_table_create in my system. Is there a spelling error?

Read only

0 Likes
4,553

It is CL_ALV_TABLE_CREATE

Thanks

Bala Duvvuri

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
4,553

So i need an ALV with 3 columns material add_data1 add_data2 (add_data2 of material one is empty this is ok).

Field will be dispalyed with a blank value. Only this is possible.

Read only

Former Member
0 Likes
4,554

Hello Benjamin,

Maybe you already found a solution for this, but if not (or for others looking for a solution), here is a sample program that creates, fills, and displays a dynamic table:

report ztpar_dynamic_salv.

parameters: p_colnr type i default 3.

start-of-selection.
  perform execute.

form execute.

  data t_table type ref to data.

** create dynamic table
  perform create_dynamic_table using p_colnr
                            changing t_table.

** fill dynamic table
  perform fill_dynamic_table changing t_table.

** display dynamic table
  perform display_table using t_table.

endform.

form create_dynamic_table using colnr type i
                       changing table type ref to data.
  data: lo_field type ref to cl_abap_typedescr,
        lo_struct type ref to cl_abap_structdescr,
        lo_table type ref to cl_abap_tabledescr.
  data: t_comp type cl_abap_structdescr=>component_table,
        l_comp like line of t_comp.

  lo_field ?= cl_abap_typedescr=>describe_by_name( 'CHAR10' ).
  do p_colnr times.
    move sy-index to l_comp-name.
    concatenate 'COLUMN' l_comp-name into l_comp-name.
    condense l_comp-name no-gaps.
    l_comp-type ?= lo_field.
    append l_comp to t_comp.
  enddo.

  lo_struct = cl_abap_structdescr=>create( p_components = t_comp p_strict = space ).
  lo_table = cl_abap_tabledescr=>create( lo_struct ).

  create data table type handle lo_table.
endform.

form fill_dynamic_table changing table type ref to data.
  field-symbols: <fs_table> type standard table,
                 <fs_line> type any,
                 <fs_field> type any.

  assign table->* to <fs_table>.
  do 5 times.
    append initial line to <fs_table> assigning <fs_line>.
    do.
      assign component sy-index of structure <fs_line> to <fs_field>.
      if sy-subrc ne 0.
        exit.
      endif.
      <fs_field> = sy-index.
    enddo.
  enddo.
endform.

form display_table using i_table type ref to data.
  data lo_alv type ref to cl_salv_table.

  field-symbols <fs_tab> type any table.

  assign i_table->* to <fs_tab>.
  try.
      cl_salv_table=>factory(
        importing
          r_salv_table = lo_alv
        changing
          t_table      = <fs_tab> ).
    catch cx_salv_msg.
      message 'Cannot display result!' type 'E'.
  endtry.

  lo_alv->display( ).
endform.

Best regards,

Tanguy