Search on this Website

Thursday, June 28, 2007

Object Oriented ALV-Using two Containers we can display data

*&---------------------------------------------------------------------*
*& Report Z_OO_ALV
*&
*&---------------------------------------------------------------------*
*& We can Use Two containers in OOALV
*&
*&---------------------------------------------------------------------*

REPORT z_oo_alv LINE-COUNT 50.

*types gt_struct type sflight.

DATA BEGIN OF gt_struct.
INCLUDE STRUCTURE sflight.
DATA rcol(4) TYPE c.
DATA colors TYPE lvc_t_scol.
DATA END OF gt_struct.

*ALV GRIDs

DATA gr_alvgrid TYPE REF TO cl_gui_alv_grid.
DATA gr_alvgrid1 TYPE REF TO cl_gui_alv_grid.

DATA gc_custom_control_name TYPE scrfname VALUE 'CC_ALV'.
DATA gc_custom_control_name1 TYPE scrfname VALUE 'CC_ALV1'.

*CONTAINERs

DATA gr_ccontainer TYPE REF TO cl_gui_custom_container.
DATA gr_ccontainer1 TYPE REF TO cl_gui_custom_container.

*FIELDCATALOGs

DATA gt_fieldcat TYPE lvc_t_fcat WITH HEADER LINE.
DATA gt_fieldcat1 TYPE lvc_t_fcat WITH HEADER LINE.

*LAYOUTs

DATA gs_layout TYPE lvc_s_layo.
DATA gs_layout1 TYPE lvc_s_layo.

DATA pt_exclude TYPE ui_functions. "internal table declaration to be passed.
*DATA pt_cell TYPE lvc_t_cell with header line.



DATA : gt_list LIKE gt_struct OCCURS 50 WITH HEADER LINE,
gt_list1 LIKE gt_struct OCCURS 50 WITH HEADER LINE.

*DATA v_ucomm TYPE sy-ucomm.

CALL SCREEN 100.


*&---------------------------------------------------------------------*
*& Module display_alv OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE display_alv OUTPUT.

PERFORM display_alv.

ENDMODULE. " display_alv OUTPUT

*&---------------------------------------------------------------------*
*& Module PAI INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE pai INPUT.
CASE sy-ucomm.

WHEN 'EXIT'.
PERFORM exit_program.
WHEN 'PICK'.
PERFORM cell_info.

ENDCASE.

ENDMODULE. " PAI INPUT


*&---------------------------------------------------------------------*
*& Form display_alv
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM display_alv.
PERFORM prepare_field_catalog CHANGING gt_fieldcat[].
PERFORM prepare_layout CHANGING gs_layout.
PERFORM data_retrival.

IF gr_alvgrid IS INITIAL.

CREATE OBJECT gr_ccontainer

EXPORTING

container_name = gc_custom_control_name

EXCEPTIONS

cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5
OTHERS = 6.

IF sy-subrc <> 0.

ENDIF.

CREATE OBJECT gr_alvgrid
EXPORTING
* I_SHELLSTYLE = 0
* I_LIFETIME =
i_parent = gr_ccontainer
* I_APPL_EVENTS = space
* I_PARENTDBG =
* I_APPLOGPARENT =
* I_GRAPHICSPARENT =
* I_NAME =
* I_FCAT_COMPLETE = SPACE
EXCEPTIONS
error_cntl_create = 1
error_cntl_init = 2
error_cntl_link = 3
error_dp_create = 4
OTHERS = 5
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

PERFORM exclude_tb_functions CHANGING pt_exclude.
PERFORM set_col.

CALL METHOD gr_alvgrid->set_table_for_first_display
EXPORTING
* I_BUFFER_ACTIVE =
* I_BYPASSING_BUFFER =
* I_CONSISTENCY_CHECK =
* I_STRUCTURE_NAME =
* IS_VARIANT =
* I_SAVE =
* I_DEFAULT = 'X'
is_layout = gs_layout
* IS_PRINT =
* IT_SPECIAL_GROUPS =
it_toolbar_excluding = pt_exclude "excluding toolbar functions
* IT_HYPERLINK =
* IT_ALV_GRAPHICS =
* IT_EXCEPT_QINFO =
* IR_SALV_ADAPTER =
CHANGING
it_outtab = gt_list[]
it_fieldcatalog = gt_fieldcat[]
* IT_SORT =
* IT_FILTER =
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.



ELSE.

CALL METHOD gr_alvgrid->refresh_table_display
* EXPORTING
* IS_STABLE =
* I_SOFT_REFRESH =
EXCEPTIONS
finished = 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.
ENDIF.


PERFORM prepare_field_catalog1 CHANGING gt_fieldcat1[].
PERFORM prepare_layout1 CHANGING gs_layout1.
PERFORM data_retrival1.


IF gr_alvgrid1 IS INITIAL.

CREATE OBJECT gr_ccontainer1

EXPORTING

container_name = gc_custom_control_name1

EXCEPTIONS

cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5
OTHERS = 6.

IF sy-subrc <> 0.

