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: 

dynamic columns adding in alv

Former Member
0 Kudos

Hi all,

I have an existing ZALV report whic is developed by Function modules (not the OO -ALV).

now i have to add few dynamic columns to the existing report. is it possible to add in the existing report?

i have searched in SDN, i have few with oo-alv, if possible to with function modules , can any one send me a sample code?

Thanks

Vimala

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi, Please find the below code

type-pools: slis. " ALV Global Types*data declaration for dynamic internal table and alv

DATA: l_structure TYPE REF TO data,

l_table TYPE REF TO data,

struc_desc TYPE REF TO cl_abap_structdescr,

lt_layout TYPE slis_layout_alv,

ls_lvc_fieldcatalogue TYPE lvc_s_fcat,

lt_lvc_fieldcatalogue TYPE lvc_t_fcat,

ls_fieldcatalogue TYPE slis_fieldcat_alv,

lt_fieldcatalogue TYPE slis_t_fieldcat_alv.

*field symbols declaration

field-symbols :

<it_table> type standard table,

<dyn_str> type any,

<str_comp> type abap_compdescr.

*declarations for grid title

data : t1(30),

t2(10),

t3(50).

*selection screen declaration for table input

parameters : p_table like dd02l-tabname.

*initialization event

initialization.

*start of selection event

start-of-selection.

*texts for grid title

t1 = 'Dynamic ALV display for table'.

t2 = p_table. CONCATENATE t1 t2 INTO t3 SEPARATED BY space.

  • dynamic creation of a structure

create data l_structure type (p_table).

ASSIGN l_structure->* TO <dyn_str>.

  • fields structure

struc_desc ?= cl_abap_typedescr=>describe_by_data( <dyn_str> ).

LOOP AT struc_desc->components ASSIGNING <str_comp>.

  • Build Fieldcatalog

ls_lvc_fieldcatalogue-fieldname = <str_comp>-name.

ls_lvc_fieldcatalogue-ref_table = p_table.

APPEND ls_lvc_fieldcatalogue TO lt_lvc_fieldcatalogue.

  • Build Fieldcatalog

ls_fieldcatalogue-fieldname = <str_comp>-name.

ls_fieldcatalogue-ref_tabname = p_table.

APPEND ls_fieldcatalogue TO lt_fieldcatalogue.

ENDLOOP.

  • Create internal table dynamic

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = lt_lvc_fieldcatalogue

IMPORTING

ep_table = l_table.

ASSIGN l_table->* TO <it_table>.

  • Read data from the table selected.

SELECT * FROM (P_TABLE)

into corresponding fields of table <it_table>.

  • ALV Layout

lt_layout-zebra = 'X'.

lt_layout-colwidth_optimize = 'X'.

lt_layout-window_titlebar = t3.

*ALV output

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

is_layout = lt_layout

it_fieldcat = lt_fieldcatalogue

TABLES

t_outtab = <it_table>

EXCEPTIONS

program_error = 1

OTHERS = 2.

IF sy-subrc <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

5 REPLIES 5

Former Member
0 Kudos

put all fields in your final internal table.

u can create the field catalog dynamically.

and as per the field catalog dynamic columns will be displayed.

hope this helps

Former Member
0 Kudos

Hello,

We can do it using normal ALV.

Use this FM

CALL METHOD cl_alv_table_create=>create_dynamic_table

First we need to find out the total number of columns.

Keep the loop to the number of columsn and create one fieldcatalog.

Pass that field catalog to the call function.

Refer this link.

https://www.sdn.sap.com/irj/scn/directforumsearch?threadid=&q=dynamicANDinternalANDtable&objID=f50&dateRange=all&numResults=15

Former Member
0 Kudos

Hi, Please find the below code

type-pools: slis. " ALV Global Types*data declaration for dynamic internal table and alv

DATA: l_structure TYPE REF TO data,

l_table TYPE REF TO data,

struc_desc TYPE REF TO cl_abap_structdescr,

lt_layout TYPE slis_layout_alv,

ls_lvc_fieldcatalogue TYPE lvc_s_fcat,

lt_lvc_fieldcatalogue TYPE lvc_t_fcat,

ls_fieldcatalogue TYPE slis_fieldcat_alv,

lt_fieldcatalogue TYPE slis_t_fieldcat_alv.

*field symbols declaration

field-symbols :

<it_table> type standard table,

<dyn_str> type any,

<str_comp> type abap_compdescr.

*declarations for grid title

data : t1(30),

t2(10),

t3(50).

*selection screen declaration for table input

parameters : p_table like dd02l-tabname.

*initialization event

initialization.

*start of selection event

start-of-selection.

*texts for grid title

t1 = 'Dynamic ALV display for table'.

t2 = p_table. CONCATENATE t1 t2 INTO t3 SEPARATED BY space.

  • dynamic creation of a structure

create data l_structure type (p_table).

ASSIGN l_structure->* TO <dyn_str>.

  • fields structure

struc_desc ?= cl_abap_typedescr=>describe_by_data( <dyn_str> ).

