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

alv grid using dynamic internal table

Former Member
0 Likes
3,939

hi i have an internal table

begin of itab

equipno like equi-equnr,

reading like imrg-readg

uom like imrg-uom

date like imrg-date

end of itab.

and many more fields in this table the reading, date, uom are dynamic its not fixed there can be five readings and 6 reading etc

if i get all the data populated into final table itab. how to display it throgh the alv grid

i can use fieldsymbols i have seen some documents i want to know how to populate the fieldname s of the dynamic internal table to the field catalog and the values of the dynamic internal table .its a alv grid display pls guide

1 ACCEPTED SOLUTION
Read only

Subhankar
Active Contributor
0 Likes
2,254

Hi,

Before displaying the records in ALV, you integrate all the fields (Fixed fields as well as variable fields) into one table. To do so you need create one dynamic table. If you already created this dynamic table then in the same way (same sequence) you need to populate the field catalog and use normal FM REUSE_ALV_GRID_DISPLAY to display it.

If you not aware of how to create this dynamic internal table, please check the below way.

1. Populate the fixed field into one field catalog(NB. this to create dyn table not ALV).

*--- Cons Unit

wa_lvc_cat-fieldname = c_cons.

wa_lvc_cat-ref_field = c_rbunit.

wa_lvc_cat-ref_table = c_ecmca.

append wa_lvc_cat to it_lvc_cat.

*--- account description

wa_lvc_cat-fieldname = c_acctd.

wa_lvc_cat-ref_field = c_txtmi.

wa_lvc_cat-ref_table = c_tf101.

append wa_lvc_cat to it_lvc_cat.

2. For variable fields you have your own logic based on which you need to display the output. (like loop at one table and make each field as one column). Here one thing need to make sure every time field name should be different. You know in one structure there are 2 fields with same name is not allowed.

3. next use the below method to create the internal table

  • Create a new Table

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = it_lvc_cat

importing

ep_table = it_new_table.

  • Create a new Line with the same structure of the table.

assign it_new_table->* to <l_table>. " internal table

create data wa_new_line like line of <l_table>.

assign wa_new_line->* to <l_line>. " Work area

4. To populate the field catalog for ALV use the same sequence. Better while you populated it_lvc_cat for dynamic table also create one lookup table with three fields. (fieldname, Text, col_pos). Here loop into this look up table and populate the ALV field catalog.

5. Display it using REUSE_ALV_GRID_DISPLAY

call function 'REUSE_ALV_GRID_DISPLAY'

exporting

i_callback_program = c_rep_name

i_callback_user_command = 'F_USER_DOUBLE_CLICK' "SD0K963313

is_layout = l_wa_layout

it_fieldcat = it_fieldcat

it_sort = it_sort

i_save = l_save

is_variant = wa_variant1

it_events = it_events

tables

t_outtab = <l_table>

exceptions

program_error = 1

others = 2.

Thanks

Subhankar

8 REPLIES 8
Read only

guillaume-hrc
Active Contributor
0 Likes
2,254

Hi,

I use SALV for the same and it retrieves all descriptions itself (by introspection / RTTS, I guess).

Best regards,

Guillaume

Read only

0 Likes
2,254

is there any sample code how to use it pls let me know

Read only

0 Likes
2,254

Hi,

This is a dummy example but it shows the handling of dynamic table and SALV display of the resulting table:

REPORT  zggar_alv_dynamic_table.

DATA: wt_data               TYPE REF TO data.

DATA: wo_alv                TYPE REF TO cl_salv_table,
      wo_alv_functions      TYPE REF TO cl_salv_functions_list.

FIELD-SYMBOLS: <ft_data>  TYPE STANDARD TABLE,
               <fs_data>  TYPE ANY,
               <fs_cell>  TYPE ANY.

PARAMETERS: p_nbr   TYPE i DEFAULT 10,
            p_type  TYPE string DEFAULT 'PERNR_D'.

START-OF-SELECTION.

  DATA: l_fldcat   TYPE lvc_s_fcat,
        lo_newtype TYPE REF TO cl_abap_structdescr,
        lo_tabtype TYPE REF TO cl_abap_tabledescr,
        lo_tmptype TYPE REF TO cl_abap_typedescr,
        t_comp     TYPE cl_abap_structdescr=>component_table,
        ls_comp     LIKE LINE OF t_comp.

  DO p_nbr TIMES.
    CLEAR: ls_comp.

    ls_comp-name = sy-index.
    CONCATENATE 'CHAMP_' ls_comp-name INTO ls_comp-name.
    CONDENSE ls_comp-name NO-GAPS.
    lo_tmptype ?= cl_abap_typedescr=>describe_by_name( p_type ).

    MOVE lo_tmptype ?TO ls_comp-type.
    APPEND ls_comp TO t_comp.
  ENDDO.

  lo_newtype = cl_abap_structdescr=>create( t_comp ).
  lo_tabtype = cl_abap_tabledescr=>create( lo_newtype ).

  CLEAR wt_data.
  CREATE DATA wt_data TYPE HANDLE lo_tabtype.

  ASSIGN wt_data->* TO <ft_data>.
  CHECK sy-subrc = 0.

  APPEND INITIAL LINE TO <ft_data> ASSIGNING <fs_data>.
  IF <fs_data> IS ASSIGNED.
    DO.
      ASSIGN COMPONENT sy-index OF STRUCTURE <fs_data> TO <fs_cell>.
      IF sy-subrc = 0.
        <fs_cell> = sy-index.
      ELSE.
        EXIT.
      ENDIF.
    ENDDO.
  ENDIF.

  CALL METHOD cl_salv_table=>factory
    IMPORTING
      r_salv_table = wo_alv
    CHANGING
      t_table      = <ft_data>.

