‎2010 Feb 18 6:25 PM
Is it possible to display header of dynamic table using cl_salv. In my program, I create a dynamic structure based on user input of the table name and display as ALV report.
However the header is missing because the structure does not use DDICT. Is there a way to display header text without any hard coding because of the dynamic structure? Here is my code:
-
REPORT z_dynamic.
Type-pools : abap.
field-symbols: <dyn_table> type standard table,
<dyn_wa>,
<dyn_field>.
data: dy_table type ref to data,
dy_line type ref to data,
xfc type lvc_s_fcat,
ifc type lvc_t_fcat.
data : idetails type abap_compdescr_tab.
data: gr_table type ref to cl_salv_table.
data: gr_functions type ref to cl_salv_functions.
data: lr_text type ref to cl_salv_form_text.
data: lr_label type ref to cl_salv_form_label.
data: lr_grid type ref to cl_salv_form_layout_grid.
parameters: p_table(30) type c default 'ZXX'. "Table name
start-of-selection.
perform get_structure.
perform create_dynamic_itab.
perform get_data.
perform display.
form get_structure.
data:
xdetails type abap_compdescr.
data : ref_table_des type ref to cl_abap_structdescr.
Get the structure of the table.
ref_table_des ?=
cl_abap_typedescr=>describe_by_name( p_table ).
idetails[] = ref_table_des->components[].
loop at idetails into xdetails.
clear xfc.
xfc-fieldname = xdetails-name .
xfc-datatype = xdetails-type_kind.
xfc-inttype = xdetails-type_kind.
xfc-intlen = xdetails-length.
xfc-decimals = xdetails-decimals.
append xfc to ifc.
endloop.
endform.
form create_dynamic_itab.
Create dynamic internal table and assign to FS
call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = ifc
importing
ep_table = dy_table.
assign dy_table->* to <dyn_table>.
Create dynamic work area and assign to FS
create data dy_line like line of <dyn_table>.
assign dy_line->* to <dyn_wa>.
endform. "create_dynamic_itab
form get_data.
Select Data from table.
select * into corresponding fields of table <dyn_table>
from (p_table) up to 10 rows.
endform.
form display.
try.
cl_salv_table=>factory(
exporting list_display = abap_false
importing r_salv_table = gr_table
changing t_table = <dyn_table> ).
catch cx_salv_msg .
endtry.
gr_functions = gr_table->get_functions( ).
gr_functions->set_all( abap_true ).
CREATE OBJECT lr_grid.
lr_label = lr_grid->create_label(
text = 'Table Info'
row = 1
column = 1 ).
lr_text = lr_grid->create_text(
text = '1.2 TEXT'
row = 1
column = 2 ).
lr_label->set_label_for( lr_text ).
lr_text = lr_grid->create_text(
text = '2.2 TEXT'
row = 2
column = 2 ).
gr_table->set_top_of_list( lr_grid ).
gr_table->display( ).
endform.
‎2010 Feb 18 7:59 PM
‎2010 Feb 19 4:17 PM
‎2010 Feb 19 4:23 PM
Loop thru dynamic structure named idetails,
I query against table DD03VT to get the short text, medium text and long text for each field in structure and set colum text.
If someone know a better way please share.
data:
xdetails type abap_compdescr,
xfname type lvc_fname,
xftext type string,
s_text type scrtext_s,
m_text type scrtext_m,
l_text type scrtext_l.
loop at idetails into xdetails.
xfname = xdetails-name .
select single scrtext_s scrtext_m scrtext_l
from dd03vt into
(s_text, m_text, l_text)
where ( tabname = p_table and
fieldname = xfname and
ddlanguage = 'EN' ).
try.
lr_column ?= lr_columns->get_column(
columnname = xfname ).
lr_column->set_short_text( value = s_text ).
lr_column->set_medium_text( value = m_text ).
lr_column->set_long_text( value = l_text ).
catch cx_salv_not_found into gr_error.
endtry.
endloop.