ENDIF.

CREATE OBJECT gr_alvgrid1
EXPORTING
* I_SHELLSTYLE = 0
* I_LIFETIME =
i_parent = gr_ccontainer1
* I_APPL_EVENTS = space
* I_PARENTDBG =
* I_APPLOGPARENT =
* I_GRAPHICSPARENT =
* I_NAME =
* I_FCAT_COMPLETE = SPACE
EXCEPTIONS
error_cntl_create = 1
error_cntl_init = 2
error_cntl_link = 3
error_dp_create = 4
OTHERS = 5
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

PERFORM set_col1.

CALL METHOD gr_alvgrid1->set_table_for_first_display
EXPORTING
* I_BUFFER_ACTIVE =
* I_BYPASSING_BUFFER =
* I_CONSISTENCY_CHECK =
* I_STRUCTURE_NAME =
* IS_VARIANT =
* I_SAVE =
* I_DEFAULT = 'X'
is_layout = gs_layout1
* IS_PRINT =
* IT_SPECIAL_GROUPS =
* IT_TOOLBAR_EXCLUDING =
* IT_HYPERLINK =
* IT_ALV_GRAPHICS =
* IT_EXCEPT_QINFO =
* IR_SALV_ADAPTER =
CHANGING
it_outtab = gt_list1[]
it_fieldcatalog = gt_fieldcat1[]
* IT_SORT =
* IT_FILTER =
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.



ELSE.

CALL METHOD gr_alvgrid1->refresh_table_display
* EXPORTING
* IS_STABLE =
* I_SOFT_REFRESH =
EXCEPTIONS
finished = 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.
ENDIF.

ENDFORM. "display_alv


*&---------------------------------------------------------------------*
*& Form prepare_field_catalog
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->GT_FIELDCAT text
*----------------------------------------------------------------------*
FORM prepare_field_catalog CHANGING pgt_fieldcat TYPE lvc_t_fcat.
DATA ls_fieldcat TYPE lvc_s_fcat.
ls_fieldcat-tabname = 'gt_list'.
ls_fieldcat-fieldname = 'CARRID'.
ls_fieldcat-scrtext_m = 'Air line code'.
ls_fieldcat-col_pos = 0.
ls_fieldcat-outputlen = 10.
* ls_fieldcat-emphasize = 'C400'.
* ls_fieldcat-key = 'X'.
APPEND ls_fieldcat TO pgt_fieldcat.

ls_fieldcat-tabname = 'gt_list'.
ls_fieldcat-col_pos = 1.
ls_fieldcat-fieldname = 'CONNID'.
ls_fieldcat-scrtext_m = 'Connection code'.
* ls_fieldcat-emphasize = 'C900'.
APPEND ls_fieldcat TO pgt_fieldcat.

ls_fieldcat-tabname = 'gt_list'.
ls_fieldcat-fieldname = 'PRICE'.
ls_fieldcat-scrtext_m = 'PRICE'.
APPEND ls_fieldcat TO pgt_fieldcat.

ENDFORM. "prepare_field_catalog


*&---------------------------------------------------------------------*
*& Form prepare_layout
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->GS_LAYOUT text
*----------------------------------------------------------------------*
FORM prepare_layout CHANGING gs_layout TYPE lvc_s_layo.

gs_layout-stylefname = 'FIELD_STYLE'.
gs_layout-zebra = 'X'.
gs_layout-grid_title = 'FLIGHT'.
gs_layout-sel_mode = 'A'.
gs_layout-ctab_fname = 'COLORS'.

ENDFORM. "prepare_layout

*&---------------------------------------------------------------------*
*& Form data_retrival
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM data_retrival.
SELECT carrid
connid
price
FROM sflight
INTO CORRESPONDING FIELDS OF TABLE gt_list
UP TO 50 ROWS.
ENDFORM. "data_retrival


*---------------------------------------------------------------------*
* FORM EXIT_PROGRAM *
*---------------------------------------------------------------------*
FORM exit_program.
CALL METHOD gr_ccontainer->free.
CALL METHOD gr_ccontainer1->free.
LEAVE TO SCREEN 0.
ENDFORM. "exit_program
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS 'STAT'.
* SET TITLEBAR 'xxx'.

* IF W_CUSTOM_CONTAINER IS INITIAL.
*
**sets TITLEBAR
* PERFORM TITLEBAR.

ENDMODULE. " STATUS_0100 OUTPUT


*&---------------------------------------------------------------------*
*& Form prepare_field_catalog1
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->GT_FIELDCAT text
*----------------------------------------------------------------------*
FORM prepare_field_catalog1 CHANGING pgt_fieldcat1 TYPE lvc_t_fcat.
DATA ls_fieldcat TYPE lvc_s_fcat.
ls_fieldcat-tabname = 'gt_list1'.
ls_fieldcat-fieldname = 'SEATSMAX'.
ls_fieldcat-scrtext_m = 'MAX. SEATS'.
ls_fieldcat-col_pos = 0.
ls_fieldcat-outputlen = 10.
* ls_fieldcat-emphasize = 'C400'.
* ls_fieldcat-key = ' '.
APPEND ls_fieldcat TO pgt_fieldcat1.

