ALV Grid for In-Line Declaration

2019-06-13 18:29发布

问题:

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?

回答1:

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.



回答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.



标签: sap abap alv