Professional Documents
Culture Documents
Extensive Tips and Tricks For Interactive SAP ALV
Extensive Tips and Tricks For Interactive SAP ALV
1
2 CREATE OBJECT container_r
3 EXPORTING
4 container_name = 'ALV_CONT'
5 EXCEPTIONS
6 cntl_error = 1
7 cntl_system_error = 2
8 create_error = 3
9 lifetime_error = 4
10 lifetime_dynpro_dynpro_link = 5.
3. Create Reference Object for the ALV Grid as below:
1
2 CREATE OBJECT grid_r
3 EXPORTING
4 i_parent = container_r.
4. Define the ALV Layout as below:
1
2 gw_layout-cwidth_opt = abap_true.
3 gw_layout-zebra = abap_true.
4 gw_layout-sel_mode = ‘D’.
5. Populate the Field Catalog data for display as below:
1
2 lv_pos = lv_pos + 1. “Position number
3 CLEAR wa_fieldcat.
4 wa_fieldcat-fieldname = 'BUKRS'. "Company Code
5 wa_fieldcat-col_pos = lv_pos.
6 wa_fieldcat-ref_table = 'T001'.
7 APPEND wa_fieldcat TO gt_fieldcat.
5.1. For Field Editable:
1
2 wa_fieldcat-edit = abap_true.
Also Read: ALV with an Editable Row.
1
2 wa_fieldcat-checkbox = abap_true.
5.4. For the field where customized F4-Help is required:
1
2 wa_fieldcat-f4availabl = abap_true.
5.5. For the field where the Summation is required:
1
2 wa_fieldcat-do_sum = abap_true.
Additionally, the field based on which summation will appear needs to be
defined as below(eg. ANLKL):
1
2 CLEAR li_sort.
3
4 lst_sort-fieldname = 'ANLKL'.
5
6 lst_sort-subtot = abap_true.
7
8 APPEND lst_sort TO li_sort
6. To Restrict Functions from ALV display:
1
2 lw_exclude = cl_gui_alv_grid=>mc_fc_check.
3 APPEND lw_exclude TO gt_exclude.
4 lw_exclude = cl_gui_alv_grid=>mc_fc_refresh.
5 APPEND lw_exclude TO gt_exclude.
6 lw_exclude = cl_gui_alv_grid=>mc_fc_loc_copy_row.
7 APPEND lw_exclude TO gt_exclude.
8 lw_exclude = cl_gui_alv_grid=>mc_fc_loc_delete_row.
9 APPEND lw_exclude TO gt_exclude.
10 lw_exclude = cl_gui_alv_grid=>mc_fc_loc_append_row.
11 APPEND lw_exclude TO gt_exclude.
12 lw_exclude = cl_gui_alv_grid=>mc_fc_loc_insert_row.
13 APPEND lw_exclude TO gt_exclude.
7. Populate the Data in table GT_DATA.
1
2 CALL METHOD grid_r->set_table_for_first_display
3 EXPORTING
4 i_structure_name = 'ZFI_S_PIS' “Structure
5 is_layout = gw_layout
6 it_toolbar_excluding = gt_exclude
7 i_save = gc_save_a "A
8 i_default = abap_true
9 CHANGING
10 it_outtab = gt_data[] “Table with ALV data
11
12 It_sort = li_sort
13 it_fieldcatalog = gt_fieldcat[].
1
2 CALL METHOD cl_gui_control=>set_focus
3 EXPORTING
4 control = grid_r.
Data declaration for Reference:
1
2 Data:
3
4 container_r TYPE REF TO cl_gui_custom_container, "Container for ALV PIS data
5
6 grid_r TYPE REF TO cl_gui_alv_grid, "Grid for ALV PIS data
7
8 gt_exclude TYPE ui_functions, "Exclude functions from ALV
9
10 gt_fieldcat TYPE lvc_t_fcat, "Field catalog for ALV PIS data
11
12 ref_aplic TYPE REF TO lcl_application_alv,
13
14 gw_layout TYPE lvc_s_layo. "Layout for PIS
9. If the Data just needs to be Refreshed:
1
2 CALL METHOD grid_r->refresh_table_display
3 EXCEPTIONS
4 finished = 1
5 OTHERS = 2.
10. To make fields Editable, call the below method while calling ALV for
display:
1
2 CALL METHOD grid_r->register_edit_event
3 EXPORTING
4 i_event_id = cl_gui_alv_grid=>mc_evt_modified. " enter. "MC_EVT_MODIFIED
11. Now to Validate the Data Changed in the grid we need to do the
following:
11.1. Define a
class (lcl_application_alv) definition and implementation and define
event (handle_data_changed) for event data_changed of
class cl_gui_alv_grid in it.
1
2 CLASS lcl_application_alv DEFINITION.
3 PUBLIC SECTION.
4 METHODS:
5 handle_data_changed
6 FOR EVENT data_changed OF cl_gui_alv_grid
7 IMPORTING er_data_changed e_onf4_after.
8 ENDCLASS. "lcl_application_alv DEFINITION
11.2. In method handle_data_changed call
methods get_cell_value and modify_cell to get and modify cell values like
below –
1
2 LOOP AT er_data_changed->mt_good_cells INTO lw_good.
3
4 WHEN 'KUNNR'.
5 CALL METHOD er_data_changed->get_cell_value
6 EXPORTING
7 i_row_id = lw_good-row_id
8 i_fieldname = lw_good-fieldname
9 IMPORTING
10 e_value = gv_kunnr.
11 * Get name1 for the kunnr
12 SELECT SINGLE name1 FROM kna1
13 INTO lw_pis-name1
14 WHERE kunnr = gv_kunnr .
15 IF sy-subrc = 0.
16 * update name1 in table
17 CALL METHOD er_data_changed->modify_cell
18 EXPORTING
19 i_row_id = lw_good-row_id
20 i_fieldname = 'NAME1'
21 i_value = lv_name1.
22 ELSE.
23 MESSAGE i000 WITH 'Invalid Customer number in row'(027)
24 lw_good-row_id.
25 ENDIF.
12. Now before calling method set_table_for_first_display, write the below
code:
1
2 CREATE OBJECT ref_aplic.
3 SET HANDLER ref_aplic->handle_data_changed FOR grid_r.
13. If the Field Catalog is required to change when different actions are
executed for the ALV grid data display, call
method set_frontend_fieldcatalog after populating the field catalog data to
refresh the field catalog settings.
1
2 CALL METHOD grid_r->set_frontend_fieldcatalog
3 EXPORTING
4 it_fieldcatalog = gt_fieldcat[].
14. If Conditional F4 Help is required to be populated in the ALV, say based
on one data in ALV, another field F4-Help data needs to be populated then do
the following:
1
2 lw_f4-fieldname = 'ZFI_BRN_LOC'. "IFSC code
3 lw_f4-register = abap_true.
4 lw_f4-getbefore = space.
5 lw_f4-chngeafter = abap_true.
6 INSERT lw_f4 INTO TABLE lt_f4.
7
8 CLEAR lw_f4.
9 lw_f4-fieldname = 'STGRD'. "Reason for Reversal
10 lw_f4-register = abap_true.
11 lw_f4-getbefore = space.
12 INSERT lw_f4 INTO TABLE lt_f4.
13
14 CALL METHOD grid_r->register_f4_for_fields
15 EXPORTING
16 it_f4 = lt_f4.
14.2. In class lcl_application_alv, define the method and then implement it.
1
2 METHODS:
3 handle_on_f4 FOR EVENT onf4 OF cl_gui_alv_grid
4 IMPORTING sender
5 e_fieldname
6 e_fieldvalue
7 es_row_no
8 er_event_data
9 et_bad_cells
10 e_display.
14.3. In class lcl_application_alv implementation, implement the
method handle_on_f4.
1
2 *-------------------------------------------------------------
3 * Add the custom F4 values for IFSC code
4 *-------------------------------------------------------------
5 IF e_fieldname = 'ZFI_BRN_LOC'.
6 IF gv_flag_inst_type = abap_true. "For chq only
7 * IF gt_data[] IS INITIAL.
8 CLEAR lt_knbk.
9 CLEAR gt_data.
10 READ TABLE gt_pis INTO lw_pis INDEX es_row_no-row_id.
11 IF sy-subrc = 0.
12 SELECT bankl bkref FROM knbk
13 INTO TABLE lt_knbk WHERE kunnr = lw_pis-kunnr. "gv_kunnr.
14 IF sy-subrc = 0.
15 LOOP AT lt_knbk INTO lw_knbk.
16 lw_data-zfi_brn_loc = lw_knbk-bankl.
17 lw_data-zfi_bnk_code = lw_knbk-bkref.
18 APPEND lw_data TO gt_data.
19 CLEAR lw_data.
20 ENDLOOP.
21 ENDIF.
22 ENDIF.
23 * ENDIF.
24 *Call the function module to display the custom F4 values
25 CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
26 EXPORTING
27 retfield = 'ZFI_BRN_LOC'
28 window_title = 'List of IFSC entries'(026)
29 value_org = gc_val_org
30 TABLES
31 value_tab = gt_data[]
32 return_tab = lt_ret[]
33 EXCEPTIONS
34 parameter_error = 1
35 no_values_found = 2
36 OTHERS = 3.
37 * (note: gt_ret[] contains the row id selected by the user from the list of f4 values)
38 IF sy-subrc = 0.
39 READ TABLE lt_ret INTO lw_sel INDEX 1.
40 ASSIGN er_event_data->m_data->* TO <itab>.
41 READ TABLE gt_pis INDEX es_row_no-row_id
42 INTO lw_pis.
43 lw_modi-row_id = es_row_no-row_id.
44 lw_modi-fieldname = 'ZFI_BRN_LOC'.
45 lw_modi-value = lw_sel-fieldval.
46 APPEND lw_modi TO <itab>.
47
48 READ TABLE gt_data INTO lw_data
49 WITH KEY zfi_brn_loc = lw_modi-value+0(11).
50 IF sy-subrc = 0.
51 lw_modi-row_id = es_row_no-row_id.
52 lw_modi-fieldname = 'ZFI_BNK_CODE'.
53 lw_modi-value = lw_data-zfi_bnk_code.
54 APPEND lw_modi TO <itab>.
55 ENDIF.
56 ENDIF.
57 ENDIF.
58 ENDIF.
59 *-------------------------------------------------------------
60 * Add the custom F4 values for Reversal Reason
61 *-------------------------------------------------------------
62 IF e_fieldname = 'STGRD'.
63 CLEAR: lt_t041ct, lt_return.
64 IF gv_uname = gc_depo_userid. "BTDEPO
65 SELECT stgrd txt40 FROM t041ct
66 INTO TABLE lt_t041ct
67 WHERE spras = sy-langu
68 AND stgrd = gc_stgrd_06.
69 IF sy-subrc = 0.
70 * do nothing
71 ENDIF.
72 ELSE.
73 SELECT stgrd txt40 FROM t041ct
74 INTO TABLE lt_t041ct
75 WHERE spras = sy-langu
76 AND stgrd IN (gc_stgrd_06,
77 gc_stgrd_08,
78 gc_stgrd_09,
79 gc_stgrd_10).
80 IF sy-subrc = 0.
81 * do nothing
82 ENDIF.
83 ENDIF.
84
85 IF sy-subrc = 0.
86 CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
87 EXPORTING
88 retfield = 'STGRD'
89 window_title = 'List of Rev reason entries'(025)
90 value_org = gc_val_org
91 TABLES
92 value_tab = lt_t041ct
93 return_tab = lt_return
94 EXCEPTIONS
95 parameter_error = 1
96 no_values_found = 2
97 OTHERS = 3.
98 IF sy-subrc = 0.
99 READ TABLE lt_return INTO lw_sel INDEX 1.
100 ASSIGN er_event_data->m_data->* TO <itab>.
101 READ TABLE gt_pis INDEX es_row_no-row_id
102 INTO lw_pis.
103 lw_modi-row_id = es_row_no-row_id.
104 lw_modi-fieldname = 'STGRD'.
105 lw_modi-value = lw_sel-fieldval.
106 APPEND lw_modi TO <itab>.
107 ENDIF.
108 ENDIF.
109 ENDIF.
110 er_event_data->m_event_handled = abap_true. "(to inform grid that f4 was handled manually)
14.4. Now after Register_F4_for_Fields is called, add below code:
1
2 SET HANDLER ref_aplic->handle_on_f4 FOR grid_r.
15. When the Back button is pressed, clear and refresh as below:
1
2 CALL METHOD grid_r->refresh_table_display.
3 CALL METHOD grid_r->free.
4 CALL METHOD container_r->free.
5 CALL METHOD cl_gui_cfw=>flush.
16. To make an ALV invisible add below code:
1
2 CALL METHOD inv_grid_r->set_visible( EXPORTING visible = '0' ).
17. To make Row Selection through code: Pass the row ids into
lt_lvc_s_roid-
1
2 lw_lvc_s_roid-row_id = sy-tabix.
3 APPEND lw_lvc_s_roid TO lt_lvc_s_roid.
4
5 CALL METHOD grid_r->set_selected_rows
6 EXPORTING
7 it_row_no = lt_lvc_s_roid.
18. To Reset Scroll Bar Position after selecting rows through code, add the
below code:
1
2 CALL METHOD grid_r->get_scroll_info_via_id
3 IMPORTING
4 es_row_no = lw_row_no
5 es_row_info = lw_row_info
6 es_col_info = lw_col_info.
7
8 CALL METHOD grid_r->set_selected_rows
9 EXPORTING
10 it_row_no = lt_lvc_s_roid.
11
12 CALL METHOD grid_r->set_scroll_info_via_id(
13 EXPORTING
14 is_row_no = lw_row_no
15 is_row_info = lw_row_info
16 is_col_info = lw_col_info
17 ).
19. Display Top-of-Page data:
1
2 * Create TOP-Document
3
4 CREATE OBJECT g_dyndoc_id
5
6 EXPORTING
7
8 style = 'ALV_GRID'.
9
10 * Create Splitter for custom_container
11
12 CREATE OBJECT g_splitter
13
14 EXPORTING
15
16 parent = g_custom_container
17
18 rows = 2
19
20 columns = 1.
1
2 CALL METHOD g_splitter->get_container
3
4 EXPORTING
5
6 row = 1
7
8 column = 1
9
10 RECEIVING
11
12 container = g_parent_top.
1
2 CALL METHOD g_splitter->get_container
3
4 EXPORTING
5
6 row = 2
7
8 column = 1
9
10 RECEIVING
11
12 container = g_parent_grid.
1
2 * Set height for g_parent_html
3
4 CALL METHOD g_splitter->set_row_height
5
6 EXPORTING
7
8 id = 1
9
10 height = 20.
11
12 CREATE OBJECT g_grid
13
14 EXPORTING
15
16 i_parent = g_parent_grid
17
18 EXCEPTIONS
19
20 error_cntl_create = 1
21
22 error_cntl_init = 2
23
24 error_cntl_link = 3
25
26 error_dp_create = 4
27
28 OTHERS = 5.
Also Check: OOPs Report Using Splitter and ALV Tree Combination
19.2. Now in the local class, define method handle_top_of_page as below:
1
2 METHODS:
3
4 *Define method to handle Toolbar
5
6 handle_top_of_page FOR EVENT top_of_page
7
8 OF cl_gui_alv_grid
9
10 IMPORTING e_dyndoc_id.
19.3. In the class implementation, define the method details:
1
2 METHOD handle_top_of_page.
3
4 PERFORM f_event_top_of_page USING g_dyndoc_id.
5
6 ENDMETHOD. "EVENT_HANDLER
19.4. Logic in the Subroutine:
1
2 FORM f_event_top_of_page USING dg_dyndoc_id TYPE REF TO cl_dd_document.
1
2 DATA: lv_name TYPE lvc_fname, "Name
3
4 li_t093b TYPE STANDARD TABLE OF ty_t093b
5
6 INITIAL SIZE 0, "T093B temp table
7
8 lst_t093b TYPE ty_t093b, "T093b wa
9
10 lv_reptext TYPE reptext, "report Text
11
12 lv_text TYPE sdydo_text_element. "Final text
1
2 * Header1
3
4 lv_name = c_header1.
5
6 PERFORM f_read_text USING lv_name
7
8 CHANGING lv_reptext.
9
10 lv_text = lv_reptext.
11
12 CALL METHOD dg_dyndoc_id->add_text
13
14 EXPORTING
15
16 text = lv_text
17
18 sap_style = cl_dd_area=>heading
19
20 sap_fontsize = cl_dd_area=>large
21
22 sap_color = cl_dd_area=>list_heading_int.
23
24 * Add new-line
25
26 CALL METHOD dg_dyndoc_id->new_line.
1
2 * Header2
3
4 WRITE s_budat-low TO g_fdate.
5
6 WRITE s_budat-high TO g_tdate.
7
8 lv_name = c_header2.
9
10 PERFORM f_read_text USING lv_name
11
12 CHANGING lv_reptext.
13
14 REPLACE '&G_FDATE&' INTO lv_reptext WITH g_fdate.
15
16 REPLACE '&G_TDATE&' INTO lv_reptext WITH g_tdate.
17
18 lv_text = lv_reptext.
19
20 CALL METHOD dg_dyndoc_id->add_gap.
21
22 CALL METHOD g_dyndoc_id->add_text
23
24 EXPORTING
25
26 text = lv_text
27
28 sap_emphasis = cl_dd_area=>heading.
29
30 * Add new-line
31
32 CALL METHOD dg_dyndoc_id->new_line.
1
2 * Header3
3
4 lv_name = c_header3.
5
6 PERFORM f_read_text USING lv_name
7
8 CHANGING lv_reptext.
9
10 lv_text = lv_reptext.
11
12 CALL METHOD dg_dyndoc_id->add_gap.
13
14 CALL METHOD g_dyndoc_id->add_text
15
16 EXPORTING
17
18 text = lv_text
19
20 sap_emphasis = cl_dd_area=>heading.
21
22 * Add new-line
23
24 CALL METHOD dg_dyndoc_id->new_line.
25
26
1
2 * Header4
3
4 lv_name = c_header4.
5
6 PERFORM f_read_text USING lv_name
7
8 CHANGING lv_reptext.
9
10 lv_text = lv_reptext.
11
12 li_t093b[] = i_t093b[].
13
14 SORT li_t093b BY waers.
15
16 DELETE ADJACENT DUPLICATES FROM li_t093b COMPARING waers.
17
18 LOOP AT li_t093b INTO lst_t093b.
19
20 CONCATENATE lv_text lst_t093b-waers INTO lv_text
21
22 SEPARATED BY space.
23
24 ENDLOOP.
25
26 CALL METHOD dg_dyndoc_id->add_gap.
27
28 CALL METHOD g_dyndoc_id->add_text
29
30 EXPORTING
31
32 text = lv_text
33
34 sap_emphasis = cl_dd_area=>heading.
1
2 * Display output
3
4 PERFORM f_display.
5
6 ENDFORM. " EVENT_TOP_OF_PAGE
1
2 *&---------------------------------------------------------------------*
3
4 *& Subroutine F_DISPLAY
5
6 *&---------------------------------------------------------------------*
7
8 * Display data
9
10 *----------------------------------------------------------------------*
11
12 FORM f_display.
13
14 * Creating html control
15
16 IF g_html_cntrl IS INITIAL.
17
18 CREATE OBJECT g_html_cntrl
19
20 EXPORTING
21
22 parent = g_parent_top.
23
24 ENDIF.
25
26 CALL METHOD g_dyndoc_id->merge_document.
27
28 g_dyndoc_id->html_control = g_html_cntrl.
29
30 * Display document
31
32 CALL METHOD g_dyndoc_id->display_document
33
34 EXPORTING
35
36 reuse_control = abap_true
37
38 parent = g_parent_top
39
40 EXCEPTIONS
41
42 html_display_error = 1.
43
44 IF sy-subrc NE 0.
45
46 * Error in displaying top-of-page
47
48 MESSAGE i023.
49
50 LEAVE LIST-PROCESSING.
51
52 ENDIF.
53
54 ENDFORM. " display
19.5. Before method set_table_for_first_display is called write the below
code:
1
2 SET HANDLER ref_aplic->handle_top_of_page FOR grid_r.
3
4 CALL METHOD g_dyndoc_id->initialize_document
5
6 EXPORTING
7
8 background_color = cl_dd_area=>col_textarea.
9
10 * Processing events
11
12 CALL METHOD g_grid->list_processing_events
13
14 EXPORTING
15
16 i_event_name = 'TOP_OF_PAGE'
17
18 i_dyndoc_id = g_dyndoc_id.
Some data declaration help:
1
2 g_dyndoc_id TYPE REF TO cl_dd_document, "Object ref for document header
3
4 g_splitter TYPE REF TO cl_gui_splitter_container, "Object ref for splitter
5
6 g_parent_grid TYPE REF TO cl_gui_container, "Object ref for grid container
7
8 g_parent_top TYPE REF TO cl_gui_container, "Object ref for top container
9
10 g_html_cntrl TYPE REF TO cl_gui_html_viewer, "Object ref for html control
11
12 g_custom_container TYPE REF TO cl_gui_custom_container, "Object ref for custom container
13
14 i_fcat TYPE lvc_t_fcat. "Field catalog
Whether you think we have mentioned all or not, in either case, please leave a
quick comment below.
From https://sapyard.com/extensive-tips-and-tricks-for-interactive-sap-alv/