0

How do you display an in-line declared data type in an ALV grid?

eg:

SELECT *
INTO TABLE @DATA(lt_result)
FROM table.

How can the lt_result be displayed in an ALV grid?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Isuru
  • 3,044
  • 6
  • 30
  • 39
  • 1
    Read the documentation? Doesn't seem like you've even tried. – Bryan Cain Jan 19 '18 at 14:03
  • 1
    @BryanCain Somewhat agree about reading documentation, but also there are many ways to create an alv grid and maybe OP was led down a bad path. – Brian Jan 19 '18 at 16:28
  • @BryanCain I did read the documentation, but I had trouble figuring out how to do this for dynamic type that was a result of multiple table joins. More of what I found was that the type had to declared and then called. – Isuru Jan 22 '18 at 05:54

2 Answers2

8

Here is a basic example:

DATA: alv TYPE REF TO cl_salv_table.   

SELECT *
INTO TABLE @DATA(lt_result)
FROM table.

cl_salv_table=>factory( IMPORTING r_salv_table = alv
                        CHANGING  t_table      = lt_result ).

alv->display( ).

You can find other examples using the SALV Object Model in package SALV_OM_OBJECTS.

This is a more modern approach than using 'REUSE_ALV_GRID_DISPLAY' and you will not need to define a field catalog.

Brian
  • 385
  • 2
  • 12
2

You have to do the same thing regardless of how you created lt_result. A select * as in your example will result in lt_result being equal to if you did DATA lt_result type table of tablename

In this case you can send in the name of the structure. But this only works if the structure type is defined in SE11, i.e. if you do a select * without any joins or aliases.

Otherwise you have to create and send in a field catalog with all the fields in lt_result you wish to display.

Example:

SELECT * FROM mara
UP TO 10 ROWS
INTO TABLE @DATA(lt_mara).

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
  EXPORTING
    i_callback_program = sy-repid
    i_structure_name   = 'MARA'
*    it_fieldcat  => use this if i_structure_name is not sufficient
  TABLES
    t_outtab           = lt_mara
  EXCEPTIONS
    OTHERS             = 1.

The same thing applies if you use cl_gui_alv_grid.

Edit: You can fill the field-catalog dynamically like this:

DATA:
  lo_t_struct TYPE REF TO cl_abap_tabledescr,
  lo_struct   TYPE REF TO cl_abap_structdescr.

lo_t_struct ?= cl_abap_tabledescr=>describe_by_data( lt_result ).
lo_struct ?= lo_t_struct->get_table_line_type( ).

LOOP AT lo_struct->components ASSIGNING FIELD-SYMBOL(<comp>).
  "Fill a range-table with <comp>-name
ENDLOOP.

Use the range table to get field-descriptions from table dd04t.

Then loop at lo_struct->components again, and fill the field catalog with fieldname and description. Here you can also add special logic for any given fields.

  • the issue with this approach is that the lt_result table is a result of table joins, so the type would not be be defined in SE11. Manually creating the filed catalog is an option, but is looking for a more dynamic approach – Isuru Jan 22 '18 at 05:57