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:

  1. Add a new field CHECKBOX to the type TY_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.
    How to do it...
  2. Then, use the get_column method to get access to the CHECKBOX column. We will set the long , medium, and short texts of this column as Checkbox. The most important step is to set the cell type of this column to a checkbox using the method set_cell_type. The constant static attribute checkbox_hotspot of the interface if_salv_c_cell_type is passed.
    How to do it...
  3. A new class mycheckbox is defined, the definition of which contains a static method on_click_checkbox defined for the link_click event for the ALV events. This method imports the row and column of the user selection.
    How to do it...
  4. 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. The read statement is used to determine which particular row's checkbox has been clicked. The IF 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. A MODIFY statement is used to change the internal table IT_PA0008. Finally, the refresh method of the ALV object is called.
    How to do it...
  5. The SET HANDLER statement is used to register the static method on_click_checkbox method of the mycheckbox class for the ALV events.
    How to do it...

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.

How it works...

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:

Value Paramter of set_cell_type

Output

0

TEXT

1

CHECKBOX

2

Button

3

DROPDOWN

4

Link

5

Hotspot

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.167.195