The standard ALV toolbar provides a number of useful functions. However, depending on the requirement, you may be asked to add new buttons to the ALV toolbar. This recipe will show how to add your own buttons to the ALV toolbar and then writing appropriate coding to be executed when the user presses the button.
We will add a new button saying View Summary to the toolbar. Upon clicking, the total number of displayed records will be shown (for purpose of illustration).
For creating your own toolbar buttons, we need to make a copy of the GUI status displayed in the original ALV program. We will then make changes to the copied status. Proceed as follows:
ALV_TABLE_STANDARD
residing in the program having name SAPLSALV_METADATA_STATUS
.SE80
.In the next section, we will see how new buttons are added.
We will see how to add new buttons and adding appropriate coding. Proceed as follows.
SE80
to see the various components of your program. The newly copied status SALV_TABLE_STANDARD is shown under the GUI Status node.set_screen_status
of the class cl_salv_table
and pass it the name of our program SY-REPID
and the newly created status SALV_TABLE_STANDARD
.summbutton
(we can also use the existing class for hotspot created in a previous Adding hotspots to columns recipe). In the definition, a static method on_button_press
is defined that responds to the triggering of the ALV event added_function
. The method has an importing parameter e_salv_function
that provides the function code of the selected customer function.on_button_press
method, we check to see if the SUMM
function button has been pressed. If found true, we then calculate the number of lines in the table IT_PA0008
, then concatenate the line numbers with appropriate text and display in an information message.SET HANDLER
statement to link the static method on_button_press
of the class newbutton
with the events object myevents
. This will make sure the clicking on the New button triggers the execution of the on_button_press
method.Calling the
set_screen_status
method results in the display of our newly created GUI status having the button View Summary. The method is called in order to make sure that instead of the standard GUI status, our newly created GUI Status is shown.
The CL_SALV_EVENTS_TABLE class contains an ADDED_FUNCTION event that is raised when our added button is pressed.
We registered this event with the static method on_button_press
of our class newbutton
using SET HANDLER
statement.
Upon clicking the View Summary button, the code of the on_button_press
method is called. We make sure in the method that the code is run only when the function code supplied by importing variable e_salv_function
contains SUMM
. If that is the case, the number of lines determined in the internal table are displayed using a MESSAGE
statement.
For adding icons in the toolbar button, refer to the SAP documentation at http://help.sap.com/saphelp_nw04/helpdata/en/d1/801d43454211d189710000e8322d00/frameset.htm.
The work so far done looks fine, but has a small problem. Clicking on the View Summary button will give the entire set of rows in the internal table IT_PA0008
, irrespective of taking into account any filter applied.
We will now refine the recipe in order to read the filters, the column names included on which the filters have been applied, and the selection options specifying the filter values. At the end, we will delete the rows from IT_PA0008
that do not adhere to the filter criteria (so that the row count is correct). The example may then be refined later for deletion of filters, and so on.
The code that will be added will be within the IF
statement (checking the function code) just before the DESCRIBE
statement. The code is divided into three parts:
get_filters
method in order to read the filter objects.get
method of the cl_salv_filters
class is then called in order to fetch the internal table myfilters_tab
specifying the column names on which filter has been specified. The R_FILTER component of this table row is a reference to the class CL_SALV_FILTER
, which contains the values, entered at the filter screen.r_filter
object are fetched.myfilters_tab
method and get the filter conditions object for each column. The get_sign
, get_option
, get_low
, and get_high
methods of the class cl_SALV_SELOPT
is used for getting the sign, option, low, and high values of the filter condition respectively. These are added to the range
table of the final_range_struc
. Finally, the final_range_struc
contents are inserted into the internal table final_range_table
. The purpose of this step is to form a final range table named final_range_table
, which will provide us with the name of each column specified in the filter definition, along with the filter values in the form of range
table.IT_PA0008
internal table those records that violate any of the filter criteria.Once this additional code is added, the internal table IT_PA0008
will take into account any applied filter. Thus, the correct values will be shown by the View Summary button.
The code shown may be written more efficiently and elegantly using field symbols. Since the table contains fewer entries and for the sake of simplicity, field symbols were not used.
3.144.15.43