Generate a Dynamic Internal Table and Display it in ALV (Program Sample)
Requirements:
ABAP Codes:
*&---------------------------------------------------------------------*
*& Report ZDI_DINTAB2
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zdi_dintab2.
*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
TABLES: sflight, s002.
DATA: t_sflight TYPE TABLE OF sflight.
DATA: v_last_day(2).
* Dynamic Table Declarations
DATA : t_table TYPE REF TO data,
w_line TYPE REF TO data,
w_line1 TYPE REF TO data,
w_fcat TYPE lvc_s_fcat,
t_fcat TYPE lvc_t_fcat.
* Field Symbols Declarations
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
<dyn_line> TYPE any,
<dyn_line1> TYPE any,
<fs1> TYPE any.
*&---------------------------------------------------------------------*
*& Seletion Screens
*&---------------------------------------------------------------------*
PARAMETERS: p_carrid TYPE sflight-carrid DEFAULT 'AZ'.
PARAMETERS: p_spmon TYPE s002-spmon DEFAULT '202403'. "sy-datum(6).
SELECT-OPTIONS s_connid FOR sflight-connid.
SELECT-OPTIONS s_fldate FOR sflight-fldate NO-DISPLAY.
*&---------------------------------------------------------------------*
*& PAI
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN.
PERFORM pai.
*&---------------------------------------------------------------------*
*& Start of Selection
*&---------------------------------------------------------------------*
START-OF-SELECTION.
PERFORM get_data.
PERFORM create_dyn_tab.
*&---------------------------------------------------------------------*
*& End of Selection
*&---------------------------------------------------------------------*
END-OF-SELECTION.
PERFORM dispaly_alv.
*&---------------------------------------------------------------------*
*& Form pai
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM pai .
DATA : lv_pos TYPE i,
lv_day(2) TYPE n,
lv_fname TYPE string.
IF sy-ucomm = 'ONLI'.
APPEND INITIAL LINE TO s_fldate ASSIGNING FIELD-SYMBOL(<ls_fldate>).
<ls_fldate>-sign = 'I'.
<ls_fldate>-option = 'BT'.
<ls_fldate>-low = p_spmon && '01'.
CALL FUNCTION 'RE_LAST_DAY_OF_MONTH'
EXPORTING
i_datum = <ls_fldate>-low
IMPORTING
e_tt = v_last_day.
<ls_fldate>-high = p_spmon && v_last_day.
"Build the columns: CARRID, CONNID, DAY01, DAY02, DAY03, etc
CLEAR w_fcat.
ADD 1 TO lv_pos.
w_fcat-fieldname = 'CARRID'.
w_fcat-outputlen = 7.
w_fcat-tabname = 'IT_SEATS'.
w_fcat-coltext = 'Airline'(001).
w_fcat-col_pos = lv_pos.
w_fcat-key = 'X'.
w_fcat-key_sel = 'X'.
APPEND w_fcat TO t_fcat.
CLEAR w_fcat.
ADD 1 TO lv_pos.
w_fcat-fieldname = 'CONNID'.
w_fcat-outputlen = 4.
w_fcat-tabname = 'IT_SEATS'.
w_fcat-coltext = 'Connect'(002).
w_fcat-col_pos = lv_pos.
w_fcat-key = 'X'.
w_fcat-key_sel = 'X'.
APPEND w_fcat TO t_fcat.
DO v_last_day TIMES.
CLEAR w_fcat.
ADD 1 TO: lv_pos, lv_day.
lv_fname = |DAY| && lv_day.
READ TABLE t_fcat INTO w_fcat WITH KEY fieldname = lv_fname.
IF sy-subrc NE 0.
w_fcat-fieldname = lv_fname.
w_fcat-tabname = 'IT_SEATS'.
w_fcat-coltext = lv_day.
w_fcat-col_pos = lv_pos.
w_fcat-outputlen = 8.
APPEND w_fcat TO t_fcat.
ENDIF.
ENDDO.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form get_data
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM get_data .
DATA: lw_new TYPE sflight.
SELECT * FROM sflight
INTO TABLE t_sflight
WHERE carrid EQ p_carrid
AND connid IN s_connid
AND fldate IN s_fldate.
IF sy-subrc IS NOT INITIAL.
MESSAGE e398(00) WITH 'Data not found'(e01) '' '' ''.
ELSE.
"Add more data
READ TABLE t_sflight INTO DATA(lw_sfl) INDEX 1.
lw_new = lw_sfl.
lw_new-connid = '0017'.
lw_new-fldate = p_spmon && |01|. lw_new-seatsocc = 180.
APPEND lw_new TO t_sflight.
lw_new-fldate = p_spmon && |03|. lw_new-seatsocc = 355.
APPEND lw_new TO t_sflight.
lw_new-fldate = p_spmon && |08|. lw_new-seatsocc = 222.
APPEND lw_new TO t_sflight.
lw_new-fldate = p_spmon && |12|. lw_new-seatsocc = 213.
APPEND lw_new TO t_sflight.
lw_new-connid = '0064'.
lw_new-fldate = p_spmon && |02|. lw_new-seatsocc = 301.
APPEND lw_new TO t_sflight.
lw_new-fldate = p_spmon && |08|. lw_new-seatsocc = 335.
APPEND lw_new TO t_sflight.
lw_new-fldate = p_spmon && |19|. lw_new-seatsocc = 89.
APPEND lw_new TO t_sflight.
lw_new-connid = '0055'.
lw_new-fldate = p_spmon && |20|. lw_new-seatsocc = 189.
APPEND lw_new TO t_sflight.
lw_new-connid = '0108'.
lw_new-fldate = p_spmon && |04|. lw_new-seatsocc = 370.
APPEND lw_new TO t_sflight.
lw_new-fldate = p_spmon && |31|. lw_new-seatsocc = 323.
APPEND lw_new TO t_sflight.
lw_new-connid = '0300'.
lw_new-fldate = p_spmon && |15|. lw_new-seatsocc = 300.
APPEND lw_new TO t_sflight.
lw_new-fldate = p_spmon && |16|. lw_new-seatsocc = 299.
APPEND lw_new TO t_sflight.
SORT t_sflight.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form create_dyn_tab
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM create_dyn_tab .
DATA lv_fldate TYPE sy-datum.
"Create a dynamic internal table with this structure.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
i_style_table = 'X'
it_fieldcatalog = t_fcat
IMPORTING
ep_table = t_table
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
IF sy-subrc EQ 0.
"Assign the new table to field symbol
ASSIGN t_table->* TO <dyn_table>.
"Create dynamic work area for the dynamic table
CREATE DATA w_line LIKE LINE OF <dyn_table>. ASSIGN w_line->* TO <dyn_line>.
CREATE DATA w_line1 LIKE LINE OF <dyn_table>. ASSIGN w_line1->* TO <dyn_line1>.
"Populate the dynamic table
LOOP AT t_sflight INTO DATA(lw_sflight).
"Avoid duplicate entries for key field PART.
READ TABLE <dyn_table> INTO <dyn_line1>
WITH KEY ('CARRID') = lw_sflight-carrid
('CONNID') = lw_sflight-connid.
IF sy-subrc = 0.
CONTINUE.
ENDIF.
"Airline Code
ASSIGN COMPONENT 'CARRID' OF STRUCTURE <dyn_line> TO <fs1>.
CHECK <fs1> IS ASSIGNED.
<fs1> = lw_sflight-carrid.
UNASSIGN <fs1>.
"Flight Connection
ASSIGN COMPONENT 'CONNID' OF STRUCTURE <dyn_line> TO <fs1>.
CHECK <fs1> IS ASSIGNED.
<fs1> = lw_sflight-connid.
UNASSIGN <fs1>.
LOOP AT t_fcat INTO w_fcat.
IF w_fcat-fieldname = 'CARRID' OR
w_fcat-fieldname = 'CONNID'.
CONTINUE.
ENDIF.
TRY.
lv_fldate = p_spmon && w_fcat-coltext.
DATA(lw_sflight1) = t_sflight[ carrid = lw_sflight-carrid
connid = lw_sflight-connid
fldate = lv_fldate ].
ASSIGN COMPONENT w_fcat-fieldname OF STRUCTURE <dyn_line> TO <fs1>.
IF <fs1> IS ASSIGNED.
<fs1> = lw_sflight1-seatsocc.
UNASSIGN <fs1>.
ENDIF.
CATCH cx_sy_itab_line_not_found.
"next data
ENDTRY.
CLEAR : lw_sflight1.
ENDLOOP.
APPEND <dyn_line> TO <dyn_table>.
CLEAR: <dyn_line>.
CLEAR: lw_sflight, lw_sflight1.
ENDLOOP.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form dispaly_alv
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
FORM dispaly_alv .
DATA : lw_alv_fieldcat TYPE slis_fieldcat_alv,
lt_alv_fieldcat TYPE slis_t_fieldcat_alv,
lw_layout TYPE slis_layout_alv,
lv_pos TYPE i.
lw_layout-colwidth_optimize =
lw_layout-zebra = abap_true.
LOOP AT t_fcat INTO w_fcat.
lv_pos = lv_pos + 1.
lw_alv_fieldcat-fieldname = w_fcat-fieldname.
lw_alv_fieldcat-tabname = w_fcat-tabname.
lw_alv_fieldcat-seltext_l = w_fcat-coltext.
lw_alv_fieldcat-outputlen = w_fcat-outputlen.
lw_alv_fieldcat-col_pos = lv_pos.
lw_alv_fieldcat-do_sum = w_fcat-do_sum.
lw_alv_fieldcat-emphasize = w_fcat-emphasize.
lw_alv_fieldcat-key = w_fcat-key.
lw_alv_fieldcat-no_out = w_fcat-no_out.
APPEND lw_alv_fieldcat TO lt_alv_fieldcat.
ENDLOOP.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
it_fieldcat = lt_alv_fieldcat
is_layout = lw_layout
i_default = 'X'
i_save = 'A'
TABLES
t_outtab = <dyn_table>.
ENDFORM.
No comments :
Post a Comment