Button: The cell type of cl_gui_alv_grid=>mc_style_button resembles as a button in the user interface of the SAP ALV list. As the user clicks this button, any event can be executed like opening up a new window.
Dropdown List: It is used to provide a list of values as a dropdown list.
IMPLEMENTATION OF BUTTON:
The following steps are necessary to implement the Button
Extend the Internal table with the additional field of type LVC_T_STYL.
Set the fields fieldname, style of lvc_t_styl to each row of the ALV List. The column is specified in the fieldname.
The stylename field of the layout is assigned with the field style component.
Extension of Internal table with Fieldstyle component.
TYPES: BEGIN OF ty_contact.
INCLUDE STRUCTURE ztt_db_table2.
TYPES: fieldstyle TYPE lvc_t_styl.
END OF ty_contact.
Set the column in the SAP ALV List with the field style ‘cl_gui_alv_grid=>mc_style_button’.
It is used to provide a list of values as a Drop down List.
There are three important steps involved in setting the cell as a drop down list.
Filling the internal table lvc_t_dral.
Setting the fields drdn_hndl, drdn_alias of field catalog.
Call the method set_drop_down_table of the ALV object.
Filling the internal table lvc_t_dral.
Handle: The field Handle holds the value, which can be used to refer to the value mentioned in the field drdn_hndl of field catalog. Therefore it connects to the field catalog.
Value: It is considered as the Display name. It holds the value to be used in the Drop down list. If the field DRDN_ALIAS of the field catalog is initial, then this value is considered for the Drop down list.
INT_VALUE: It is considered as the Technical name. It also holds the value to be displayed in the Drop down List. If the field DRDN_ALIAS of the field catalog is ‘X’, then the value from the int_value field is used in the Drop down list.
Let us consider the following example:
DATA lt_dral TYPE lvc_t_dral.
DATA ls_dral TYPE lvc_s_dral.
ls_dral-handle = 3.
ls_dral-value = ‘Street1_value’.
ls_dral-int_value = ‘Street1_int_value’.
APPEND ls_dral TO lt_dral.
ls_dral-value = ‘Street2_value’.
ls_dral-int_value = ‘Street2_int_value’.
APPEND ls_dral TO lt_dral.
Setting the ‘DRDN’ fields of Field Catalog.
FIELD-SYMBOLS <ls_field_cat> TYPE lvc_s_fcat.
LOOP AT lt_field_cat ASSIGNING <ls_field_cat> WHERE fieldname = ‘CONTACT ADDRESS’.
<ls_field_cat> – drdn_hndl = 3.
<ls_field_cat> – drdn_alias = ‘X’.
ENDLOOP.
In the above field catalog assignment, the field ‘DRDN_ALIAS’ is set to ‘X’, which means the value of field ‘INT_VALUE’ is considered in the Drop down list. The value of field ‘VALUE’ is ignored. If the field ‘DRDN_ALIAS’ is initial like <ls_field_cat> – drdn_alias = ‘ ‘, Then the value of field ‘VALUE’ is considered in the Drop down list.
Passing the internal table values lvc_t_dral to the ALV object.
The method set_drop_down_table of the ALV object is used to set the drop down values (Internal table) to the partical column ‘CONTACT_ADDRESS’ of the ALV List.
The column names can be changed with the help of the fields REPTEXT, SCRTEXT_L, SCRTEXT_M and SCRTEXT_L of field catalog. If you wish to use only text of field ‘scrtext_l’, then fill the text only in this field ‘SCRTEXT_L’. The other texts can be ignored as the ALV takes the filled text automatically.
FIELD-SYMBOLS: <ls_field_cat> TYPE lvc_s_fcat.
LOOP AT lt_field_cat ASSIGNING <ls_field_cat> WHERE fieldname = ‘CONTACT_ADDRESS’.
The class CL_GUI_ALV_GRID of the old ALV model provides different events. It is available in the Tab EVENTS of the class CL_GUI_ALV_GRID.
Event
Parameter
Description
BUTTON_CLICK
E_ROW -> Row E_COLUMN -> Column E_ROW_NO -> Row id
It is triggered when a button is clicked.
USER_COMMAND
E_UCOMM -> Function code
It is triggered when a function is triggered,
DOUBLE_CLICK
E_ROW -> Row E_COLUMN -> Column E_ROW_NO -> Row Id
It is triggered when a cell of the ALV Table is double clicked.
HOT_SPOT_CLICK
E_ROW -> Row E_COLUMN -> Column E_ROW_NO -> Row Id
It is triggered when a Hotspot is clicked.
TOOLBAR
E_OBJECT -> It is an object of type CL_ALV_EVENT_TOOLBAR_SET
It is triggered when any entity on the toolbar is clicked.
Events of class CL_GUI_ALV_GRID
The implementation of the above events is similar for all events. It involves definition of Eventhandler, registering the Event handler with the SET HANDLER command.
The implementation of Double click event is shown below
CLASS zcl_handler DEFINITION PUBLIC.
PUBLIC SECTION.
CLASS_METHODS double_click_method FOR EVENT double_click
OF cl_gui_alv_grid IMPORTING e_row e_column es_row_no.
ENDCLASS.
CLASS zcl_handler IMPLEMENTATION.
METHOD double_click_method.
CALL FUNCTION ‘POPUP_TO_CONFIRM’
EXPORTING
titlebar = ‘Double Click occurred’
text_question = e_column-fieldname
text_button_1 = ‘Yes’
text_button_2 = ‘No’.
ENDMETHOD.
ENDCLASS.
Registration of Double Click event through SET HANDLER
SET HANDLER zcl_handler => double_click_method FOR lr_gui_alv_grid.
The class CL_GUI_ALV_GRID provides three methods to register events.
REGISTER_DELAYED_EVENT ( ) : If the cell of the ALV table or the line of record is changed, then this event can be used.
REGISTER_EDIT_EVENT ( ) :
If the data in the ALV is changed, then this event can be triggered automatically. The key benefit here is user does not have to click the ‘ENTER’ Button explicitly.
Let us look at this event with an example:
The edit event is registered with the help of the REGISTER_EDIT_EVENT method of the ALV object like below.
The parameter i_event_id is set to mc_evt_modified. By setting this event_id, the variable m_cell_edit is set explicitly to ‘X’. Even if the user forgets to click the enter key, then this variable m_cell_edit is set automatically.
REGISTER_F4_FOR_FIELDS ( ) :
If the F4- Value help for a field is clicked, then this event can be used.
In this blog post, we discuss about the events of the class CL_GUI_ALV_GRID. The old SAP ALV model provides standard functions such as summing, sorting, search function, Excel Export function etc. It also enables us to add new functions to the SAP ALV toolbar.
The Event TOOLBAR of the class CL_GUI_ALV_GRID is used to add functions to the toolbar of the SAP ALV List.
The following steps are needed in order to add a Button.
Define the Event handler.
Register the Event handler for the event.
Activate the event through the method SET_TOOLBAR_INTERACTIVE()
Definition of Event Handler.
The Event handler is defined in the static method of the class like below.
CLASS zcl_handler DEFINITION PUBLIC.
PUBLIC SECTION.
CLASS-METHODS st_select_all_method FOR EVENT toolbar
OF cl_gui_alv_grid IMPORTING e-object.
ENDCLASS.
CLASS zcl_handler IMPLEMENTATION.
METHOD st_select_all_method.
DATA ls_button_toolbar TYPE stb_button.
ls_button_toolbar-function = ‘SELALL’.
ls_button_toolbar-icon = icon-select-all.
ls_button_toolbar-quickinfo = ‘SELECT ALL function’.
INSERT ls_button_toolbar INTO e_object -> mt_toolbar INDEX 2.
ENDMETHOD.
ENDCLASS.
2. Event handler Registration.
The Event handler is registered using the ‘SET HANDLER … FOR … ‘ command.
An example below shows the registration.
SET HANDLER zcl_handler => st_select_all_method FOR lr_gui_alv_grid.
3. Activation of Event.
As a last step, the event must be activated through the method SET_TOOLBAR_INTERACTIVE. This ensures that the event is triggered from ALV. It is the method of the ALV object.
The event handler registration and event activation code section appears between the ALV table call ‘SET_TABLE_FOR_FIRST_DISPLAY’ and the ‘CALL SCREEN’ of Dynpro call.
The Event handler uses the parameter e_object in order to insert the button entry into the internal table mt_toolbar. The parameter E_OBJECT is of type CL_ALV_EVENT_TOOLBAR_SET.
The primary key fields of the data records must be unique. There should not be duplicate records in the Database table. The fields of the ALV List are editable through the field catalog. The primary key fields of the SAP ALV List cannot be edited directly. If a new line of record is inserted in to SAP ALV List, the primary key fields of the SAP ALV List are disabled by default. It is not possible to insert a new record in the ALV List through the edit field of the field catalog.
Contact_Id
Contact_name
Contact_address
101
Ram
Street 10
102
Mahes
Street 11
If a new line of record is inserted in to above SAP ALV List, the field contact_id is disabled by default. The contact_id is the primary key field of the SAP ALV List. If a new line of record must be inserted in the Table, then the primary key fields of the SAP ALV List must be enabled for editing. The primary key fields of the existing data records must be disabled by default.
A new line of record can be inserted in to the ALV Table through the internal table lvc_t_styl. It is introduced to the structure of the internal table of data records. The keywords ‘INCLUDE STRUCTURE’ adds the existing structure of internal table which contains the data records. The following steps helps us to achieve this.
Define the structure with lvc_t_styl.
Define the stylename field of the layout
Define the style field of lvc_s_styl to CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.
Define the structure with lvc_t_styl.
TYPES: BEGIN OF st_contact.
INCLUDE STRUCTURE ztt_db_table2.
TYPES: fieldstyle TYPE lvc_t_styl,
END OF st_contact.
DATA lt_contact TYPE TABLE OF st_contact.
Define the stylename field of the layout
DATA ls_layout TYPE lvc_s_layo.
ls_layout-stylename = ‘FIELDSTYLE’.
This layout is given as parameter while calling the set_table_for_first_display method of the ALV object.
Define the style field of lvc_s_styl to CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.
This style is defined for the primary key fields of the database table. This style is set to each data record of the database table. But it is constrained to specific columns by specifying the fieldname of the data records. It is finally inserted in to the field style of each data record.
Key steps to set the style field are:
a) Define the fieldname of structure lvc_s_styl.
b) Define the style of structure lvc_s_styl.
c) Insert the structure lvc_s_styl in the fieldstyle field of each data record. Fieldstyle field is the internal table of type lvc_t_styl.
The selection of the style cl_gui_alv_grid=>mc_style_disabled blocks the fields of the respective column in editing mode. For example in the above table, the editing of column CONTACT_ID is deactivated.
INSERT ls_style_record INTO TABLE <ls_contact>-fieldstyle.
ENDLOOP.
The primary key fields of the table will now be available for insertion of new record. The insert button facilitates insertion of new record. The primary key fields of the ALV List for already existing data records are disabled for editing.