SAP ABAP Advanced Cookbook
上QQ阅读APP看书,第一时间看更新

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:

  1. Declare a column variable with reference to the class cl_salv_column_table.
  2. Next, use the get_column method of the mycolumns object pointing to the columns of the ALV in order to get the reference to the column PERNR. We will then use the set_key method to set the PERNR column as the key column.
  3. Similarly, the get_column method is called for the Amount column BET01. The set_zero method is called to convert the zeros to blanks while outputting the BET01 column. It is necessary that the value space (' ') is passed to the set_zero method.
    How to do it...
  4. The code fragment was added before the ALV display method but after the assignment to mycolumns statement followed by the factory method call.

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:

How it works...

Note the change in the PersNo. and the Amount column.