0

I have a program which displays a simple table based on the class CL_SALV_TABLE, and I'd like to position the column CURRCODE at the fifth position, instead of 4 currently (default order defined in database table SCARR), as shown here:

Simple ALV output with column to be moved

How can I do that?

Here is the current source code (compiles with ABAP version 7.40) :

  SELECT * FROM scarr INTO TABLE @DATA(scarr_s).

  cl_salv_table=>factory(
    IMPORTING
      r_salv_table = DATA(salv)
    CHANGING
      t_table      = scarr_s ).

  salv->display( ).

NB: if you want to reproduce and the table SCARR is empty, execute the program SAPBC_DATA_GENERATOR to fill it.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48

1 Answers1

1

You have to do it in two steps, before the call of the method display :

  • Call the method GET_COLUMNS of the ALV instance (of class CL_SALV_TABLE), to get the instance of the class CL_SALV_COLUMNS_TABLE corresponding to all columns.

  • The latter class has a method SET_COLUMN_POSITION to change the position of a given column whose name is passed as an argument.

Here is the source code:

  SELECT * FROM scarr INTO TABLE @DATA(scarr_s).

  cl_salv_table=>factory(
    IMPORTING
      r_salv_table = DATA(salv)
    CHANGING
      t_table      = scarr_s ).

  salv->get_columns( )->set_column_position( columnname = 'CURRCODE' position = 5 ). " <== ADD THIS LINE

  salv->get_columns( )->set_optimize( ).
  salv->display( ).

Result:

Simple ALV output with column moved as expected

NB: I also use the method SET_OPTIMIZE so that the width of all columns is automatically adjusted to their contents (but it's not related to the question).

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48