Setting ALV columns as key columns and making zero amount appear as blank
This recipe is comprised of two subrecipes:
We will see how the personnel number (PERNR
) column may be set as key with blue colour. In addition, how a particular column Amount, where a zero amount is shown as 0.00, may be made to appear blank.
How to do it...
For making the above adjustments, proceed as follows:
- Declare a column variable with reference to the class
cl_salv_column_table
. - Next, use the
get_column
method of themycolumns
object pointing to the columns of the ALV in order to get the reference to the columnPERNR
. We will then use theset_key
method to set thePERNR
column as the key column. - Similarly, the
get_column
method is called for the Amount columnBET01
. Theset_zero
method is called to convert the zeros to blanks while outputting theBET01
column. It is necessary that the value space (' '
) is passed to theset_zero
method. - The code fragment was added before the ALV display method but after the assignment to
mycolumns
statement followed by thefactory
methodcall
.
How it works...
We already had the reference to the entire set of ALV columns within the variable mycolumns
. For each of the two columns, PERNR
and BET01
, the single column object reference was required. This was done through usage of the get_column
method. Methods set_key
and set_zero
were then called and appropriate values passed for parameters (if necessary) for setting the key column and removal of zeros respectively.
Any exception raised due to wrong name supplied to the get_column
method is caught using the TRY .. ENDTRY
and CATCH
statements. The exception that was addressed was CX_SALV_NOT_FOUND
. We can add a suitable MESSAGE
statement after the catch
statement to output a message in case an error arises, which is not been shown in the previous screenshot:
Note the change in the PersNo. and the Amount column.