Friday, March 31, 2017

Sample Code : ALV using factory class








Sample Code




*&---------------------------------------------------------------------*
*& Report YTEST220_ALV
*&
*&---------------------------------------------------------------------*
*&
*& By Debesh
*&---------------------------------------------------------------------*

report ytest220_alv.

include ytest220_alv_top. “ At the end
include ytest220_alv_form.

start-of-selection.

select kunnr
land1
name1
name2
ort01
pstlz
regio
sortl
stras
telf1
telfx
xcpdk
adrnr
from kna1 into table gt_kna1 where kunnr in so_kunnr.

if sy-subrc is initial.

if p_r3 is not initial.

call screen 0100.

endif.

endif.















*&---------------------------------------------------------------------*
*& Include YTEST220_ALV_FORM
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module status_0100 output.
set pf-status '0100'.
set titlebar '0100'.

endmodule. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module user_command_0100 input.

case sy-ucomm.
when 'BACK'.
refresh gt_kna1.
submit ytest220_alv via selection-screen.
when 'EXIT'.
leave program.
when others.

endcase.

endmodule. " USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
*& Module populate_alv OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module populate_alv output.

data : lo_container type ref to cl_gui_custom_container,
lo_splitter type ref to cl_gui_splitter_container,
lo_container1 type ref to cl_gui_container,
lo_container2 type ref to cl_gui_container,
lo_header type ref to cl_dd_document,
lo_colref type ref to cl_salv_columns_table,
lo_col type ref to cl_salv_column,
lo_funclist type ref to cl_salv_functions_list,
lo_event type ref to cl_salv_events_table.


if lo_container is initial.
******************************************************Create container.
create object lo_container
exporting
container_name = 'CC_CONTAINER'
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.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

*********************************************************Split Container.
create object lo_splitter
exporting
parent = lo_container
rows = 2
columns = 1
exceptions
cntl_error = 1
cntl_system_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.

************************************************************Get container.
************************************************************ HEADER

call method lo_splitter->get_container
exporting
row = 1
column = 1
receiving
container = lo_container1.


call method lo_splitter->set_row_height
exporting
id = 1
height = 10
exceptions
cntl_error = 1
cntl_system_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.

if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

create object lo_header.

call method lo_header->add_text
exporting
text = text-001
* sap_color = 'red'
* sap_fontsize = '5'
sap_emphasis = 'STRONG'
.

call method lo_header->display_document
exporting
parent = lo_container1
exceptions
html_display_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.


*************************************************************Get container.
************************************************************* Content
call method lo_splitter->get_container
exporting
row = 2
column = 1
receiving
container = lo_container2.
call method lo_splitter->set_row_height
exporting
id = 2
height = 80
exceptions
cntl_error = 1
cntl_system_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.


** Call Factory method to get ALV table reference

try.
call method cl_salv_table=>factory
exporting
list_display = if_salv_c_bool_sap=>false
r_container = lo_container2
importing
r_salv_table = gt_tabref
changing
t_table = gt_kna1.
catch cx_salv_msg .
endtry.

*********************************************************** SET COLUMN VISBILITY
lo_colref = gt_tabref->get_columns( ).
try.
lo_col ?= lo_colref->get_column( 'NAME2' ).
call method lo_col->set_visible
exporting
value = if_salv_c_bool_sap=>false.

catch cx_salv_not_found.

endtry.

********************************************************* To select multiple rows at a time

call method gt_tabref->get_selections
receiving
value = go_sel.
call method go_sel->set_selection_mode
exporting
value = if_salv_c_selection_mode=>row_column.

******************************************************** Get Functions
call method gt_tabref->get_functions
receiving
value = lo_funclist.
lo_funclist->set_all( abap_true ).

try.
call method lo_funclist->add_function
exporting
name = 'CC_DETAILS'
* ICON =
text = 'CC Details'
tooltip = 'Company Code Details'
position = if_salv_c_function_position=>right_of_salv_functions
.
catch cx_salv_existing .
catch cx_salv_wrong_call .
endtry.

call method gt_tabref->get_event
receiving
value = lo_event.
set handler lcl_event_handling=>handle_user_command for all instances.

******************************************************** Display Using alv table reference display table
call method gt_tabref->display.
else.
call method gt_tabref->refresh.
endif.

endmodule. " populate_alv OUTPUT
*&---------------------------------------------------------------------*
*& Form form_get_selected
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form form_get_selected .

data : ls_rows type int4.
data : ls_kna1 type ty_data.
data : lr_kunnr type range of kna1-kunnr,
lrs_kunnr like line of lr_kunnr.

