Showing posts with label Dynamic Internal Table. Show all posts
Showing posts with label Dynamic Internal Table. Show all posts

How to get Internal Table Components at runtime

This code can be helpful to get fields names of an Internal Table at Runtime.

The Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
*&---------------------------------------------------------------------*
*& Report ZTEST_INTERNAL_TAB_COMP
*&
*&---------------------------------------------------------------------*
*&***********DEMO Code for http://abap-explorer.blogspot.com/***********
*&
*&---------------------------------------------------------------------*

report ztest_internal_tab_comp line-size 100.
type-pools: abap.

*Declare the type of the internal table
types: begin of x_final,
matnr type matnr,
werks type werks_d,
flag type c length 1,
value type p length 10 decimals 2,
end of x_final.

data:
*The Internal table whose components are to found
i_data type sorted table of x_final with unique key matnr,
*Table to hold the components
tab_return type abap_compdescr_tab,
*Work area for the component table
components like line of tab_return.

*Call Perform to get the Int. Table Components
perform get_int_table_fields using i_data
changing tab_return.
*Display Components
loop at tab_return into components.
write: / components-name, components-type_kind,
components-length,components-decimals.
endloop.




*&---------------------------------------------------------------------*
*& Form get_int_table_fields
*&---------------------------------------------------------------------*
* Get the Components of an internal table
*----------------------------------------------------------------------*
* -->T_DATA text
* -->T_RETURN text
*----------------------------------------------------------------------*
form get_int_table_fields using t_data type any table
changing t_return type abap_compdescr_tab.

data:
oref_table type ref to cl_abap_tabledescr,
oref_struc type ref to cl_abap_structdescr,
oref_error type ref to cx_root,
text type string.
*Get the description of data object type
try.
oref_table ?=
cl_abap_tabledescr=>describe_by_data( t_data ).
catch cx_root into oref_error.
text = oref_error->get_text( ).
write: / text.
exit.
endtry.
*Get the line type
try.
oref_struc ?= oref_table->get_table_line_type( ).
catch cx_root into oref_error.
text = oref_error->get_text( ).
write: / text.
exit.
endtry.

append lines of oref_struc->components to t_return.

endform. " GET_INT_TABLE_FIELDS


Now if we test it...


The code can also be used to get components of a structure.

Dynamic table display using CL_SALV_TABLE

Here is a simple code to display any table data using ALV using class CL_SALV_TABLE (factory method)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
*&---------------------------------------------------------------------*
*& Report ZSB_DYNAMIC_ALV
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

report zsb_dynamic_alv.
field-symbols:
<
f_tab1> type standard table.

parameters:
p_tname type tabname16 obligatory, " DEFAULT 'MARA' ,
p_rows(5) type c default '200'.
*----------------------------------------------------------------------*
* CLASS lcl_dynamic DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_dynamic definition .
public section.
methods:
check_selection
exceptions invalid_table,
main
exceptions no_data_found,
display.

private section.

type-pools: abap.

data: tab type ref to cl_abap_structdescr,
wa_tab type ref to cl_abap_structdescr,
comp_tab type cl_abap_structdescr=>component_table,
i_tab type ref to cl_abap_tabledescr,
i_table type ref to data.
endclass. "lcl_dynamic DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_dynamic IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_dynamic implementation.
method check_selection.

select count( * )
from dd02l
where tabname = p_tname
and as4local = 'A'
and tabclass = 'TRANSP'.

if sy-subrc <> 0.
raise invalid_table.
endif.
endmethod. "check_selection

method main.
tab ?= cl_abap_typedescr=>describe_by_name( p_tname ).

comp_tab = tab->get_components( ).

wa_tab = cl_abap_structdescr=>create( comp_tab ).

i_tab = cl_abap_tabledescr=>create( wa_tab ).

create data i_table type handle i_tab.

assign i_table->* to <f_tab1>.

if p_rows is initial.

p_rows = '50000'.

endif.

*Get data

select * from (p_tname)
into table <f_tab1>
up to p_rows rows.
if sy-subrc <> 0.
raise no_data_found.
endif.

endmethod. "main

method display.

set titlebar sy-title
of program sy-cprog
with 'Display table:' p_tname.

data:

l_gr_alv type ref to cl_salv_table,
l_gr_functions type ref to cl_salv_functions.

try.

call method cl_salv_table=>factory
importing
r_salv_table = l_gr_alv
changing
t_table = <f_tab1>.
catch cx_salv_msg . "#EC NO_HANDLER

endtry.

l_gr_functions = l_gr_alv->get_functions( ).
l_gr_functions->set_all( abap_true ).
l_gr_alv->display( ).

endmethod. "display

endclass. "lcl_dynamic IMPLEMENTATION

at selection-screen.


data oref_check type ref to lcl_dynamic.
create object oref_check.
call method oref_check->check_selection
exceptions
invalid_table = 1.

if sy-subrc <> 0.
message e001(00) with
p_tname ' is not a Transparant Table'.
endif.

start-of-selection.
data oref_main type ref to lcl_dynamic.
create object oref_main.
call method oref_main->main
exceptions
no_data_found = 1.
if sy-subrc <> 0.
message i001(00) with 'No data found'.
leave list-processing.
endif.

end-of-selection.

call method oref_main->display.