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: 

How to create ALV fieldcatalog dynamically.

Former Member
0 Kudos

Hallo guys,

I need to create ALV report where fieldcatalog depends on content of data dictionary table.

In table ZXY I have column ZCOL with values VAL1, VAL2, VAL3... . This values I need to get

into columns of ALV. Is there any way how to do it?

According to this layout I have to get also data into internal table and display them in ALV.

Thanks.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

You can achieve this using Field symbols.

Thanks and Regards,

Lakshmi.

17 REPLIES 17

Former Member
0 Kudos

Hi,

You can achieve this using Field symbols.

Thanks and Regards,

Lakshmi.

0 Kudos

VAL* are the values of one table column which I need to bring into ALV as I column names.

Edited by: Marek Ferencak on Apr 18, 2008 8:59 AM

Edited by: Marek Ferencak on Apr 18, 2008 9:01 AM

0 Kudos

You need a bit more that just the columns names, you also need to know the type of the field, like date, time, character, numeric. Have a look at this:

http://help.sap.com/saphelp_470/helpdata/en/52/5f060de02d11d2b47d006094192fe3/frameset.htm

and than you can use a Method cl_alv_table_create=>create_dynamic_table

Former Member
0 Kudos

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

EXPORTING

i_structure_name = str_report

CHANGING

ct_fieldcat = p_it_fieldcat

EXCEPTIONS

inconsistent_interface = 1

program_error = 2

OTHERS = 3.

IF sy-subrc 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

str_report is your structure or table name and the p_it_fieldcat is the fieldcat table.

define both variables or you can directly pass the structure or table.

Sm1tje
Active Contributor
0 Kudos

Let me see if I understand:

In table ZXY you have a column with VAL1, VAL2, VAL3, etc. These fields, are those values, or are that the names of the COLUMNS?

Former Member
0 Kudos

Hi Marek,

Try this..

If you are using

LVC_T_FCAT table type as your fieldcatalog

data : begin of i_col occurs 0,

zcol type ZXY-ZCOL,

end of i_col.

DATA : l_colname(20) type c.

data : i_fcat TYPE lvc_t_fcat,

DATA : wa_fcat LIKE LINE OF i_fcat.

select zcol into table i_col from ZXY where.....<your condition>.

loop at i_col

l_colname = i_col-zcol.

condense l_colname.

wa_fcat-fieldname = L_COLNAME

wa_fcat-tabname = 'ZXY'.

.....<other constant parameters for field catalog.>

append wa_fcat to i_fcat

endloop.

regards,

Mohaiyuddin

0 Kudos

Realized one mistake.

Instead of using this,

define internal table with all types with VAL* and pass

data : begin of i_ref_tab occurs 0,

VAL* TYPE <type of val>, (hope field referenced by VAL is defined as data element)

....

end of i_ref_tab.

wa_fcat-fieldname = L_COLNAME

wa_fcat-tabname = 'I_REF_TAB'

0 Kudos

.....<other constant parameters for field catalog.>

What other 'constants'. You need way more than just this. For example is it a character field or numeric. Let's say it is character, you still need to know its length.....etc. etc.

0 Kudos

Hi Mohaiyuddin,

thanks, this could work but how do I dynamically create an internal table with data in this case?

Looks like this itab has to be created dynamically as well...

How do I define it?

0 Kudos

Hi Marek,

Define internal table will all values available ZCOL of table ZXY, (hope ZXY will not get updated frequently).

Use only those parameters selected from ZXY to define your field catalog.

Regards,

Mohaiyuddin

0 Kudos

As soon as you have your fieldcatalog (look at the link I gave you) ready, which will be difficult when you have not defined any dataelements for you columns VAL1, 2,3 etc. you can use the method I mentioned. It will give you the internal table based on your fieldcatalog.

By the way, it won't matter if your table changes oftenly, because if you do it RIGHT (meaning dynamically) you will have no problems what so ever in the future.

Edited by: Micky Oestreich on Apr 18, 2008 9:23 AM

0 Kudos

Hmm, what I have from select statement is an itab with VAL1, VAL2, VAL3 ...

As you described I can create fieldcatalog using this values. But how do I create an itab for data which has to be displayed in ALV. Let say ALV stucrure is like C1 C2 C3 VAL1 VAL2 VAL3 C4 C5.

How to create itab like this using DATA clause?

0 Kudos

Thanks Micky, I will try to use your way.

0 Kudos

Interesting...

If C1,C2,C3...others are constant and VAL1, VAL2 are variables, you can defined internal table will all fields.

itab

C1...

C2..

C3...

VAL1..

VAL2..

VAL3.

VAL4......VALn

Cn..

Define field catalog for all field mentioned in itab.

Depending upon VAL1, VAL2...selected from ZXY, use field NO_OUT of field catalog to hide it from output for those VAL* filed not selected in your select query....

You may try EDIT_MAST, but i haven't used it till now..

Moreover, if your ZXY is updated often, I would suggest to go for dynamic creation of itab - mentioned by Micky..

Regards,

Mohaiyuddin

0 Kudos

Hi Micky,

I just think how to create fieldcatalog when I basically don't know how many columns ALV will have...?

Should I use loop statement like described higher?

0 Kudos

So, how is your requirement coming? I wasn't watching this thread, but I am now. Did you make any progress yet?

As to your question for the number of columns.

If you do it right it won't matter how many columns you have, as long as you define them correctly in your ZXY table. But like I already said, just the names of the columns won't do you any good, you als need (mandatory) type datatype.

And then indeed, select all the data from your table into an internal table and do a loop. For every single column you have to create a fieldcatalog. In this fieldcatalog you give the name of your column (or column header text actually), datatype, keyfield, etc. etc.

Before you proceed, let me give you an example.

contents table ZXY:

MANDT(client) COLUMN TYPE LENGTH

100 VAL1 CHAR 10

100 VAL2 DATS 8

100 VAL3 NUMC 5

Select data into internal table itab (type table of ZXY).

loop at itab into wa.

fieldcat-tablename = 'ZXY'.

fieldcat-fieldname = wa-column.

fieldcat-datatype = wa-type.

fieldcat-length = wa-length.

fieldcat-seltext_l = wa-column (this is the actual text which will be displayed on ALV column heading).

fieldcat-seltext_m = wa-column (this is the actual text which will be displayed on ALV column heading).

fieldcat-seltext_s = wa-column (this is the actual text which will be displayed on ALV column heading).

etc.

difference between seltext is the length of the output, Long, Medium, Short text. But if you only enter one, that will work as well.

P.S. I'm not sure about the fieldnames from FIELDCAT but should be something like it. Also have a look at the link I send you for manually creating fieldcatalog.

With this fieldcatalog you can use the method create_dynamic_table.

Good luck with that.

Edited by: Micky Oestreich on Apr 18, 2008 4:05 PM

Former Member
0 Kudos

Hi Marek,

DATA : wa_fcat LIKE LINE OF i_fcat.

select zcol into table i_col from ZXY where.....<your condition>.

loop at i_col

l_colname = i_col-zcol.

condense l_colname.

wa_fcat-fieldname = L_COLNAME

wa_fcat-tabname = 'ZXY'.

.....<other constant parameters for field catalog.>

append wa_fcat to i_fcat

endloop.

Reward If Helpfull,

Naresh.