call method gt_tabref->get_metadata.
* FREE go_sel.
call method gt_tabref->get_selections
receiving
value = go_sel.
gt_rows = go_sel->get_selected_rows( ).

loop at gt_rows into ls_rows.

read table gt_kna1 into ls_kna1 index ls_rows.
if sy-subrc is initial.
lrs_kunnr-sign = 'I'.
lrs_kunnr-option = 'EQ'.
lrs_kunnr-low = ls_kna1-kunnr.
append lrs_kunnr to lr_kunnr.
endif.
clear lrs_kunnr.
endloop.

if lr_kunnr is not initial.
select kunnr
bukrs
pernr
erdat
ernam
sperr
loevm
zuawa
busab
akont
from knb1 into table gt_knb1
where kunnr in lr_kunnr.

if sy-subrc is initial.
clear lr_kunnr.
call screen 200.
endif.

endif.


endform. " form_get_selected
*&---------------------------------------------------------------------*
*& Module STATUS_0200 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module status_0200 output.
set pf-status '0200'.
set titlebar '0200'.
perform populate_alv2.
endmodule. " STATUS_0200 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0200 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
module user_command_0200 input.

case sy-ucomm.
when 'BACK'.
refresh gt_knb1.
* IF gt_tabref2 IS BOUND.
* FREE gt_tabref2.
* ENDIF.

leave list-processing.
set screen 100.
when 'EXIT'.
leave program.
when others.

endcase.
endmodule. " USER_COMMAND_0200 INPUT
*&---------------------------------------------------------------------*
*& Form populate_alv2
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form populate_alv2 .

data : lo_containerr type ref to cl_gui_custom_container,
lo_splitter2 type ref to cl_gui_splitter_container,
lo_cont1 type ref to cl_gui_container,
lo_cont2 type ref to cl_gui_container,
lo_head2 type ref to cl_dd_document,
lo_colref2 type ref to cl_salv_columns_table,
lo_col2 type ref to cl_salv_column,
lo_funclist2 type ref to cl_salv_functions_list,
lo_event2 type ref to cl_salv_events_table.

if lo_containerr is initial.
******************************************************Create container.
create object lo_containerr
exporting
container_name = 'CC_CONTAINERR'
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.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

*********************************************************Split Container.
create object lo_splitter2
exporting
parent = lo_containerr
rows = 2
columns = 1
exceptions
cntl_error = 1
cntl_system_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.

************************************************************Get container.
************************************************************ HEADER

call method lo_splitter2->get_container
exporting
row = 1
column = 1
receiving
container = lo_cont1.


call method lo_splitter2->set_row_height
exporting
id = 1
height = 10
exceptions
cntl_error = 1
cntl_system_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.

if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.

create object lo_head2.

call method lo_head2->add_text
exporting
text = text-002
* sap_color = 'red'
* sap_fontsize = '5'
sap_emphasis = 'STRONG'
.

call method lo_head2->display_document
exporting
parent = lo_cont1
exceptions
html_display_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.


*************************************************************Get container.
************************************************************* Content
call method lo_splitter2->get_container
exporting
row = 2
column = 1
receiving
container = lo_cont2.
call method lo_splitter2->set_row_height
exporting
id = 2
height = 80
exceptions
cntl_error = 1
cntl_system_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.


** Call Factory method to get ALV table reference
if gt_tabref2 is not initial.
gt_tabref2->refresh( ).
endif.

try.
call method cl_salv_table=>factory
exporting
list_display = if_salv_c_bool_sap=>false
r_container = lo_cont2
importing
r_salv_table = gt_tabref2
changing
t_table = gt_knb1.
catch cx_salv_msg .
endtry.

*********************************************************** SET COLUMN VISBILITY
lo_colref2 = gt_tabref2->get_columns( ).
try.
lo_col2 ?= lo_colref2->get_column( 'NAME2' ).
call method lo_col2->set_visible
exporting
value = if_salv_c_bool_sap=>false.

catch cx_salv_not_found.

endtry.

********************************************************* To select multiple rows at a time

call method gt_tabref2->get_selections
receiving
value = go_sel2.
call method go_sel2->set_selection_mode
exporting
value = if_salv_c_selection_mode=>row_column.

******************************************************** Get Functions
call method gt_tabref2->get_functions
receiving
value = lo_funclist2.
lo_funclist2->set_all( abap_true ).


******************************************************** Display Using alv table reference display table
call method gt_tabref2->refresh.
call method gt_tabref2->display.
else.
call method gt_tabref2->refresh.
endif.
endform. " populate_alv2

