Use of the ALV control
The ALV (SAP List Viewer) control is one of the most commonly used controls in the SAP business, and this chapter introduces a simple example of using the ALV control to display data, and then an instance of ALV's powerful features.
The main contents of this chapter are:
(1) Simple ALV Control instance
(2) ALV control instance for custom output fields
(3) Creating ALV controls on the screen
(4) Customizing the toolbar button of the ALV control
(5) Handling ALV Control double-click event
(6) Edit inner table and database updates via ALV control
(7) Use of the ALV tree
1. Simple ALV Control instances use the school table (Ytjayschool) as an example to define the structure using a data dictionary and display the data through the ALV control
report ytest20160617001.* definition inner table data wa_school like Table of the Ytjayschool with HEADER line.* inner table assignment select * Into table Wa_sch OOL from ytjayschool.* display alvcall FUNCTION ' reuse_alv_list_display ' exporting i_structure_name = ' via Data dictionary structure Ytjayschool ' TABLES t_outtab = Wa_school EXCEPTIONS program_error = 1 OTHERS = 2.
Output Result:
2. ALV control instances for custom output fields
Report YTEST20160617002.*ALV uses the class library type-pools:slis.* a column describing the data wa_alv_field TYPE slis_fieldcat_alv.* column Description inner table, column list data Wa_ Alv_fieldcat TYPE slis_t_fieldcat_alv.* definition inner table data wa_school like Table of Ytjayschool with HEADER line.* inner table assignment select * Into TA BLE Wa_school from ytjayschool.* defines the first to fourth fields Wa_alv_field-col_pos = 1.wa_alv_field-fieldname = ' yct_id '. Wa_alv_field-seltext_m = ' City number '. APPEND Wa_alv_field to Wa_alv_fieldcat. Wa_alv_field-col_pos = 2.wa_alv_field-fieldname = ' ysh_id '. Wa_alv_field-seltext_m = ' School Number '. APPEND Wa_alv_field to Wa_alv_fieldcat. Wa_alv_field-col_pos = 3.wa_alv_field-fieldname = ' Ysh_name '. Wa_alv_field-seltext_m = ' School name '. APPEND Wa_alv_field to Wa_alv_fieldcat. Wa_alv_field-col_pos = 4.wa_alv_field-fieldname = ' ysh_addr '. Wa_alv_field-seltext_m = ' School address '. APPEND Wa_alv_field to Wa_alv_fieldcat. Call FUNCTION ' reuse_alv_list_display ' exporting It_fieldcat = wa_alv_fieldcat TABLES t_outtab = Wa_school.
Output Result:
3. Establish ALV control on the screen 3.1, define screen window to create two text element controls, an Exit button control, a custom control control
3.2. Define logical Flow
PROCESS before OUTPUT. MODULE status_0100.process after INPUT. MODULE user_command_0100.
3.3. Main program code
Report ytest20160620001.* function code return value Data:ok_code type Sy-ucomm, SAVE_OK type sy-ucomm.* definition inner table, variable needs to be passed, without header Linedata WA_SC HOOL TYPE Table of ytjayschool.* inside table assignment select * into table Wa_school from Ytjayschool.*alvdata is the name of the control control defined in screen 100 Data:wa_ CONTAINER type scrfname VALUE ' alvdata ', Alv_grid type ref to Cl_gui_alv_grid, Wa_custom_container type ref to Cl_ Gui_custom_container.* directly calls the window call screen 100.MODULE status_0100 OUTPUT. SET pf-status ' STATUS1 '. * If the window has not yet been created ALV object then create it if Wa_custom_container is INITIAL. CREATE OBJECT wa_custom_container Exporting container_name = Wa_container. CREATE OBJECT alv_grid Exporting i_parent = Wa_custom_container. Call METHOD alv_grid->set_table_for_first_display Exporting i_structure_name = ' Ytjayschool ' CHANGING It_outtab = Wa_school. ENDIF. Endmodule. MODULE user_command_0100 INPUT. SAVE_OK = Ok_code. CLEAR Ok_code. Case SAVE_OK. When ' CANCEL '. LEAVE program. Endcase. Endmodule.
Output Result:
4. Customize the toolbar button of the ALV control to add a custom button on the ALV toolbar and click it to prompt for the row data content. Execution results
Output after clicking the custom button
Description of the program processing flow:
Defines ALV control-related events, interfaces, implementation methods, and so on, primarily defining the following events.
(1) The ALV control's toolbar handles events, defining new buttons and function codes;
(2) ALV The function code handling event for the control, which defines the function code event generated by the user clicking the button.
The main program code is as follows:
Report YTEST20160620002. INCLUDE <icon>. CLASS lcl_event_receiver DEFINITION DEFERRED. Data:ok_code type Sy-ucomm, SAVE_OK type Sy-ucomm. DATA event_receiver TYPE REF to Lcl_event_receiver. Data:wa_school TYPE TABLE of Ytjayschool, A_school like Ytjayschool. SELECT * into TABLE wa_school from Ytjayschool. Data:wa_container type scrfname VALUE ' alvdata ', Alv_grid type REF to Cl_gui_alv_grid, Wa_custom_container type RE F to Cl_gui_custom_container. Call screen 100.CLASS lcl_event_receiver DEFINITION. Public section. Methods:handle_toolbar for EVENT TOOLBAR of Cl_gui_alv_grid importing E_object e_interactive, H Andle_user_command for EVENT User_command of Cl_gui_alv_grid importing E_ucomm. Endclass. CLASS lcl_event_receiver implementation. METHOD Handle_toolbar. Data:ls_toolbar TYPE Stb_button. CLEAR Ls_toolbar-butn_type. APPEND Ls_toolbar to E_object->mt_toolbar. CLEAR Ls_toolbar. MOVE ' Show_deta ' to Ls_tooLbar-function. MOVE Icon_ppe_vnode to Ls_toolbar-icon. MOVE ' School details show ' to Ls_toolbar-quickinfo. MOVE ' School details show ' to Ls_toolbar-text. MOVE ' to ls_toolbar-disabled. APPEND Ls_toolbar to E_object->mt_toolbar. EndMethod. METHOD Handle_user_command. Data:lt_rows TYPE Lvc_t_row. Case E_ucomm. When ' Show_deta '. Call METHOD alv_grid->get_selected_rows Importing et_index_rows = Lt_rows. Call METHOD Cl_gui_cfw=>flush. IF SY-SUBRC = 0. MESSAGE S005 (ymess) with ' selected Row '. PERFORM Messdeta TABLES lt_rows. ENDIF. Endcase. EndMethod. Endclass. MODULE status_0100 OUTPUT. SET pf-status ' STATUS1 '. IF Wa_custom_container is INITIAL. CREATE OBJECT wa_custom_container Exporting container_name = Wa_container. CREATE OBJECT alv_grid Exporting i_parent = Wa_custom_container. Call METHOD alv_grid->set_table_for_first_display Exporting i_structure_name = ' Ytjayschool ' CHANGING It_outtab = Wa_school. CREATE OBJECT event_receiver. SET HANDLER Event_receiver->handle_user_command for Alv_grid. SET HANDLER Event_receiver->handle_toolbar for Alv_grid. Call METHOD alv_grid->set_toolbar_interactive. ENDIF. Endmodule. MODULE user_command_0100 INPUT. SAVE_OK = Ok_code. CLEAR Ok_code. Case SAVE_OK. When ' CANCEL '. LEAVE program. Endcase. Endmodule. FORM Messdeta TABLES p_et_index_rows STRUCTURE lvc_s_row. Data:ls_selected_line like Lvc_s_row, Lf_row_index TYPE lvc_index. DATA:S1 (+) Type C, S2 (3) Type C. S2 = '-'. LOOP at P_et_index_rows to Ls_selected_line. Lf_row_index = Ls_selected_line-index. READ TABLE wa_school INDEX lf_row_index into A_school. S1 = ' Select Line Contents '. Concatenate S1 A_school-ysh_name a_school-ysh_addr into S1 separated by S2. MESSAGE I005 (ymess) with S1. Endloop. EndForm.
5, processing ALV Double-click event Main program code as follows
Report ytest20160620003.* function code return value Data:ok_code type Sy-ucomm, SAVE_OK type sy-ucomm.* definition inner table, variable needs to be passed, without header Linedata WA_SC HOOL TYPE Table of ytjayschool.* inside table assignment select * into table wa_school from ytjayschool.* define window custom control, define ALV object Data:wa_container TYP E scrfname VALUE ' alvdata ', Alv_grid type ref to Cl_gui_alv_grid, Wa_custom_container type ref to Cl_gui_custom_co Ntainer.* defining Event Type class Lcl_event_receiver definition deferred.* defining event data event_receiver TYPE REF to Lcl_event_receiver.* Directly call the window called screen 100.CLASS lcl_event_receiver DEFINITION. Public section. Methods:handle_double_click for EVENT Double_click of Cl_gui_alv_grid importing E_row e_column. Endclass. CLASS lcl_event_receiver implementation. METHOD Handle_double_click. Data:li_school like line of Wa_school. READ TABLE wa_school INDEX e_row-index into li_school.* merge rows and other information into the string data:s1 (+) TYPE c.* concatenate ' line: ' E_row-ind EX ' column name: ' E_column-fieldname into S1. Concatenate S1 ' SCHOOL NAME: ' Li_school-ysH_name into S1. Concatenate S1 ' SCHOOL ADDR: ' li_school-ysh_addr into s1.* displays the row and column information clicked in the status bar message S208 (XX) with S1. EndMethod. Endclass. MODULE status_0100 OUTPUT. SET pf-status ' STATUS1 '. * If the window has not yet been created ALV object then create it if Wa_custom_container is INITIAL. CREATE OBJECT wa_custom_container Exporting container_name = Wa_container. CREATE OBJECT alv_grid Exporting i_parent = Wa_custom_container. Call METHOD alv_grid->set_table_for_first_display Exporting i_structure_name = ' Ytjayschool ' CHANGING It_outtab = Wa_school.*alv Object Assignment Double-click event CREATE object event_receiver. SET HANDLER Event_receiver->handle_double_click for Alv_grid. ENDIF. Endmodule. MODULE user_command_0100 INPUT. SAVE_OK = Ok_code. CLEAR Ok_code. Case SAVE_OK. When ' CANCEL '. LEAVE program. Endcase. Endmodule.
For example, when you double-click the output interface, note the information displayed by the status bar: the name of the school, the value of the school address.
6, through the ALV control edit the internal table and database update processing process is as follows:
(1) Set ALV control can be edited;
(2) Update the data to the internal table when exiting the screen;
(3) Capturing the data change information of the ALV control and saving the deleted row information of the ALV control to the inner table;
(4) In the output, the comparison deletes the row and the last inner table, deletes the duplicate row;
(5) Update the data to the data table.
The main program code is as follows:
Report YTEST20160620004. Data:ok_code type Sy-ucomm, SAVE_OK type Sy-ucomm. TABLES Ytjayschool. DATA Ls_school TYPE Ytjayschool. DATA Wa_school TYPE TABLE of Ytjayschool. DATA Wadel_school TYPE TABLE of Ytjayschool. SELECT * into TABLE wa_school from Ytjayschool. Data:wa_container type scrfname VALUE ' alvdata ', Alv_grid type REF to Cl_gui_alv_grid, Wa_custom_container type RE F to Cl_gui_custom_container. DATA wa_layout TYPE Lvc_s_layo. Wa_layout-edit = ' X '. CLASS lcl_event_receiver DEFINITION DEFERRED. DATA event_receiver TYPE REF to Lcl_event_receiver. CLASS lcl_event_receiver DEFINITION. Public section. Types:del_rows TYPE Standard TABLE of Ytjayschool. Data:ddel_rows TYPE Standard TABLE of Ytjayschool. Methods:handle_data_changed for EVENT data_changed of Cl_gui_alv_grid importing er_data_changed. Methods:update_delta_tables importing pr_data_changed TYPE REF to Cl_alv_changed_data_protocol. Methods:get_deleted_rows Exporting Deleted_rows TYPE del_rows. Endclass. CLASS lcl_event_receiver implementation. METHOD handle_data_changed. Call METHOD update_delta_tables (er_data_changed). EndMethod. METHOD Update_delta_tables. Data:l_del_row TYPE Lvc_s_moce. LOOP at Pr_data_changed->mt_deleted_rows to L_del_row. READ TABLE wa_school into Ls_school INDEX l_del_row-row_id. IF SY-SUBRC NE 0. MESSAGE E208 with ' processing error '. ELSE. APPEND Ls_school to Ddel_rows. ENDIF. Endloop. EndMethod. METHOD get_deleted_rows. Deleted_rows = Me->ddel_rows. EndMethod. Endclass. Start-of-selection. Call Screen 100. Write/'---------------delete the inner table record---------------'. WRITE/' ____________________________________________ '. Call METHOD event_receiver->get_deleted_rows Importing deleted_rows = Wadel_school. LOOP at Wadel_school to Ytjayschool. write:/ Ytjayschool-ysh_id,ytjayschool-ysh_name,ytjayschool-ysh_addr. Endloop. write:/ '---------------updated inner table records---------------:'. write:/ '____________________________________________'. LOOP at Wa_school to Ytjayschool. write:/ Ytjayschool-ysh_id,ytjayschool-ysh_name,ytjayschool-ysh_addr. endloop.* when the ' SAVE '. * MODIFY ytjayschool from TABLE wa_school.* IF sy-subrc NE 0.* MESSAGE I005 (ymess) with ' Update data error '. * exit.* else.* MESSAGE I005 (ymess) with ' Update data OK '. * endif.** DELETE ytjayschool from TABLE DEL a_school.* IF SY_SUBRC NE 0.* message I005 (ymess) with ' Update data error '. * else.* MESSAGE I005 (ymess) with ' Update data OK ' . * ENDIF. End-of-selection. MODULE status_0100 OUTPUT. SET pf-status ' STATUS1 '. IF Wa_custom_container is INITIAL. CREATE OBJECT wa_custom_container Exporting container_name = Wa_container. CREATE OBJECT alv_grid Exporting i_parent = Wa_custom_container. CREATE OBJECT event_receiver. SET HANDLER event_receiver->handle_data_changed for Alv_grid. Call METHOD Alv_grid->set_table_for_first_display Exporting I_structure_name = ' Ytjayschool ' is_layout = wa_layout changing It_outtab = Wa_school. ENDIF. Endmodule. MODULE user_command_0100 INPUT. SAVE_OK = Ok_code. CLEAR Ok_code. Case SAVE_OK. When ' CANCEL '. DATA l_ret VALUE ' X '. Call METHOD alv_grid->check_changed_data Importing e_valid = L_ret. LEAVE to screen 0. Endcase. Endmodule.
Output: Delete the last two rows of data, click the "CANCEL" button, the results are as follows:
7. ALV tree is used in SAP business system, the ALV tree object is used extensively, the object classifies the same kind of data on the basis of table, and summarizes the data of each classification.
Take the school table (Ytjayschool) as an example:
(1) Classification by city and school;
(2) In the right screen output school name, school address, and control its output length.
Processing process:
(1) Set up the program and screen, set up the custom control object on the screen, define the logical flow;
(2) The establishment of Pai, PBO incident;
(3) Define the custom control object in PAI and establish the ALV tree object;
(4) Establishing the title of the Alv tree object;
(5) Set up the Right Screen output field list, field length and other content;
(6) Establish the Tree classification field list and output sequence;
(7) Displays the ALV tree object.
Main program code:
Report YTEST20160621001. Data:ok_code type Sy-ucomm, SAVE_OK type Sy-ucomm. Data:gb_fieldcat TYPE Lvc_t_fcat. DATA:GB_SORTFLD TYPE Lvc_t_sort. DATA Wa_school TYPE TABLE of Ytjayschool. SELECT * into TABLE wa_school from Ytjayschool. Data:wa_container type scrfname VALUE ' alvdata ', Alv_grid type REF to Cl_gui_alv_tree_simple, Wa_custom_contain ER TYPE REF to Cl_gui_custom_container. Call screen 100.MODULE status_0100 OUTPUT. SET pf-status ' STATUS1 '. IF Wa_custom_container is INITIAL. DATA Ls_list_comm TYPE Slis_t_listheader. DATA Ls_alist_comm TYPE Slis_listheader. Ls_alist_comm-typ = ' H '. Ls_alist_comm-info = ' MY ALV TREE testing '. APPEND Ls_alist_comm to Ls_list_comm. PERFORM Bldcat. PERFORM bldsortfld. CREATE OBJECT wa_custom_container Exporting container_name = Wa_container. CREATE OBJECT alv_grid Exporting i_parent = Wa_custom_container. Call METHOD Alv_grid->set_table_for_first_display Exporting It_list_commentarY = Ls_list_comm i_structure_name = ' ytjayschool ' changing it_sort = gb_sortfld It_fieldcatalog = Gb_fieldcat It_outtab = Wa_school. Call METHOD alv_grid->expand_tree Exporting I_level = 1. ENDIF. Endmodule. MODULE user_command_0100 INPUT. SAVE_OK = Ok_code. CLEAR Ok_code. Case SAVE_OK. When ' CANCEL '. LEAVE program. Endcase. Endmodule. FORM Bldcat. Call FUNCTION ' lvc_fieldcatalog_merge ' exporting i_structure_name = ' ytjayschool ' changing ct_fieldcat = G B_fieldcat. DATA ls_fldcat TYPE Lvc_s_fcat. LOOP at Gb_fieldcat to Ls_fldcat. Case Ls_fldcat-fieldname. When ' ysh_name ' OR ' ysh_addr '. Ls_fldcat-outputlen = 15. When OTHERS. Ls_fldcat-no_out = ' X '. Endcase. MODIFY Gb_fieldcat from Ls_fldcat. Endloop. EndForm. FORM bldsortfld. DATA ls_sortfld TYPE Lvc_s_sort. Ls_sortfld-spos = 1. Ls_sortfld-fieldname = ' yct_id '. ls_sortfld-up = ' X '. Ls_sortfld-subtot = ' X '. APPEND Ls_sortFLD to GB_SORTFLD. Ls_sortfld-spos = 2. Ls_sortfld-fieldname = ' ysh_id '. ls_sortfld-up = ' X '. Ls_sortfld-subtot = ' X '. APPEND ls_sortfld to gb_sortfld.* ls_sortfld-spos = 3.* ls_sortfld-fieldname = ' ysh_name '. * ls_sortfld-up = ' X '. * LS_S Ortfld-subtot = ' X '. * APPEND ls_sortfld to gb_sortfld.** ls_sortfld-spos = 4.* ls_sortfld-fieldname = ' ysh_addr '. * LS_ sortfld-up = ' x '. * ls_sortfld-subtot = ' x '. * APPEND ls_sortfld to GB_SORTFLD. EndForm.
Use of ABAP ALV controls