LOOP AT struc_desc->components ASSIGNING <str_comp>.

  • Build Fieldcatalog

ls_lvc_fieldcatalogue-fieldname = <str_comp>-name.

ls_lvc_fieldcatalogue-ref_table = p_table.

APPEND ls_lvc_fieldcatalogue TO lt_lvc_fieldcatalogue.

  • Build Fieldcatalog

ls_fieldcatalogue-fieldname = <str_comp>-name.

ls_fieldcatalogue-ref_tabname = p_table.

APPEND ls_fieldcatalogue TO lt_fieldcatalogue.

ENDLOOP.

  • Create internal table dynamic

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = lt_lvc_fieldcatalogue

IMPORTING

ep_table = l_table.

ASSIGN l_table->* TO <it_table>.

  • Read data from the table selected.

SELECT * FROM (P_TABLE)

into corresponding fields of table <it_table>.

  • ALV Layout

lt_layout-zebra = 'X'.

lt_layout-colwidth_optimize = 'X'.

lt_layout-window_titlebar = t3.

*ALV output

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

is_layout = lt_layout

it_fieldcat = lt_fieldcatalogue

TABLES

t_outtab = <it_table>

EXCEPTIONS

program_error = 1

OTHERS = 2.

IF sy-subrc <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

Former Member
0 Kudos

While defining the Field Catalog mention things as shown below: And add all the fields to the internal table whichever you want to display..

CLEAR g_t_fcat.

g_t_fcat-fieldname = 'FLD_1'.

g_t_fcat-tabname = 'G_T_DATA'.

g_t_fcat-seltext_l = 'Field 1'.

APPEND g_t_fcat TO g_t_fcat.

CLEAR g_t_fcat.

g_t_fcat-fieldname = 'FLD_2'.

g_t_fcat-tabname = 'G_T_DATA'.

g_t_fcat-seltext_l = 'Field 2'.

g_t_fcat-no_out = 'X'.

APPEND g_t_fcat TO g_t_fcat.

no_out field stands for not to be displayed on ALV on first go..

then once u display ALV for basic fields and you want to add the new fields to ALV use the button 'Change Layout' To dynamically change the layout of ALV...

Hope this helps

Regards,

Kanchan

Madhurivs23
Participant
0 Kudos

Generally use the grid display since, sometimes the list display FM gives error


*    CREATE THE DYNAMIC INTERNAL TABLE
  data: NEW_TABLE type ref to DATA,
        NEW_LINE type ref to DATA.


  clear WA_FLDCAT.
  WA_FLDCAT-FIELDNAME   = 'CUOBJ_BM'.
  WA_FLDCAT-DATATYPE    = 'CHAR'.
  WA_FLDCAT-INTLEN      = 18.
  WA_FLDCAT-DD_OUTLEN   = 18.
  WA_FLDCAT-SELTEXT     = 'Int.obj.no'.
  append WA_FLDCAT to IT_FLDCAT .


*    CREATE DYNAMIC INTERNAL TABLE AND ASSIGN TO FS
  call method CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
    exporting
      IT_FIELDCATALOG = IT_FLDCAT
    importing
      EP_TABLE        = NEW_TABLE.

  assign NEW_TABLE->* to <DYN_TABLE>.

*    CREATE DYNAMIC WORK AREA AND ASSIGN TO FS
  create data NEW_LINE like line of <DYN_TABLE>.
  assign NEW_LINE->* to <DYN_WA>.



* filling the dynamic internal table
   assign component 'VAL_CODE' of structure <DYN_WA> to <FS1>.
    <FS1> = WA_BTCH_VALUATION_SUMM-VAL_CODE.
append <DYN_WA> to <DYN_TABLE>.

displaying the dynamic ALV

  loop at IT_FLDCAT1 into WA_FLDCAT1.
    FIELDCAT_LN-FIELDNAME = WA_FLDCAT1-FIELDNAME.
    FIELDCAT_LN-SELTEXT_M = WA_FLDCAT1-SELTEXT.
    FIELDCAT_LN-SELTEXT_L = WA_FLDCAT1-SCRTEXT_L.
    FIELDCAT_LN-OUTPUTLEN = WA_FLDCAT1-DD_OUTLEN.
    FIELDCAT_LN-DDICTXT = 'L'.
    append FIELDCAT_LN to FIELDCAT.
  endloop.

  call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
      I_CALLBACK_PROGRAM      = V_REPID
      I_CALLBACK_TOP_OF_PAGE  = 'TOP_OF_PAGE'
      I_CALLBACK_USER_COMMAND = 'USER_COMMAND_1'
      IS_LAYOUT               = GS_LAYOUT_DTL
      I_GRID_TITLE            = I_TITLE_DETAILS
      IT_FIELDCAT             = FIELDCAT
      I_SAVE                  = 'A'
      IS_PRINT                = GS_PRINT
    tables
      T_OUTTAB                = <DYN_TABLE>
    exceptions
      PROGRAM_ERROR           = 1
      others                  = 2.