11-15-2013 11:48 AM
Hi,
I'm creating dynamic columns using field symbols & dynamic internal table.
In SELECTION-SCREEN when i enter the range (for eg: 4 to 😎 in FISCAL PERIOD, i do not get the output in order. The order i get is : 05 04 08 06 07 in the columns.
I'm finding it difficult to sort dynamic internal table.
I'm a fresher. Can anyone help me with explanations?
Please refer my code & let me know where i need to make changes --
*&---------------------------------------------------------------------*
*& Report ZSID_ALV_REPORT5
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZSID_ALV_REPORT5.
TABLES: BSID.
TYPE-POOLS: SLIS.
TYPES: BEGIN OF TY_BSID,
bukrs TYPE bsid-bukrs,
kunnr TYPE bsid-kunnr,
monat TYPE bsid-monat,
END OF TY_BSID.
DATA: IT_BSID TYPE TABLE OF TY_BSID,
WA_BSID TYPE TY_BSID,
WA_BSID2 TYPE TY_BSID.
DATA : gv_pos TYPE i.
DATA : fname TYPE string.
* Dynamic Table Declarations
DATA : gt_dyn_table TYPE REF TO data,
gw_line TYPE REF TO data,
gw_line1 TYPE REF TO data,
gw_dyn_fcat TYPE lvc_s_fcat,
gt_dyn_fcat TYPE lvc_t_fcat.
* Field Symbols Declarations
FIELD-SYMBOLS: <gfs_line>,<gfs_line1>,
<gfs_dyn_table> TYPE STANDARD TABLE,
<fs1>.
data : gw_alv_fieldcat type slis_fieldcat_alv,
gt_alv_fieldcat type slis_t_fieldcat_alv,
lv_pos type I.
SELECTION-SCREEN: BEGIN OF BLOCK A WITH FRAME TITLE S.
SELECT-OPTIONS : PERIOD FOR BSID-MONAT.
SELECTION-SCREEN: END OF BLOCK A.
INITIALIZATION.
S = 'ENTER FISCAL PERIOD'.
START-OF-SELECTION.
PERFORM SELECT.
PERFORM DYNAMIC.
*&---------------------------------------------------------------------*
*& Form SELECT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form SELECT .
SELECT bukrs
kunnr
monat FROM BSID INTO CORRESPONDING FIELDS OF TABLE IT_BSID
WHERE MONAT IN PERIOD.
endform. " SELECT
*&---------------------------------------------------------------------*
*& Form DYNAMIC
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form DYNAMIC .
gv_pos = gv_pos + 1.
gw_dyn_fcat-fieldname = 'BUKRS'.
gw_dyn_fcat-outputlen = 5.
gw_dyn_fcat-tabname = 'IT_BSID'.
gw_dyn_fcat-coltext = 'COMPANY CODE'.
gw_dyn_fcat-col_pos = gv_pos.
gw_dyn_fcat-key = 'X'.
gw_dyn_fcat-key_sel = 'X'.
APPEND gw_dyn_fcat TO gt_dyn_fcat.
clear gw_dyn_fcat.
"Loop through the internal table creating a column for every distinct month in the internal table
LOOP AT it_BSID INTO wa_BSID.
gv_pos = gv_pos + 1.
CONCATENATE WA_BSID-MONAT ' ' INTO fname.
read table gt_dyn_fcat into gw_dyn_fcat with key fieldname = wa_BSID-MONAT.
if sy-subrc NE 0.
gw_dyn_fcat-fieldname = wa_BSID-MONAT.
gw_dyn_fcat-tabname = 'IT_BSID'.
gw_dyn_fcat-coltext = fname.
gw_dyn_fcat-outputlen = 10.
gw_dyn_fcat-col_pos = gv_pos.
APPEND gw_dyn_fcat TO gt_dyn_fcat.
endif.
clear gw_dyn_fcat.
ENDLOOP.
** Create a dynamic internal table with this structure.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
i_style_table = 'X'
it_fieldcatalog = gt_dyn_fcat
IMPORTING
ep_table = gt_dyn_table
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2.
IF sy-subrc EQ 0.
* Assign the new table to field symbol
ASSIGN gt_dyn_table->* TO <gfs_dyn_table>.
* Create dynamic work area for the dynamic table
CREATE DATA gw_line LIKE LINE OF <gfs_dyn_table>.
CREATE DATA gw_line1 LIKE LINE OF <gfs_dyn_table>.
ASSIGN gw_line->* TO <gfs_line>.
ASSIGN gw_line1->* TO <gfs_line1>.
ENDIF.
* Populate the dynamic table
LOOP AT it_BSID INTO wa_BSID.
"Avoid duplicate entries for key field PART.
READ TABLE <gfs_dyn_table> INTO <gfs_line1> WITH KEY ('BUKRS') = wa_BSID-BUKRS.
IF sy-subrc = 0.
CONTINUE.
ENDIF.
ASSIGN COMPONENT 'BUKRS' OF STRUCTURE <gfs_line> TO <fs1>.
<fs1> = wa_BSID-BUKRS.
UNASSIGN <fs1>.
LOOP AT gt_dyn_fcat INTO gw_dyn_fcat.
IF gw_dyn_fcat-fieldname = 'BUKRS'.
CONTINUE.
ENDIF.
READ TABLE it_BSID WITH KEY BUKRS = wa_BSID-BUKRS MONAT = gw_dyn_fcat-fieldname INTO wa_BSID2.
IF sy-subrc = 0.
ASSIGN COMPONENT gw_dyn_fcat-fieldname OF STRUCTURE <gfs_line> TO <fs1>.
<fs1> = wa_BSID2-KUNNR.
UNASSIGN <fs1>.
ENDIF.
clear : wa_BSID2.
ENDLOOP.
APPEND <gfs_line> TO <gfs_dyn_table>.
CLEAR: <gfs_line>.
clear: wa_BSID, wa_BSID2.
ENDLOOP.
loop at gt_dyn_fcat into gw_dyn_fcat.
lv_pos = lv_pos + 1.
gw_alv_fieldcat-fieldname = gw_dyn_fcat-fieldname.
gw_alv_fieldcat-tabname = gw_dyn_fcat-tabname.
gw_alv_fieldcat-seltext_l = gw_dyn_fcat-coltext.
gw_alv_fieldcat-outputlen = gw_dyn_fcat-outputlen.
gw_alv_fieldcat-col_pos = lv_pos.
gw_alv_fieldcat-do_sum = gw_dyn_fcat-do_sum.
gw_alv_fieldcat-emphasize = gw_dyn_fcat-emphasize.
gw_alv_fieldcat-key = gw_dyn_fcat-key.
gw_alv_fieldcat-no_out = gw_dyn_fcat-no_out.
append gw_alv_fieldcat to gt_alv_fieldcat.
endloop.
call function 'REUSE_ALV_GRID_DISPLAY'
exporting
i_callback_program = sy-repid
it_fieldcat = gt_alv_fieldcat
i_default = 'X'
i_save = 'A'
tables
t_outtab = <gfs_dyn_table>.
endform. " DYNAMIC
11-15-2013 12:09 PM
Hi Sidharth,
Just do this:
gv_pos = gv_pos + 1.
gw_dyn_fcat-fieldname = 'BUKRS'.
gw_dyn_fcat-outputlen = 5.
gw_dyn_fcat-tabname = 'IT_BSID'.
gw_dyn_fcat-coltext = 'COMPANY CODE'.
"gw_dyn_fcat-col_pos = gv_pos.
gw_dyn_fcat-key = 'X'.
gw_dyn_fcat-key_sel = 'X'.
APPEND gw_dyn_fcat TO gt_dyn_fcat.
CLEAR gw_dyn_fcat.
"Loop through the internal table creating a column for every
"distinct month in the internal table
SORT it_bsid BY monat.
LOOP AT it_bsid INTO wa_bsid.
gv_pos = gv_pos + 1.
CONCATENATE wa_bsid-monat ' ' INTO fname.
READ TABLE gt_dyn_fcat INTO gw_dyn_fcat
WITH KEY fieldname = wa_bsid-monat.
IF sy-subrc NE 0.
gw_dyn_fcat-fieldname = wa_bsid-monat.
gw_dyn_fcat-tabname = 'IT_BSID'.
gw_dyn_fcat-coltext = fname.
gw_dyn_fcat-outputlen = 10.
"gw_dyn_fcat-col_pos = gv_pos.
APPEND gw_dyn_fcat TO gt_dyn_fcat.
ENDIF.
CLEAR gw_dyn_fcat.
ENDLOOP.
Sort it_bsid by month before creating fieldcatalog and than you don't need column positions..
Regards,
Vladimir
11-15-2013 12:09 PM
Hi Sidharth,
Just do this:
gv_pos = gv_pos + 1.
gw_dyn_fcat-fieldname = 'BUKRS'.
gw_dyn_fcat-outputlen = 5.
gw_dyn_fcat-tabname = 'IT_BSID'.
gw_dyn_fcat-coltext = 'COMPANY CODE'.
"gw_dyn_fcat-col_pos = gv_pos.
gw_dyn_fcat-key = 'X'.
gw_dyn_fcat-key_sel = 'X'.
APPEND gw_dyn_fcat TO gt_dyn_fcat.
CLEAR gw_dyn_fcat.
"Loop through the internal table creating a column for every
"distinct month in the internal table
SORT it_bsid BY monat.
LOOP AT it_bsid INTO wa_bsid.
gv_pos = gv_pos + 1.
CONCATENATE wa_bsid-monat ' ' INTO fname.
READ TABLE gt_dyn_fcat INTO gw_dyn_fcat
WITH KEY fieldname = wa_bsid-monat.
IF sy-subrc NE 0.
gw_dyn_fcat-fieldname = wa_bsid-monat.
gw_dyn_fcat-tabname = 'IT_BSID'.
gw_dyn_fcat-coltext = fname.
gw_dyn_fcat-outputlen = 10.
"gw_dyn_fcat-col_pos = gv_pos.
APPEND gw_dyn_fcat TO gt_dyn_fcat.
ENDIF.
CLEAR gw_dyn_fcat.
ENDLOOP.
Sort it_bsid by month before creating fieldcatalog and than you don't need column positions..
Regards,
Vladimir
11-16-2013 5:04 AM
11-16-2013 5:49 AM
Hi,
I was just wondering what is your maximum expected columns ?
Regards .