** Functions
  wo_alv_functions = wo_alv->get_functions( ).
  wo_alv_functions->set_default( 'X' ).
  wo_alv_functions->set_all( 'X' ).

** Display the table
  wo_alv->display( ).

Read only

0 Likes
2,254

when i tries to use the same code i got an error saying cl_salv_table is unknown

can it be used in 4.6 c??

Read only

Subhankar
Active Contributor
0 Likes
2,255

Hi,

Before displaying the records in ALV, you integrate all the fields (Fixed fields as well as variable fields) into one table. To do so you need create one dynamic table. If you already created this dynamic table then in the same way (same sequence) you need to populate the field catalog and use normal FM REUSE_ALV_GRID_DISPLAY to display it.

If you not aware of how to create this dynamic internal table, please check the below way.

1. Populate the fixed field into one field catalog(NB. this to create dyn table not ALV).

*--- Cons Unit

wa_lvc_cat-fieldname = c_cons.

wa_lvc_cat-ref_field = c_rbunit.

wa_lvc_cat-ref_table = c_ecmca.

append wa_lvc_cat to it_lvc_cat.

*--- account description

wa_lvc_cat-fieldname = c_acctd.

wa_lvc_cat-ref_field = c_txtmi.

wa_lvc_cat-ref_table = c_tf101.

append wa_lvc_cat to it_lvc_cat.

2. For variable fields you have your own logic based on which you need to display the output. (like loop at one table and make each field as one column). Here one thing need to make sure every time field name should be different. You know in one structure there are 2 fields with same name is not allowed.

3. next use the below method to create the internal table

  • Create a new Table

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = it_lvc_cat

importing

ep_table = it_new_table.

  • Create a new Line with the same structure of the table.

assign it_new_table->* to <l_table>. " internal table

create data wa_new_line like line of <l_table>.

assign wa_new_line->* to <l_line>. " Work area

4. To populate the field catalog for ALV use the same sequence. Better while you populated it_lvc_cat for dynamic table also create one lookup table with three fields. (fieldname, Text, col_pos). Here loop into this look up table and populate the ALV field catalog.

5. Display it using REUSE_ALV_GRID_DISPLAY

call function 'REUSE_ALV_GRID_DISPLAY'

exporting

i_callback_program = c_rep_name

i_callback_user_command = 'F_USER_DOUBLE_CLICK' "SD0K963313

is_layout = l_wa_layout

it_fieldcat = it_fieldcat

it_sort = it_sort

i_save = l_save

is_variant = wa_variant1

it_events = it_events

tables

t_outtab = <l_table>

exceptions

program_error = 1

others = 2.

Thanks

Subhankar

Read only

Former Member
0 Likes
2,254

hi thanks very useful info

but have look at my final internal table equipment matnr desciption and some fields r fixed in the table i will handle them as u told

but for variable fields they will be like this this is the final table

1.matnr ,desc, reading1, uom1,date1, reading 2, uom2 ,date 2,reading 3, uom3 ,date 3-istrecord

2.matnr ,desc, reading1, uom1,date1, reading 2, uom2 -2ndtrecord

3.matnr ,desc, reading1, uom1,date1, reading 2, uom2 -,reading 3, uom3 ,date 3 3ndtrecord

it goes on here reading ,uom and date fields varu for each record

that is my problem

when i tried some ways it goes dump especially when i try to assign the final itab to the created field symbol

i need this part so that next i can happily assign that populated field symbol to the grid

but i feel ur logic is good can u pls send declarations as well for the field symbols as well

thanks in advance

Read only

Former Member
0 Likes
2,254

Hi Revanth,

After generating your dynamic internal table you can use the following logic to get the components of the dynamic internal table.


data: lrw_data  TYPE REF TO data.
data: lr_struct   TYPE REF TO cl_abap_structdescr.
data: lt_comp    TYPE abap_compdescr_tab.
data: lw_comp  TYPE abap_compdescr.

field-symbols: <lw_data> TYPE any.

CREATE DATA lrw_data LIKE LINE OF <YOUR_TABLE_NAME>.
ASSIGN lrw_data->* TO <lw_data>.

lr_struct ?= cl_abap_typedescr=>describe_by_data( <lw_data> ).
lt_comp = lr_struct->components[].

Now the LT_COMP will have the names of all the components in your internal table. You can build your field catalog based on these information.

Hope this will help.

Regards,

Immanuel

Edited by: Immanuel T A on Dec 8, 2010 7:23 AM

Read only

Former Member
0 Likes
2,254

hi i created a structure in se11 and used field catalog merge to get the titles

finally in the out put iam getting as expected all the values but iam not getting the descriptions.iam only getting the first 2

fields descriptions.

  • * Create a new Table

call method cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = gt_fieldcat

IMPORTING

ep_table = new_table.

  • Create a new Line with the same structure of the table.

assign new_table->* to <fs_table>.

create data new_line like line of <fs_table>.

assign new_line->* to <fs_struc>.

refresh gt_fieldcat1.

clear ls_fieldcat1.

loop at gt_fieldcat into is_fieldcat.

move-corresponding is_fieldcat to ls_fieldcat1.

.

ls_fieldcat1-fieldname = is_fieldcat-fieldname.

ls_fieldcat1-intlen = is_fieldcat-intlen .

ls_fieldcat1-datatype = is_fieldcat-datatype .

ls_fieldcat1-seltext_l = is_fieldcat-scrtext_l.

ls_fieldcat1-seltext_m = is_fieldcat-scrtext_m.

ls_fieldcat1-seltext_s = is_fieldcat-scrtext_s.

append ls_fieldcat1 to gt_fieldcat1.

clear ls_fieldcat1.