Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Insert a table in a dynamic table

Former Member
0 Kudos

I need a dynamic table to display it with an ALV. That works fine with the following code below.

Now I have the requirement to color each cell differently in my ALV.

For that, I need a table with the type LVC_T_SCOL in my dynamic table for my ALV.

How could I add a table? (The code below is only for adding a normal data-element.)

wa_fieldcat_mr-fieldname = 'FIELDNAME'.

wa_fieldcat_mr-datatype  = 'char'.

wa_fieldcat_mr-intlen    = 10.

wa_fieldcat_mr-coltext    = 'coltext'.

APPEND wa_fieldcat_mr TO it_fieldcat_mr.


     CALL METHOD cl_alv_table_create=>create_dynamic_table

       EXPORTING

         it_fieldcatalog = it_fieldcat_mr

       IMPORTING

         ep_table        = t_newtable.

     ASSIGN t_newtable->* TO <t_dyntable>. "now I can use <t_dyntable> as my internal table


"Fill it with data

...


"Display it


* Field that identify cell color in inetrnal table

MOVE 'COLOR_CELL' TO gs_layout_mr-ctab_fname.

CALL METHOD alv_grid_mr->set_table_for_first_display

       EXPORTING

         is_layout                     = gs_layout_mr

       CHANGING

         it_outtab                     = <t_dyntable>

         it_fieldcatalog               = it_fieldcat_mr.

1 ACCEPTED SOLUTION

Chintu6august
Contributor
0 Kudos

Hi,

same query was asked before : create a deep structure for dynamic internal table | SCN

thanks!!

6 REPLIES 6

ziolkowskib
Active Contributor
0 Kudos

Hi Alex,

I do not know the correct answer yet, but definitelly you will not achieve it using method cl_alv_table_create=>create_dynamic_table. That method calls form FB_TABLE_CREATE_STRING and when you review its code you will see there is not such option to create element that would be a table or structure.

Former Member
0 Kudos

Hi Alex,

you can create an internal table with dynamic type using RTTC (Runtime Type Creation).

Check following documentation: CREATE DATA - HANDLE - ABAP Keyword Documentation

I know it's a little tricky if you are not familiar to it, but give it a try.

ziolkowskib
Active Contributor
0 Kudos

Would that work for you?


DATA lo_data_descr TYPE REF TO cl_abap_datadescr.

DATA lo_type_descr TYPE REF TO cl_abap_typedescr.

DATA lo_struct_descr TYPE REF TO cl_abap_structdescr.

DATA lo_tab_descr TYPE REF TO cl_abap_tabledescr.

DATA lt_component TYPE abap_component_tab.

DATA ls_component LIKE LINE OF lt_component[].

DATA lo_data_struct TYPE REF TO data.

DATA lo_data_tab TYPE REF TO data.

FIELD-SYMBOLS <ls_table> TYPE any.

FIELD-SYMBOLS <lt_table> TYPE STANDARD TABLE.

lo_type_descr ?= cl_abap_typedescr=>describe_by_name( p_name = 'LVC_T_SCOL' ).

ls_component-name = 'ID'.

ls_component-type = cl_abap_elemdescr=>get_c( p_length = 5 ).

APPEND ls_component TO lt_component[].

ls_component-name = 'TABLE'.

ls_component-type ?= lo_type_descr.

APPEND ls_component TO lt_component[].

lo_struct_descr ?= cl_abap_structdescr=>create( lt_component[] ).

CREATE DATA lo_data_struct TYPE HANDLE lo_struct_descr.

ASSIGN lo_data_struct->* TO <ls_table>.

lo_data_descr ?= lo_struct_descr.

lo_tab_descr ?= cl_abap_tabledescr=>create( lo_data_descr ).

CREATE DATA lo_data_tab TYPE HANDLE lo_tab_descr.

ASSIGN lo_data_tab->* TO <lt_table>.

rosenberg_eitan
Active Contributor
0 Kudos

Hi,

Another sample RTTC (I think I found it in SCN long time ago ) .

FORM test_07 .

* Create a table with LVC_T_SCOL for ALV .

  DATA: it_component TYPE cl_abap_structdescr=>component_table .

  DATA: st_component LIKE LINE OF it_component .

  DATA: ob_abap_structdescr TYPE REF TO cl_abap_structdescr,

        ob_abap_tabledescr  TYPE REF TO cl_abap_tabledescr,

        r_data_tab    TYPE REF TO data,

        r_data_str    TYPE REF TO data.

  st_component-name = 'IT_SCOL'.

  st_component-type ?= cl_abap_elemdescr=>describe_by_name( 'LVC_T_SCOL' ).

  APPEND st_component TO it_component .

  st_component-name = 'CARRID'.

  st_component-type ?= cl_abap_elemdescr=>describe_by_name( 'S_CARR_ID' ).

  APPEND st_component TO it_component .

  st_component-name = 'CONNID'.

  st_component-type ?= cl_abap_elemdescr=>describe_by_name( 'S_CONN_ID' ).

  APPEND st_component TO it_component .

  st_component-name = 'FLDATE'.

  st_component-type ?= cl_abap_elemdescr=>describe_by_name( 'S_DATE' ).

  APPEND st_component TO it_component .

  st_component-name = 'SEATSMAX_B'.

  st_component-type ?= cl_abap_elemdescr=>describe_by_name( 'S_SMAX_B' ).

  APPEND st_component TO it_component .

  st_component-name = 'SEATSOCC_B'.

  st_component-type ?= cl_abap_elemdescr=>describe_by_name( 'S_SOCC_B' ).

  APPEND st_component TO it_component .

  TRY.

      ob_abap_structdescr = cl_abap_structdescr=>create( it_component ).

    CATCH cx_sy_struct_creation .

  ENDTRY.

  TRY.

      ob_abap_tabledescr = cl_abap_tabledescr=>create( ob_abap_structdescr ).

    CATCH cx_sy_table_creation .

  ENDTRY.

  CREATE DATA: r_data_tab TYPE HANDLE ob_abap_tabledescr ,

               r_data_str TYPE HANDLE ob_abap_structdescr .

  FIELD-SYMBOLS: <it_data> TYPE INDEX TABLE,

                 <st_data>    TYPE ANY.

  ASSIGN: r_data_tab->* TO <it_data> ,

          r_data_str->* TO <st_data> .

  SELECT * INTO CORRESPONDING FIELDS OF TABLE <it_data>

  FROM sflight UP TO 20 ROWS .

  FIELD-SYMBOLS: <it_scol> TYPE lvc_t_scol .

  DATA: st_scol LIKE LINE OF <it_scol> .

  DATA: ob_abap_random_2 TYPE REF TO cl_abap_random_int .

  ob_abap_random_2 = cl_abap_random_int=>create( min = 1 max = 7 ) .

  LOOP AT <it_data> ASSIGNING <st_data> .

    ASSIGN COMPONENT 'IT_SCOL' OF STRUCTURE <st_data> TO <it_scol>.

    CLEAR <it_scol> .

* Some complex 🙂 logic to decide the color used .

    st_scol-color-col = ob_abap_random_2->get_next( ) .

    st_scol-color-int = 0.

    st_scol-color-inv = 0.

    st_scol-fname = 'SEATSOCC_B' .

    INSERT st_scol INTO TABLE <it_scol> .

  ENDLOOP.

ENDFORM .                                                   "test_07

Chintu6august
Contributor
0 Kudos

Hi,

same query was asked before : create a deep structure for dynamic internal table | SCN

thanks!!

matt
Active Contributor
0 Kudos

Yes. Members should search before posting. Thread locked.