REPORT z_assign_component.
TYPE-POOLS :
slis.
DATA :
it_table TYPE REF TO data,
st_line TYPE REF TO data,
it_keys TYPE TABLE OF dfies,
st_keys TYPE dfies,
it_fcat TYPE TABLE OF lvc_s_fcat,
st_fcat TYPE lvc_s_fcat,
dy_table TYPE REF TO data,
dy_line TYPE REF TO data
.
* Field-simbols
FIELD-SYMBOLS :
‘<fs_table> TYPE STANDARD TABLE,
‘<fs_line>,
‘<dyn_table> TYPE STANDARD TABLE,
‘<dyn_line>,
‘<dyn_field>.
START-OF-SELECTION.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS :
p_table TYPE dd02l-tabname OBLIGATORY,
p_rows TYPE i,
p_where TYPE char100
.
SELECTION-SCREEN END OF BLOCK b1.
PERFORM : z_select_dynamic,
z_get_keys,
z_create_dynamic_table,
z_feed_dynamic_table,
z_alv
.
*&---------------------------------------------------------------------*TYPE-POOLS :
slis.
DATA :
it_table TYPE REF TO data,
st_line TYPE REF TO data,
it_keys TYPE TABLE OF dfies,
st_keys TYPE dfies,
it_fcat TYPE TABLE OF lvc_s_fcat,
st_fcat TYPE lvc_s_fcat,
dy_table TYPE REF TO data,
dy_line TYPE REF TO data
.
* Field-simbols
FIELD-SYMBOLS :
‘<fs_table> TYPE STANDARD TABLE,
‘<fs_line>,
‘<dyn_table> TYPE STANDARD TABLE,
‘<dyn_line>,
‘<dyn_field>.
START-OF-SELECTION.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS :
p_table TYPE dd02l-tabname OBLIGATORY,
p_rows TYPE i,
p_where TYPE char100
.
SELECTION-SCREEN END OF BLOCK b1.
PERFORM : z_select_dynamic,
z_get_keys,
z_create_dynamic_table,
z_feed_dynamic_table,
z_alv
.
*& Form Z_SELECT_DYNAMIC
*&---------------------------------------------------------------------*
FORM z_select_dynamic .
TRANSLATE p_table TO UPPER CASE.
CREATE DATA it_table TYPE TABLE OF (p_table).
ASSIGN it_table->* TO '<fs_table>.
SELECT *
FROM (p_table)
UP TO p_rows ROWS
INTO CORRESPONDING FIELDS OF TABLE ‘<fs_table>
WHERE (p_where).
CREATE DATA st_line LIKE LINE OF ‘<fs_table>.
ASSIGN st_line->* TO ‘<fs_line>.
ENDFORM. " Z_SELECT_DYNAMIC
*&---------------------------------------------------------------------*
*& Form Z_GET_KEYS
*&---------------------------------------------------------------------*
FORM z_get_keys .
CALL FUNCTION 'GET_KEY_FIELDS_OF_TABLE'
EXPORTING
tabname = p_table
* mandt_needed =
TABLES
key_fieldtab = it_keys
EXCEPTIONS
not_supported = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM. " Z_GET_KEYS
*&---------------------------------------------------------------------*
*& Form Z_CREATE_DYNAMIC_TABLE
*&---------------------------------------------------------------------*
FORM z_create_dynamic_table .
BREAK-POINT.
LOOP AT it_keys INTO st_keys.
MOVE-CORRESPONDING st_keys TO st_fcat.
APPEND st_fcat TO it_fcat.
ENDLOOP.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = it_fcat
i_length_in_byte = 'X'
IMPORTING
ep_table = dy_table.
ASSIGN dy_table->* TO '<dyn_table>.
CREATE DATA dy_line LIKE LINE OF '<dyn_table>.
ASSIGN dy_line->* TO '<dyn_line>.
ENDFORM. " Z_CREATE_DYNAMIC_TABLE
*&---------------------------------------------------------------------*
*& Form Z_FEED_DYNAMIC_TABLE
*&---------------------------------------------------------------------*
FORM z_feed_dynamic_table .
DATA :
vl_contador TYPE i,
vl_lines TYPE i,
vl_field(50) TYPE c.
FIELD-SYMBOLS :
<field>
.
BREAK-POINT.
DESCRIBE TABLE it_keys LINES vl_lines.
LOOP AT <fs_table> INTO <fs_line>.
CLEAR vl_contador.
DO vl_lines TIMES.
vl_contador = vl_contador + 1.
READ TABLE it_keys INTO st_keys
INDEX vl_contador.
ASSIGN COMPONENT st_keys-fieldname
OF STRUCTURE '<fs_line> TO '<dyn_field>.
CONCATENATE '<dyn_line>-' st_keys-fieldname INTO vl_field.
ASSIGN (vl_field) TO '<field>.
MOVE '<dyn_field> TO '<field>.
CLEAR vl_field.
ENDDO.
APPEND '<dyn_line> TO '<dyn_table>.
ENDLOOP.
ENDFORM. " Z_FEED_DYNAMIC_TABLE
*&---------------------------------------------------------------------*
*& Form Z_ALV
*&---------------------------------------------------------------------*
FORM z_alv .
DATA :
st_fieldcat TYPE slis_fieldcat_alv,
it_fieldcat TYPE TABLE OF slis_fieldcat_alv,
st_layout TYPE slis_layout_alv,
vl_flag TYPE i.
BREAK-POINT.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = sy-repid
i_structure_name = p_table
i_client_never_display = 'X'
CHANGING
ct_fieldcat = 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.
LOOP AT it_fieldcat INTO st_fieldcat.
vl_flag = sy-tabix.
READ TABLE it_keys INTO st_keys
WITH KEY fieldname = st_fieldcat-fieldname.
IF sy-subrc IS NOT INITIAL.
DELETE it_fieldcat INDEX vl_flag.
ENDIF.
ENDLOOP.
* Layout
st_layout-zebra = 'X'.
st_layout-colwidth_optimize = 'X'.
* ALV
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
is_layout = st_layout
it_fieldcat = it_fieldcat
TABLES
t_outtab = ‘<dyn_table>
EXCEPTIONS
program_error = 1
OTHERS = 2
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM. " Z_ALV
Serkan AKKAVAK
Computer Engineer
serkurumsal@yandex.com
Social Plugin