2009 May 16 3:46 AM
Hi,
I have got stuck at one place. I want to declare fields dynmically in the internal table based on the content of another table.
For eg.
internal table1 - it1 contains 5 rows.(No.of rows can change)
then i want to declare 5 cloumns i.e fields in another internal table it2.
Thanks,
Mohammadi
2009 May 16 7:47 AM
Hi,
data: begin of it1 occurs 0,
fieldname type string,
...
end of it1.
data it_fieldcat TYPE lvc_t_fcat with header line.
data: new_table TYPE REF TO data.
data: new_line TYPE REF TO data.
field-symbols: <new_tab> type any table,
<new_line> type any.
Loop at it1.
"create fied catalog as you would create one for alv
it_fieldcat-fieldname = it1-fieldname. "create field of content of IT1-fieldname
....
"either set type explicitly or use reference to some DDIC field
it_fieldcat-INTTYPE = ".....your custom type
"or
it_fieldcat-ref_field = "...referenced field in DDIC
it_fieldcat-ref_table = "...and a table
append it_fieldcat.
endloop.
"create dynamic table
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fieldcat[]
IMPORTING
ep_table = new_table.
"now retrieve table from reference pointing to it
assign new_table->* to <new_tab>.
create data new_line like line of <new_tab>.
assign new_line>* to <new_line>.
"now <new_tab> is your dynamic table and <new_line> is a workarea to work with that table
"you can i.e. do like
Loop at <new_tab> assigning <new_line>.
"work here witl <new_line>
endloop.
Regards
Marcin
Hi,
I have got stuck at one place. I want to declare fields dynmically in the internal table based on the content of another table.
For eg.
internal table1 - it1 contains 5 rows.(No.of rows can change)
then i want to declare 5 cloumns i.e fields in another internal table it2.
Thanks,
Mohammadi
2009 May 16 5:22 AM
Hi,
By describe statement you will get the no of rows of the internal table.
Then according to that no you need to create the internal table using FIELD-SYMBOLS.
DATA : w_line TYPE I.
DESCRIBE TABLE <internal_table> LINES w_line.Here w_line will contain the no of rows of the internal table.
Now use the FIELD-SYMBOLS and the method CREATE_DYNAMIC_TABLE
of the class CL_ALV_TABLE_CREATE .
You can check the link to get the fields of internal table dynamically -
[https://wiki.sdn.sap.com/wiki/display/Snippets/Howtocreateinternaltable+dynamically]
Regards
Pinaki
2009 May 16 7:24 AM
2009 May 16 7:47 AM
Hi,
data: begin of it1 occurs 0,
fieldname type string,
...
end of it1.
data it_fieldcat TYPE lvc_t_fcat with header line.
data: new_table TYPE REF TO data.
data: new_line TYPE REF TO data.
field-symbols: <new_tab> type any table,
<new_line> type any.
Loop at it1.
"create fied catalog as you would create one for alv
it_fieldcat-fieldname = it1-fieldname. "create field of content of IT1-fieldname
....
"either set type explicitly or use reference to some DDIC field
it_fieldcat-INTTYPE = ".....your custom type
"or
it_fieldcat-ref_field = "...referenced field in DDIC
it_fieldcat-ref_table = "...and a table
append it_fieldcat.
endloop.
"create dynamic table
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fieldcat[]
IMPORTING
ep_table = new_table.
"now retrieve table from reference pointing to it
assign new_table->* to <new_tab>.
create data new_line like line of <new_tab>.
assign new_line>* to <new_line>.
"now <new_tab> is your dynamic table and <new_line> is a workarea to work with that table
"you can i.e. do like
Loop at <new_tab> assigning <new_line>.
"work here witl <new_line>
endloop.
Regards
Marcin
2009 May 18 2:43 AM
Hi,
Using the code provided by you, i was able to create a internal table which is combination of 2 internal table. But now i want to append the data from those internal table to this newly created internal table and pass this internal table to FM- reuse_alv_list_display.
Please help.
2009 May 18 5:43 AM
call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = gt1_fieldcat
importing
ep_table = gt_new_table.
assign gt_new_table->* to <gt_table>.
create data gs_new_line like line of <gt_table>.
assign gs_new_line->* to <gs_line>.
this way u can append the data.
assign component 'NTNAME' of structure <gs_line> to <gd_field>.
this way u can pass the data to ALV
<gt_table> - dyn internal table...
try.
cl_salv_table=>factory(
exporting
list_display = abap_false
importing
r_salv_table = lo_alv
changing
t_table = <gt_table> ).
catch cx_salv_msg .
endtry.
lo_alv->display( ).
2009 May 18 7:06 AM
Hi,
Thanks for the reply. But my requirement is as follows--
I have 2 internal tables say it1 and it2.
it1 have fields A, B and C.
it2 have fields A, D, E.
I have created an internal table it3 dynamically which contains the fields of both the internal table.
Now i want to pass the data from it1 and it2 to it3.
and this it3 to FM for display.
2009 May 18 7:09 AM
LOOP at it1 and find the corresponding record from it2, next do a MOVE-CORRESPONDING from workare1 and workarea2 to <LINE> and append it.
2009 May 18 8:03 AM
Excatly as Micky said:
loop at it1.
read table it2 index sy-tabix. "read corresponding entry in IT2
if sy-subrc = 0.
move-corresponding: it1 to <new_line>,
it2 to <new_line>.
append <new_line> to <new_tab>. "now <new_tab> stores entries from both IT1 and IT2
endif.
endloop.
Regards
Marcin
2009 May 18 8:25 AM
Hi ,
You can check the link I specified in my last post , there it is specified how to
populate dynamically created internal table.
The way to append the data to the created (dynamically) table - -
DATA : W_I TYPE I.
DO 5 TIMES.
W_I = SY-INDEX - 1.
DO 5 TIMES.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE <FS_LINE> TO <FS_FIELD>.
<FS_FIELD> = SY-INDEX + W_I.
ENDDO.
INSERT <FS_LINE> INTO TABLE <FS_TABLE>. " Use to populate the table
ENDDO.Regards
Pinaki
2009 May 18 10:19 AM
Hi ,
I did it , this followed contents will resolve the issue ..
Here it1 table have fld1 , fld2 and fld3 fields and it2 have fld1 , fld4 and fld5.
Now my dynamically create table <FS_TABLE> contains fld1, fld2, fld3, fld4 and fld5.
the structure means the field string is <FS_LINE>.
Check the code -
DATA : BEGIN OF FS1 ,
FLD1(2),
FLD2(2),
FLD3(2),
END OF FS1,
IT1 LIKE TABLE OF FS1,
BEGIN OF FS2 ,
FLD1(2),
FLD4(2),
FLD5(2),
END OF FS2,
IT2 LIKE TABLE OF FS2.
FS1-FLD1 = 1.
FS1-FLD2 = 2.
FS1-FLD3 = 3.
APPEND FS1 TO IT1.
FS1-FLD1 = 6.
FS1-FLD2 = 7.
FS1-FLD3 = 8.
APPEND FS1 TO IT1.
FS2-FLD1 = 1.
FS2-FLD4 = 4.
FS2-FLD5 = 5.
APPEND FS2 TO IT2.
FS2-FLD1 = 6.
FS2-FLD4 = 9.
FS2-FLD5 = 10.
APPEND FS2 TO IT2.
LOOP AT IT1 INTO FS1.
READ TABLE IT2 INTO FS2 with key fld1 = fs1-fld1.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE <FS_LINE> TO <FS_FIELD>.
MOVE-CORRESPONDING FS1 TO <FS_LINE>.
MOVE-CORRESPONDING FS2 TO <FS_LINE>.
INSERT <FS_LINE> INTO TABLE <FS_TABLE>.
ENDLOOP.To display the contents --
LOOP AT <FS_TABLE> INTO <FS_LINE> .
DO 5 TIMES. " times because the table have 5 fields (Fld1, Fld2 ....Fld5)
ASSIGN COMPONENT SY-INDEX OF STRUCTURE <FS_LINE> TO <FS_FIELD>.
WRITE <FS_FIELD>.
ENDDO.
NEW-LINE.
ENDLOOP.Note : As because you got how to create the table dynamically, that is why
I gave the example of populating the table from to internal table.
Regards
Pinaki
2009 May 18 10:44 AM
hi,
Now i am able to create the dynamic internal table and populate it.
I want to display it using FM - reuse_alv_list_display.
For this i am creating fieldcatalog by using FM - reuse_alv_fieldcatalog_merge. In this i am passing i_call back program, i inclname and iinternal table name (dynamically created table)
But my Fieldcatalog is not getting created.
Please help.
2009 May 18 11:09 AM
I'd like to make a remark here:
You obviously not trying to resolve the problem yourself but waiting for excat solution "given you on a plate". Your questions were answered this forum many many times. But apparently you simply can't locate search button on this site. This is not right! You obviously violate Rules and Engagements which should be obligated here. But these you seem not to read ever.
Moreover, people here are to give you a direction how to use some tools, not to use them for you.
And finally I bit of appreciation (by assinng points) to all people sacrifying their time to resolve some basic question and explain it to a newbie is welcome . This would not only encourage people to contribute here further, but also is a matter of good behaviour .
To all people:
Please do not answer here anymore. Reported to moderators.
2009 May 18 11:14 AM
Locked for the reason given by Marcin. Please read and follow the Rules of Engagement.
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |