Skip to Content
author's profile photo Former Member
Former Member

How to write a generic alv class to handle ALV operations

Hi All,

Sometimes for alv display we use cl_gui_alv_grid that involves creating field catalog ,creating alv objects,settings layout and setting events.I am planning to move all this code to a class so any report which needs this kind of functionality will just call this class and everything will be handled inside the class that will make program more readable,less number of code lines and more importantly OO approach.I started with field catalog and creating alv and setting layout into separate methods but I am stuck with event handling because generally we declare a local class and use subroutines for different events.How to handle this scenario in this generic class and also how to move the subroutines code (for events) in to this generic class.Event handling code should be generic because i want to display ALV either with PO data or SO data and I want to have interactive actions on those fields.Please point me in the right direction


Bala Duvvuri

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Best Answer
    Posted on May 23, 2013 at 08:40 AM

    Hello Bala,

    You should consider using SALV OM which exactly does what you've been designing on your own - why re-invent the wheel? 😭

    But if your ALV needs to be editable, then you can can't (damn typo) use SALV 😔



    PS - Refer to SALV*DEMO* programs for further details

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on May 23, 2013 at 09:21 AM

    Hi Former Member ,

    I'm using My own class like you mentioned. But in report I'm inheriting local class from global class. and redefine method as i need.

    For event I Think you have two options.

    1) in global class create method for events: just click

    and then define event

    in local program redefine this method

    2) in local class

    write your own method, and assign manualy to your alv object.

    class lcl_alv definition inheriting from zcl_alv_global final.




    METHODS : handle_data_changed

    FOR EVENT data_changed

    OF cl_gui_alv_grid

    IMPORTING er_data_changed





    METHODS : set_handler.






    class lcl_alv implementation.

    METHOD handle_data_changed.

    DATA : ls_good_cells TYPE lvc_s_modi.

    LOOP AT er_data_changed->mt_good_cells

    INTO ls_good_cells.


    ENDMETHOD. "handle_data_changed

    METHOD set_handler.

    SET HANDLER me->handle_data_changed

    FOR o_alv.

    o_alv->register_edit_event( cl_gui_alv_grid=>mc_evt_enter ).

    o_alv->register_edit_event( cl_gui_alv_grid=>mc_evt_modified ).

    ENDMETHOD. "set_handler


    But for this you have to redefine it, because it can change for every report.


    Button.PNG (1.1 kB)
    Method.PNG (11.4 kB)
    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on May 23, 2013 at 09:36 AM


    See the code below. It is a form that you can call in any program with any table and the corrsponding work area. Not everything is possible, but a lot is done with this.

    The header names are taken from the names of the structure. So if you use internal tables declared with your own types, you can control the header names easily.

    The handle doubleclick is commented out, because you will need to declare the definition and implementation in your program.

    You can just use this FORM anywhere for all tables and pass the table and the corresponding ork area (mandatory) and an optional title and call it like:

    PERFORM WRITE_RESULT Using Tablename



    FORM write_result USING lt_table TYPE ANY TABLE
    wa_table TYPE any
    ls_title type string.

    * data declaraties
    DATA: gr_table TYPE REF TO cl_salv_table,
    gr_columns_table TYPE REF TO cl_salv_columns_table,
    gr_column TYPE REF TO cl_salv_column,
    gr_display_settings TYPE REF TO cl_salv_display_settings,
    * gr_events TYPE REF TO cl_salv_events_table,
    gr_functions TYPE REF TO cl_salv_functions,

    DATA: lv_name TYPE lvc_fname,
    lv_text TYPE scrtext_s,
    lv_textm TYPE scrtext_m,
    lv_textl TYPE scrtext_l,
    lv_tip TYPE lvc_tip,

    lv_title type lvc_title.

    l_rcl_struc TYPE REF TO cl_abap_structdescr.

    * DATA: lr_event_handler TYPE REF TO lcl_event_handler.

    CONSTANTS: gc_true VALUE 'X'.

    FIELD-SYMBOLS: <components> TYPE abap_compdescr.

    * show result on screen
    CALL METHOD cl_salv_table=>factory
    * list_display = IF_SALV_C_BOOL_SAP=>FALSE
    * r_container =
    * container_name =
    r_salv_table = gr_table
    t_table = lt_table.

    CATCH cx_salv_msg .

    * standard buttons and function codes

    gr_functions = gr_table->get_functions( ).
    gr_functions->set_all( gc_true ).

    * ALV title

    lv_title = ls_title.

    CALL METHOD gr_display_settings->set_list_header
    value = lv_title.

    * adjust column texts, first get all columns
    gr_columns_table = gr_table->get_columns( ).
    gr_columns_table->set_optimize( 'X' ).

    * now adjust per column,
    l_rcl_struc ?= cl_abap_structdescr=>describe_by_data( wa_table ).

    LOOP AT l_rcl_struc->components ASSIGNING <components>.
    lv_name = <components>-name.
    gr_column = gr_columns_table->get_column( columnname = lv_name ).
    gr_column->set_optimized( 'X' ).
    lv_text = lv_textm = lv_textl = lv_tip = lv_name.
    gr_column->set_short_text( lv_text ).
    gr_column->set_medium_text( lv_textm ).
    gr_column->set_long_text( lv_textl ).
    gr_column->set_tooltip( lv_tip ).
    CATCH cx_salv_not_found .

    CATCH cx_salv_not_found .

    * gr_events = gr_table->get_event( ).
    * CREATE OBJECT lr_event_handler.
    * SET HANDLER lr_event_handler->handle_doubleclick FOR gr_events.

    * show the result
    gr_table->display( ).


    Add a comment
    10|10000 characters needed characters exceeded

    • interactive action handling should be handled in the program where i want to display alv though a global class can be created and inherited by local class in program?

      You could do that or define and implement a local class in your program like e.g (for a list with orders and action double_click).

      * CLASS lcl_event_handler DEFINITION
      CLASS lcl_event_handler DEFINITION.


      METHODS: handle_doubleclick FOR EVENT double_click
      OF cl_salv_events_table
      IMPORTING row column.

      ENDCLASS. "lcl_event_handler DEFINITION

      * CLASS lcl_event_handler IMPLEMENTATION
      CLASS lcl_event_handler IMPLEMENTATION.

      METHOD handle_doubleclick.

      READ TABLE lt_aufk INTO wa_aufk INDEX row.
      IF sy-subrc = 0.

      SET PARAMETER ID 'BR1' FIELD wa_aufk-aufnr.



      ENDMETHOD. "handle_doubleclick
      ENDCLASS. "lcl_event_handler IMPLEMENTATION

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.