*&---------------------------------------------------------------------*
*& Include YTEST220_ALV_TOP
*&---------------------------------------------------------------------*
tables : kna1.


selection-screen begin of block b1 with frame.
select-options : so_kunnr for kna1-kunnr obligatory.
selection-screen end of block b1.


selection-screen begin of block b2 with frame.
parameters:p_r1 type c radiobutton group rg1 default 'X'.
parameters:p_r2 type c radiobutton group rg1 .
parameters:p_r3 type c radiobutton group rg1 .
selection-screen end of block b2 .

types: begin of ty_data,
kunnr type kunnr ,
land1 type land1_gp ,
name1 type name1_gp ,
name2 type name2_gp ,
ort01 type ort01_gp ,
pstlz type pstlz ,
regio type regio ,
sortl type sortl ,
stras type stras_gp ,
telf1 type telf1 ,
telfx type telfx ,
xcpdk type xcpdk ,
adrnr type adrnr ,
end of ty_data.

types: begin of ty_compdata,
kunnr type kunnr ,
bukrs type bukrs ,
pernr type pernr_d ,
erdat type erdat_rf ,
ernam type ernam_rf ,
sperr type sperb_b ,
loevm type loevm_b ,
zuawa type dzuawa ,
busab type busab ,
akont type akont ,
end of ty_compdata.

data : gt_kna1 type standard table of ty_data,
gt_knb1 type standard table of ty_compdata.
data : gt_tabref type ref to cl_salv_table,
gt_tabref2 type ref to cl_salv_table,
go_sel type ref to cl_salv_selections,
go_sel2 type ref to cl_salv_selections.
data: gt_rows type salv_t_row.

*----------------------------------------------------------------------*
* CLASS lcl_event_handling DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_event_handling definition.
public section.
class-methods:
handle_user_command
for event user_command of cl_gui_alv_grid
importing e_ucomm.
private section.
endclass. "lcl_event_handling DEFINITION

*----------------------------------------------------------------------*
* CLASS lcl_event_handling IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class lcl_event_handling implementation.

method handle_user_command.
case e_ucomm.

when 'CC_DETAILS'.
perform form_get_selected.
endcase.
endmethod. "handle_user_command
endclass. "lcl_event_handling IMPLEMENTATION





*&---------------------------------------------------------------------*
*& Include YTEST220_ALV_TOP
*&---------------------------------------------------------------------*
TABLES : kna1.


SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
SELECT-OPTIONS : so_kunnr FOR kna1-kunnr OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.




TYPES: BEGIN OF ty_data,
kunnr TYPE kunnr ,
land1 TYPE land1_gp ,
name1 TYPE name1_gp ,
name2 TYPE name2_gp ,
ort01 TYPE ort01_gp ,
pstlz TYPE pstlz ,
regio TYPE regio ,
sortl TYPE sortl ,
stras TYPE stras_gp ,
telf1 TYPE telf1 ,
telfx TYPE telfx ,
xcpdk TYPE xcpdk ,
adrnr TYPE adrnr ,
END OF ty_data.

TYPES: BEGIN OF ty_compdata,
kunnr TYPE kunnr ,
bukrs TYPE bukrs ,
pernr TYPE pernr_d ,
erdat TYPE erdat_rf ,
ernam TYPE ernam_rf ,
sperr TYPE sperb_b ,
loevm TYPE loevm_b ,
zuawa TYPE dzuawa ,
busab TYPE busab ,
akont TYPE akont ,
END OF ty_compdata.

DATA : gt_kna1 TYPE TABLE OF ty_data,
gt_knb1 TYPE STANDARD TABLE OF ty_compdata.
DATA : gt_tabref TYPE REF TO cl_salv_table,
gt_tabref2 TYPE REF TO cl_salv_table,
go_sel TYPE REF TO cl_salv_selections,
go_sel2 TYPE REF TO cl_salv_selections,
lo_container TYPE REF TO cl_gui_custom_container,
lo_containerr TYPE REF TO cl_gui_custom_container.
DATA: gt_rows TYPE salv_t_row.



*----------------------------------------------------------------------*
* CLASS lcl_event_handling DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_handling DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
handle_user_command
FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm.
PRIVATE SECTION.
ENDCLASS. "lcl_event_handling DEFINITION

*----------------------------------------------------------------------*
* CLASS lcl_event_handling IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_handling IMPLEMENTATION.

METHOD handle_user_command.
CASE e_ucomm.

WHEN 'CC_DETAILS'.
* PERFORM form_get_selected.
ENDCASE.
ENDMETHOD. "handle_user_command
ENDCLASS. "lcl_event_handling IMPLEMENTATION



























No comments:

Post a Comment