REPORT z_alv_template. * Data (for the ALV Grid) TYPES: BEGIN OF t_alv_data, cust_id TYPE kunnr, "Customer Number cust_name TYPE name1_gp, "Customer Name * cell coloring field color TYPE lvc_t_scol, "Cell coloring END OF t_alv_data. DATA: v_alv_data TYPE t_alv_data, i_alv_data TYPE STANDARD TABLE OF t_alv_data. * ALV grid containers and objects DATA: o_alv_grid TYPE REF TO cl_gui_alv_grid, o_alv_cont TYPE REF TO cl_gui_custom_container. * ALV field catalog DATA: i_alv_fc TYPE lvc_t_fcat, v_alv_fc LIKE lvc_s_fcat. * ALV Layout (colors) DATA: v_alv_layout TYPE lvc_s_layo, i_alv_color TYPE lvc_t_scol, v_alv_color TYPE lvc_s_scol, v_alv_color_cell TYPE lvc_s_colo. * ALV variant DATA: v_alv_variant TYPE disvariant. PARAMETERS: p_alvvar TYPE disvariant-variant DEFAULT 'DEFAULT'. * Class for event handling *----------------------------------------------------------------------* * CLASS lcl_event_receiver DEFINITION *----------------------------------------------------------------------* * [+] Event listener for the ALV grid * [+] Handles hotspots and data changes *----------------------------------------------------------------------* CLASS lcl_event_receiver DEFINITION. PUBLIC SECTION. METHODS: * Hotspot clicking hotspot_click FOR EVENT hotspot_click OF cl_gui_alv_grid IMPORTING e_row_id e_column_id es_row_no, * Data changed (such as checkbox clicking) handle_data_changed FOR EVENT data_changed OF cl_gui_alv_grid IMPORTING er_data_changed. ENDCLASS. "lcl_event_receiver DEFINITION *---------------------------------------------------------------------* * CLASS lcl_event_receiver IMPLEMENTATION *---------------------------------------------------------------------* * [+] Implementation of the ALV Grid event handler class *---------------------------------------------------------------------* CLASS lcl_event_receiver IMPLEMENTATION. *---------------------------------------------------------------------* * METHOD hotspot_click * *---------------------------------------------------------------------* * [+] Calls evvent_hotspot_click when a hotspot is clicked in the ALV *---------------------------------------------------------------------* METHOD hotspot_click. PERFORM event_hotspot_click USING e_row_id e_column_id. ENDMETHOD. "hotspot_click *---------------------------------------------------------------------* * METHOD handle_data_changed * *---------------------------------------------------------------------* * [+] Updates the source data when the data in the ALV display has * been changed, such as by clicking a checkbox. *---------------------------------------------------------------------* METHOD handle_data_changed. DATA: lv_changed TYPE lvc_s_modi. LOOP AT er_data_changed->mt_good_cells INTO lv_changed WHERE fieldname = 'CUST_NAME'. READ TABLE i_alv_data INTO v_alv_data INDEX lv_changed-row_id. IF sy-subrc = 0. MOVE lv_changed-value TO v_alv_data-cust_name. MODIFY i_alv_data FROM v_alv_data INDEX lv_changed-row_id. ENDIF. ENDLOOP. ENDMETHOD. "handle_data_changed ENDCLASS. "lcl_event_receiver IMPLEMENTATION * Reference to the event listener class DATA: event_receiver TYPE REF TO lcl_event_receiver. *---------------------------------------------------------------------* * FORM build_event_listener *---------------------------------------------------------------------* * [+] Set the event handler on the ALV Grid *---------------------------------------------------------------------* FORM build_event_listener. * Assigning the event listener to the ALV CREATE OBJECT event_receiver. SET HANDLER event_receiver->handle_data_changed FOR o_alv_grid. SET HANDLER event_receiver->hotspot_click FOR o_alv_grid. ENDFORM. "build_event_listener *---------------------------------------------------------------------* * AT SELECTION-SCREEN * ON VALUE-REQUEST FOR p_alvvar *---------------------------------------------------------------------* * [+] Calls choose_alv_variant to ask the user to select an alv grid * layout variant *---------------------------------------------------------------------* AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_alvvar. PERFORM choose_alv_variant CHANGING p_alvvar v_alv_variant. *---------------------------------------------------------------------* * START_OF_SELECTION *---------------------------------------------------------------------* START-OF-SELECTION. PERFORM get_data. SET PF-STATUS 'ALVSCREEN' IMMEDIATELY. CALL SCREEN 2000. *---------------------------------------------------------------------* * FORM get_data *---------------------------------------------------------------------* * [+] Gets the data for the ALV grid *---------------------------------------------------------------------* FORM get_data. SELECT kunnr name1 FROM kna1 INTO CORRESPONDING FIELDS OF TABLE i_alv_data. ENDFORM. "get_data *---------------------------------------------------------------------* * MODULE build_alv_grid *---------------------------------------------------------------------* * * THIS SHOULD BE IN THE "PROCESS BEFORE OUTPUT" OF THE ALV SCREEN * * [+] Builds the ALV Grid objects * [+] Calls to build the field catalog table * [+] Loads the field catalog table into the ALV Grid * [+] Loads the table data into the ALV Grid *---------------------------------------------------------------------* MODULE build_alv_grid OUTPUT. SET TITLEBAR '2000'. * Also enables layout saving PERFORM set_alv_variant USING p_alvvar CHANGING v_alv_variant. * Building the grid and container on the screen * NOTE: the container name MUST be upper-case * Also, we don't want the objects to be created if in batch mode! IF sy-batch IS INITIAL. CREATE OBJECT o_alv_cont EXPORTING container_name = 'O_ALV_TABLE'. ENDIF. CREATE OBJECT o_alv_grid EXPORTING i_parent = o_alv_cont. * builds the event listener PERFORM build_event_listener. * Color the cells PERFORM color_cells. * Build the field catalog PERFORM build_alv_fc. * Loads the data into the grid CALL METHOD o_alv_grid->set_table_for_first_display EXPORTING i_save = 'A' i_default = 'X' is_variant = v_alv_variant is_layout = v_alv_layout CHANGING it_outtab = i_alv_data it_fieldcatalog = i_alv_fc. ENDMODULE. "build_alv_grid OUTPUT *---------------------------------------------------------------------* * FORM build_alv_fc *---------------------------------------------------------------------* * [+] Constructs the ALV Grid field catalog table *---------------------------------------------------------------------* FORM build_alv_fc. CLEAR i_alv_fc. REFRESH i_alv_fc. * NOTE: the field name MUST be upper-case * field heading hide hot * name zero spot PERFORM: alv_field USING 'CUST_ID' 'Cust ID' ' ' ' ', alv_field USING 'CUST_NAME' 'Customer Name' ' ' ' '. ENDFORM. "build_alv_fc *---------------------------------------------------------------------* * FORM alv_field *---------------------------------------------------------------------* * [+] Describes and constructs a single field for the ALV Grid field * catalog. The field length and type are both obtained from the * actual field passed in to this method. * [+] Adds the constructed field to the ALV Grid field catalog table *---------------------------------------------------------------------* FORM alv_field USING p_field_name TYPE c p_heading TYPE c p_hide_zeros TYPE c p_hotspot TYPE c. CLEAR v_alv_fc. DATA: lv_type(1) TYPE c, lv_length TYPE i, lv_heading_length TYPE i. * get the type and length of this field FIELD-SYMBOLS . ASSIGN p_field_name TO . DESCRIBE FIELD TYPE lv_type OUTPUT-LENGTH lv_length. * re-adjust the length to the length of the header, if too short lv_heading_length = STRLEN( p_heading ). IF lv_length < lv_heading_length. lv_length = lv_heading_length. ENDIF. * NOTE: the field name MUST be upper-case v_alv_fc-fieldname = p_field_name. TRANSLATE v_alv_fc-fieldname TO UPPER CASE. v_alv_fc-inttype = lv_type. v_alv_fc-outputlen = lv_length. v_alv_fc-coltext = p_heading. v_alv_fc-seltext = p_heading. v_alv_fc-hotspot = p_hotspot. * Determining which fields should show zeros IF p_hide_zeros = 'X'. v_alv_fc-no_zero = 'X'. v_alv_fc-lzero = ' '. ELSE. v_alv_fc-no_zero = ' '. v_alv_fc-lzero = 'X'. ENDIF. * Add the field to the field catalog APPEND v_alv_fc TO i_alv_fc. ENDFORM. "alv_field *---------------------------------------------------------------------* * FORM choose_alv_variant *---------------------------------------------------------------------* * [+] Shows a popup that allows the user to choose the layout variant * for the alv grid of the current program * [+] Usually called by an AT SELECTION-SCREEN method. *---------------------------------------------------------------------* FORM choose_alv_variant CHANGING p_variant_name TYPE disvariant-variant p_variant TYPE disvariant. CLEAR p_variant. DATA: p_exit_check(1) TYPE c. MOVE sy-repid TO p_variant-report. CALL FUNCTION 'LVC_VARIANT_F4' EXPORTING is_variant = p_variant i_save = 'A' IMPORTING e_exit = p_exit_check es_variant = p_variant EXCEPTIONS not_found = 1 OTHERS = 99. IF sy-subrc = 0. IF p_exit_check <> 'X'. p_variant_name = p_variant-variant. ENDIF. ENDIF. ENDFORM. "choose_alv_variant *---------------------------------------------------------------------* * FORM set_alv_variant *---------------------------------------------------------------------* * [+] Sets the alv grid layout variant. Used for setting the variant * when its name is entered in a parameter rather than by using the * popup, or when loading the variant from a variable of type C *---------------------------------------------------------------------* FORM set_alv_variant USING p_variant_name TYPE disvariant-variant CHANGING p_variant TYPE disvariant. MOVE sy-repid TO p_variant-report. p_variant-variant = p_variant_name. ENDFORM. "set_alv_variant *---------------------------------------------------------------------* * FORM color_cells *---------------------------------------------------------------------* * [+] Loop through the data and apply coloring *---------------------------------------------------------------------* FORM color_cells. * tell the ALV grid what field in v_alv_data contains color information v_alv_layout-ctab_fname = 'color'. * loop through each row of the table LOOP AT i_alv_data INTO v_alv_data. * clear the variables CLEAR: v_alv_color, v_alv_color_cell, i_alv_color. REFRESH: i_alv_color. * apply the colors IF v_alv_data-cust_name = 'BOB'. PERFORM color_cell USING 'CUST_ID' 6. "negative ELSEIF v_alv_data-cust_name = 'FRED'. PERFORM color_cell USING 'CUST_ID' 5. "positive ENDIF. * set the color data for this table row v_alv_data-color = i_alv_color. MODIFY i_alv_data FROM v_alv_data. ENDLOOP. ENDFORM. "color_cells *---------------------------------------------------------------------* * FORM color_cell *---------------------------------------------------------------------* * [+] Colors a cell in the ALV grid *---------------------------------------------------------------------* FORM color_cell USING p_cellname TYPE c p_color TYPE i. CLEAR: v_alv_color_cell, v_alv_color. * set the color for the cell IF p_color = 0. v_alv_color_cell-col = cl_gui_resources=>list_col_background. ELSEIF p_color = 1. v_alv_color_cell-col = cl_gui_resources=>list_col_heading. ELSEIF p_color = 2. v_alv_color_cell-col = cl_gui_resources=>list_col_normal. ELSEIF p_color = 3. v_alv_color_cell-col = cl_gui_resources=>list_col_total. ELSEIF p_color = 4. v_alv_color_cell-col = cl_gui_resources=>list_col_key. ELSEIF p_color = 5. v_alv_color_cell-col = cl_gui_resources=>list_col_positive. ELSEIF p_color = 6. v_alv_color_cell-col = cl_gui_resources=>list_col_negative. ELSEIF p_color = 7. v_alv_color_cell-col = cl_gui_resources=>list_col_group. ENDIF. v_alv_color-nokeycol = 'X'. v_alv_color-fname = p_cellname. v_alv_color-color = v_alv_color_cell. APPEND v_alv_color TO i_alv_color. ENDFORM. "color_cell *---------------------------------------------------------------------* * FORM event_hotspot_click *---------------------------------------------------------------------* * [+] What to do when clicking on a hotspot in the ALV Grid *---------------------------------------------------------------------* FORM event_hotspot_click USING p_row TYPE lvc_s_row p_column TYPE lvc_s_col. DATA: lv_docnum TYPE kunnr. READ TABLE i_alv_data INTO v_alv_data INDEX p_row-index. IF p_column = 'CUST_ID'. * call a transaction when the cust_id is clicked SET PARAMETER ID 'AUN' FIELD v_alv_data-cust_id. CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN. ENDIF. ENDFORM. "event_hotspot_click