‎2007 Sep 24 8:00 AM
I have created a dynamic internla table and was able to puplulate data init but could not do it in a way i wanted ...
Please have a look at the code and read the rest of the doubt
REPORT yms_dynamicalv.
TABLES : mara .
TYPE-POOLS: slis.
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
<dyn_wa>.
FIELD-SYMBOLS: <dyn_table2> TYPE STANDARD TABLE,
<dyn_wa2>.
DATA : BEGIN OF itab OCCURS 0 , "Based on the values in this tabel dynamic
mtart LIKE mara-mtart , "table coulmns will be generated
END OF itab .
DATA : BEGIN OF itab2 OCCURS 0 , "This is a normal internal table from which
hai(5) TYPE c, " i have to populate data into the dynamic
field2(5) TYPE c, "internal table
mtart LIKE mara-mtart ,
matnr LIKE mara-matnr ,
END OF itab2 .
DATA: alv_fldcat TYPE slis_t_fieldcat_alv,
it_fldcat TYPE lvc_t_fcat,
lin TYPE i .
DATA: new_table TYPE REF TO data,
new_line TYPE REF TO data,
wa_it_fldcat TYPE lvc_s_fcat.
*For the purpose of simplicity i have assigned default values of Seelct options
SELECT-OPTIONS: s_mtart FOR mara-mtart DEFAULT 'FERT' TO 'HALB' .
START-OF-SELECTION.
SELECT
mtart
FROM mara INTO CORRESPONDING FIELDS OF
TABLE itab
WHERE mtart IN s_mtart .
DELETE ADJACENT DUPLICATES FROM itab.
*This select enables dynamic values in itab but commented for simplicity in fixing
*the problem instead appended 6 rows of data
SELECT
matnr
mtart
FROM mara INTO CORRESPONDING FIELDS OF
TABLE itab2
WHERE mtart IN s_mtart .
itab2-hai = 'FIRST'.
itab2-field2 = 'A'.
itab2-mtart = 'FERT'.
itab2-matnr = 'FF000001' .
APPEND itab2.
itab2-hai = 'FIRST'.
itab2-field2 = 'A'.
itab2-mtart = 'HALB'.
itab2-matnr = 'FH000001' .
APPEND itab2.
itab2-hai = 'SEC'.
itab2-field2 = 'A'.
itab2-mtart = 'FERT'.
itab2-matnr = 'SF000002' .
APPEND itab2.
itab2-hai = 'SEC'.
itab2-field2 = 'A'.
itab2-mtart = 'HALB'.
itab2-matnr = 'SH000002'.
APPEND itab2.
itab2-hai = 'THIR'.
itab2-field2 = 'A'.
itab2-mtart = 'HALB'.
itab2-matnr = 'TH000003'.
APPEND itab2.
itab2-hai = 'THIR'.
itab2-field2 = 'A'.
itab2-mtart = 'FGTR'.
itab2-matnr = 'TFG000003 '.
APPEND itab2.
build the dynamic internal table
PERFORM build_dyn_itab.
*Passing values into dynamic intrenal table from normal internal table
PERFORM build_report.
call the alv grid.
PERFORM call_alv.
************************************************************************
Build_dyn_itab
************************************************************************
FORM build_dyn_itab.
CLEAR wa_it_fldcat.
wa_it_fldcat-fieldname = 'HAI'.
wa_it_fldcat-seltext = 'HAI'.
wa_it_fldcat-outputlen = '5'.
APPEND wa_it_fldcat TO it_fldcat.
CLEAR wa_it_fldcat.
wa_it_fldcat-fieldname = 'field2'.
wa_it_fldcat-seltext = 'field2'.
wa_it_fldcat-outputlen = '5'.
APPEND wa_it_fldcat TO it_fldcat.
SORT itab BY mtart .
LOOP AT itab .
CLEAR wa_it_fldcat.
wa_it_fldcat-tabname = itab.
wa_it_fldcat-fieldname = itab-mtart.
wa_it_fldcat-datatype = itab2-matnr.
wa_it_fldcat-intlen = 18.
APPEND wa_it_fldcat TO it_fldcat .
ENDLOOP.
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. "build_dyn_itab
*********************************************************************
Form build_report
*********************************************************************
FORM build_report.
DATA: fieldname(20) TYPE c.
DATA: fieldvalue(5) TYPE c.
DATA: index(3) TYPE c.
FIELD-SYMBOLS: <fs1> TYPE mara-matnr,<fs>."ANY TABLE .
ASSIGN COMPONENT OF STRUCTURE <dyn_wa> TO <fs1>.
LOOP AT itab2.
CLEAR: <dyn_wa>.
LOOP AT it_fldcat INTO wa_it_fldcat.
ASSIGN COMPONENT sy-tabix OF STRUCTURE <dyn_wa> TO <fs> .
IF sy-tabix = 1 ."AND itab2-hai = wa_it_fldcat-fieldname.
<fs> = itab2-hai .
ELSEIF sy-tabix = 2.
<fs> = itab2-field2.
ELSEIF sy-tabix = 3 AND itab2-mtart = wa_it_fldcat-fieldname.
<fs> = itab2-matnr .
ELSEIF sy-tabix = 4 AND itab2-mtart = wa_it_fldcat-fieldname.
<fs> = itab2-matnr .
ELSEIF sy-tabix = 5 AND itab2-mtart = wa_it_fldcat-fieldname.
<fs> = itab2-matnr .
ENDIF.
ENDLOOP.
APPEND <dyn_wa> TO <dyn_table>.
ENDLOOP.
ASSIGN <dyn_table> TO <dyn_table2>.
ENDFORM. "build_report
************************************************************************
CALL_ALV
************************************************************************
FORM call_alv.
DATA: wa_cat LIKE LINE OF alv_fldcat,
rp TYPE i VALUE 0 .
CLEAR wa_cat.
wa_cat-fieldname = 'HAI'.
wa_cat-seltext_s = 'HAI'.
wa_cat-outputlen = '5'.
APPEND wa_cat TO alv_fldcat.
CLEAR wa_cat.
wa_cat-fieldname = 'field2'.
wa_cat-seltext_s = 'field2'.
wa_cat-outputlen = '5'.
APPEND wa_cat TO alv_fldcat.
DO lin TIMES.
LOOP AT itab .
CLEAR wa_cat.
wa_cat-tabname = itab.
wa_cat-fieldname = itab-mtart.
wa_cat-seltext_s = itab-mtart.
wa_cat-outputlen = '20'.
APPEND wa_cat TO alv_fldcat.
ENDLOOP.
CLEAR wa_cat.
wa_cat-fieldname = 'HAI2'.
wa_cat-seltext_s = 'HAI2'.
wa_cat-outputlen = '5'.
APPEND wa_cat TO alv_fldcat.
CLEAR wa_cat.
wa_cat-fieldname = 'HAI3'.
wa_cat-seltext_s = 'HAI3'.
wa_cat-outputlen = '5'.
APPEND wa_cat TO alv_fldcat.
CLEAR wa_cat.
Call ABAP List Viewer (ALV)
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
it_fieldcat = alv_fldcat
TABLES
t_outtab = <dyn_table>.
ENDFORM. "call_alv
The above code populates data as follows
HAI | field2 | FERT | FGTR | HALB | HAI2 | HAI3
______________________________________________________________
FIRST FIRST FF000001
FIRST FIRST FH000001 FH000001 FH000001
SEC SEC SF000002
SEC SEC SH000002 SH000002 SH000002
THIR THIR TH000003 TH000003 TH000003
THIR THIR TFG000003
But the Out put required should be as follows
HAI | field2 | FERT | FGTR | HALB | HAI2 | HAI3
____________________________________________________________________
FIRST | FIRST |FF000001 | |FH000001
SEC |SEC |SF000002 | |SH000002
THIR |THIR | |TFG000003 |H000003
The layout may be bit confusing to put simply ..FF00001--means First Field's FERT material no similarly SH00002 means second fields HALB material number the materila numbers should come under their sorresponding material type .
Hope my question is clear .....
For the sake of simplicity i have inserted 6 record into the internal table from which iam passing into a DYNAMIC INTERNAL TABLE ..
This code has no dependencies hence try it in ABAP editor if necessary and pelase give me solution ..
Thanking you inadvance .
Suriya .
‎2007 Sep 24 8:22 AM
hi Suriya,
I am not sure if anyone will do your job (i. e. debugging your own program), but few things are definetly wrong:
wa_it_fldcat-tabname = itab.
Here you assign the whole table (with values) instead of the name of the table. On the other hand, you have to assign a tablename from the Data Dictionary, you cannot do with internal table name.
wa_it_fldcat-fieldname = itab-mtart.
Again, you assign a value (like 'FERT'), instead of assigning the name of the field (which you cannot do this way).
wa_it_fldcat-datatype = itab2-matnr.
Again...
Pls. correct these first and come back if you have more questions.
ec