ls_fieldcat-tabname = 'gt_list1'.
ls_fieldcat-col_pos = 1.
ls_fieldcat-fieldname = 'SEATSOCC'.
ls_fieldcat-scrtext_m = 'SEATS OCCUPIED'.
APPEND ls_fieldcat TO pgt_fieldcat1.

ENDFORM. "prepare_field_catalog


*&---------------------------------------------------------------------*
*& Form prepare_layout1
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->GS_LAYOUT text
*----------------------------------------------------------------------*
FORM prepare_layout1 CHANGING gs_layout1 TYPE lvc_s_layo.

gs_layout1-stylefname = 'FIELD_STYLE'.
gs_layout1-zebra = 'X'.
gs_layout1-grid_title = 'DETAILS'.
* gs_layout-sel_mode = 'C'.
gs_layout1-info_fname = 'RCOL'.
* gs_layout-no_toolbar = 'X'.

ENDFORM. "prepare_layout

*&---------------------------------------------------------------------*
*& Form data_retrival1
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM data_retrival1.
SELECT seatsmax
seatsocc
FROM sflight
INTO CORRESPONDING FIELDS OF TABLE gt_list1
UP TO 50 ROWS.
ENDFORM. "data_retrival

*&---------------------------------------------------------------------*
*& Form exclude_tb_functions
*&---- subroutine to exclude toolbar options --------------------------*
* text
*----------------------------------------------------------------------*
* -->PT_EXCLUDE text
*----------------------------------------------------------------------*
FORM exclude_tb_functions CHANGING pt_exclude TYPE ui_functions.

DATA ls_exclude TYPE ui_func.

ls_exclude = cl_gui_alv_grid=>mc_fc_maximum.
APPEND ls_exclude TO pt_exclude.

ls_exclude = cl_gui_alv_grid=>mc_fc_minimum.
APPEND ls_exclude TO pt_exclude.

ls_exclude = cl_gui_alv_grid=>mc_fc_subtot.
APPEND ls_exclude TO pt_exclude.

ls_exclude = cl_gui_alv_grid=>mc_fc_sort.
APPEND ls_exclude TO pt_exclude.

ls_exclude = cl_gui_alv_grid=>mc_fc_sum.
APPEND ls_exclude TO pt_exclude.

ls_exclude = cl_gui_alv_grid=>mc_mb_subtot.
APPEND ls_exclude TO pt_exclude.

ls_exclude = cl_gui_alv_grid=>mc_mb_sum.
APPEND ls_exclude TO pt_exclude.

ls_exclude = cl_gui_alv_grid=>mc_mb_filter.
APPEND ls_exclude TO pt_exclude.

ENDFORM. "data_retrival1

*&---------------------------------------------------------------------*
*& Form cell_info
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM cell_info. "CHANGING pt_cell TYPE lvc_t_cell.
DATA lt_cell TYPE lvc_t_cell WITH HEADER LINE.
CALL METHOD gr_alvgrid->get_selected_cells
IMPORTING
et_cell = lt_cell[].
LOOP AT lt_cell.
WRITE : lt_cell-col_id , lt_cell-row_id.
ENDLOOP.
* MODIFY pt_cell[] from lt_cell[].
ENDFORM. "cell_info

*&---------------------------------------------------------------------*
*& Form set_col
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM set_col .

DATA ls_cellcolor TYPE lvc_s_scol.

LOOP AT gt_list.
IF gt_list-price GT 500.
ls_cellcolor-fname = 'PRICE'.
ls_cellcolor-color-col = 5.
ls_cellcolor-color-int = 1.
ls_cellcolor-color-inv = 0.
APPEND ls_cellcolor TO gt_list-colors.
else.
ls_cellcolor-fname = 'PRICE'.
ls_cellcolor-color-col = 3.
ls_cellcolor-color-int = 1.
APPEND ls_cellcolor TO gt_list-colors.

ENDIF.
MODIFY gt_list.
ENDLOOP.

ENDFORM. "set_col

*&---------------------------------------------------------------------*
*& Form set_col1
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM set_col1.
data : ind type sy-tabix,
indx type sy-tabix.
loop at gt_list1.
ind = sy-tabix / 2.
indx = sy-tabix - ind.
if indx eq ind.
gt_list1-rcol = 'C500'.
endif.
MODIFY gt_list1.
endloop.

ENDFORM. "set_col

*FORM TITLEBAR.
*SET TITLEBAR 'TITLE'.
*ENDFORM.

*double click on TITLE and write ur title


Designed By Srikanth Vadlamani

Other Useful Link :
Object Oriented ALV-Sample program to insert Logo in ALV
ALV Function Module
Customize ALV grid layout at run time
Download ALV grid Control Tutorial
Understand ALV report ( Just Copy and paste )
Dynamic selection on ALV at run time
Dynamic selection on ALV at run time

No comments: