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: 

Field name on ALV Grid

blee1
Explorer
0 Kudos

Using cl_salv_table to display a custom built internal table, do you know of a way to switch between field name and field label, similar to how SE16 does it. Basically show the column names of the of the internal table as header fields in the ALV grid.

1 ACCEPTED SOLUTION

Sandra_Rossi
Active Contributor

for one column:

data(columns) = yoursalv->get_columns( ).
data(column) = columns->get_column( 'NAME' ).
column->set_medium_text( 'Heading' ).
column->set_long_text( 'Heading' ).

to iterate on all columns:

data(columns) = yoursalv->get_columns( ).
data(itab_columns) = columns->get( ).
LOOP AT itab_columns INTO DATA(ls_column).
  ...
ENDLOOP.
12 REPLIES 12

Sandra_Rossi
Active Contributor
0 Kudos

to change the column heading, yoursalv->get_columns( ), set_medium_text, set_long_text, etc.

blee1
Explorer
0 Kudos

Hi Sandra,

Do you know of a way to iterate through the columns returned by get_columns()? This way we do not have to do it one by one and be susceptible for future code changes.

Sandra_Rossi
Active Contributor
0 Kudos

Please use the COMMENT button for comments, questions, adding details, etc., ANSWER is only to propose a solution, dixit SAP text at the right of the answer area: "Before answering You should only submit an answer when you are proposing a solution to the poster's problem"

0 Kudos

Benson, I think you are going to be stuck looping over the structure of the itab one field at a time. The ALV pulls the column name from either the itab column name based on the structure of the itab or by manually defining those labels one-by-one as in Sandra's code snippet. The "field label" in SE11 that you are using as an example is defined in the Dictionary at the data element level.

Just free-thinking, one way you might approach it would be to loop over the itab structure definition assigning a field-symbol for the column name and then looking up the data element label in DD04L. But I've only used that technique to get the data from a cell of the itab, not the technical name of the column, so that may not be feasible either.

I am not sure the ALV would know how to drill-down to the data element. And how would it know which data element label was to be used (short, medium, long or heading)? On top of that, because the itab field definitions could be totally unrelated to any Dictionary data element, how would it handle a generic column defined as type CHAR20 from the Dictionary or a column that was manually typed as char(20) in the itab definition?

I am interested to see what you come up with. Keep us informed here, please.

Sandra_Rossi
Active Contributor

for one column:

data(columns) = yoursalv->get_columns( ).
data(column) = columns->get_column( 'NAME' ).
column->set_medium_text( 'Heading' ).
column->set_long_text( 'Heading' ).

to iterate on all columns:

data(columns) = yoursalv->get_columns( ).
data(itab_columns) = columns->get( ).
LOOP AT itab_columns INTO DATA(ls_column).
  ...
ENDLOOP.

0 Kudos

Hi Sandra,

On line #2 of your code, it requires that you know the column name. Do we have a way to do it in such a way that it will iterate through all the columns rather than having to do it one by one?

0 Kudos

Answer updated.

0 Kudos

Hi Sandra,

This is exactly I was looking for. I made the code changes based on your suggestions and I got exactly what I needed. Many thanks for your help!

former_member1716
Active Contributor
0 Kudos

Can you please help us to understand your question better?

are you asking a similar functionlity as we have se11 where we can switch between column names and field names?

Or

Or do you want to display desired field naane while displaying ALV?

if it's the latter you wanted then follow the instructions as suggested by Sandra Rossi.

If it's the first option then you have to follow different approach.

Kindly clarify the same.

blee1
Explorer
0 Kudos

Hi Satish,

It would be the first option, but to do it for all columns rather than just one column at a time.

Sandra_Rossi
Active Contributor
0 Kudos

Please use the COMMENT button for comments, questions, adding details, etc., ANSWER is only to propose a solution, dixit SAP text at the right of the answer area: "Before answering You should only submit an answer when you are proposing a solution to the poster's problem"

SimoneMilesi
Active Contributor
0 Kudos

Given your comment on Sandra's solution, I suggest you to investigate class CL_SALV_COLUMNS_TABLE, the object returned by CL_SALV_TABLE=>GET_COLUMNS: it has a lovely method GET returning a table with all the columns names and the referenced object (table SALV_T_COLUMN_REF with structure SALV_S_COLUMN_REF)