Adding checkboxes to columns
In this recipe, we will see how we can display checkboxes in a column in the ALV output. The user may select a checkbox and switch it on or off. Then, based on his or her selection, a button may be pressed and function executed. The primary emphasis of this recipe will be the display of checkboxes within a column and the coding to set it as on or off.
Getting ready
For this recipe, we will make a copy of the simple ALV program that was created in the beginning of the chapter. We will then add the checkbox-related coding to this copy.
How to do it...
For adding a checkbox column to your ALV program, follow these steps:
- Add a new field
CHECKBOX
to the typeTY_PA0008
defined in your program. The position of the field will determine the position of this column in the ALV display. We will place it at the end. - Then, use the
get_column
method to get access to theCHECKBOX
column. We will set the long , medium, and short texts of this column asCheckbox
. The most important step is to set the cell type of this column to a checkbox using the methodset_cell_type
. The constant static attributecheckbox_hotspot
of the interfaceif_salv_c_cell_type
is passed. - A new class
mycheckbox
is defined, the definition of which contains a static methodon_click_checkbox
defined for thelink_click
event for the ALV events. This method imports the row and column of the user selection. - Next, the implementation of the
mycheckbox
method is created. As already mentioned, this method is triggered when the user clicks on a particular checkbox for any row of the displayed ALV table. Theread
statement is used to determine which particular row's checkbox has been clicked. TheIF
statement checks whether the checkbox field of the row in consideration is already on or off (meaning equal to'X'
or space). Depending on the current value, the value of the checkbox field is changed. AMODIFY
statement is used to change the internal tableIT_PA0008
. Finally, therefresh
method of the ALV object is called. - The
SET HANDLER
statement is used to register the static methodon_click_checkbox
method of themycheckbox
class for the ALV events.
How it works...
When the program is run, the set_cell_type
method results in the CHECKBOX
column to be displayed with editable checkboxes. The set_short_text
, set_medium_text
, and set_long_text
methods display the heading of this column as Checkbox.
When the user clicks a particular checkbox, the on_click_checkbox
method is executed. The READ
statement gets the selected row using the parameter row
. If the value of the CHECKBOX
field of the selected row is found to be initial (meaning, checkbox off), it is assigned 'X'
(that is, switched on). Otherwise, if it is already on, the CHECKBOX
field is cleared (that is, switched off).
The table IT_PA0008
that is linked to ALV display is modified in order to reflect the user's selection. Finally, the refresh
method is called to display the new state of the checkboxes on the user screen.
There's more...
Apart from the CHECKBOX
display, there are other possibilities of a particular column. For the set_cell_type
method, the various possible values supplied for the value
parameter and the relevant output is shown as follows: