Skip to Content

ALV Factory Method Deletion functionality

Hi Experts,

I am creating an ALV using Factory method. I added a delete button on the ALB tool bar. i need to delete selected rows from the alv.

i know how to achieve this deletion functionality when using reuse_alv_grid_display or cl_gui_alv_grid class. But i dont know how to add this functinality using factory method.

Can any one explain this to me?

In case any one have good examples of factory method alv with various functionalities then please do share.

Thanks n Regards,

Nitanshu

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

3 Answers

  • Best Answer
    Posted on Nov 05, 2015 at 07:15 AM

    Hi!

    In ALV OO model it is pretty simple.

    Here is an example. It is not suitable for copy and paste, but it is good for understanding.

    CLASS lcl_app DEFINITION CREATE PRIVATE FINAL.

    PUBLIC SECTION.

    PRIVATE SECTION.

    CONSTANTS: btn_delete TYPE salv_de_function VALUE 'BTN_DELETE'.

    DATA: salv_table TYPE REF TO cl_salv_table.

    METHODS:

    METHODS:

    handle_added_function FOR EVENT added_function OF cl_salv_events_table

    IMPORTING

    e_salv_function.

    METHODS:

    handle_btn_delete.

    ENDCLASS.

    CLASS lcl_app IMPLEMENTATION.

    METHOD display_report.

    DATA: lr_events TYPE REF TO cl_salv_events_table.

    TRY.

    cl_salv_table=>factory(

    IMPORTING

    r_salv_table = me->salv_table

    CHANGING

    t_table = me->report_data ).

    CATCH cx_salv_msg .

    ENDTRY.

    * Event handling

    lr_events = me->salv_table->get_event( ).

    SET HANDLER me->handle_added_function FOR lr_events.

    me->salv_table->display( ).

    ENDMETHOD.

    METHOD handle_added_function.

    CASE e_salv_function.

    WHEN btn_delete.

    me->handle_btn_delete( ).

    ENDCASE.

    ENDMETHOD.

    METHOD handle_btn_delete.

    DATA: lr_selections TYPE REF TO cl_salv_selections,

    lt_sel_rows TYPE salv_t_row,

    lv_sel_row LIKE LINE OF lt_sel_rows,

    lv_n_rows TYPE i,

    lv_answer TYPE c LENGTH 1.

    lr_selections = me->salv_table->get_selections( ).

    lt_sel_rows = lr_selections->get_selected_rows( ).

    lv_n_rows = lines( lt_sel_rows ).

    CASE lv_n_rows.

    WHEN 1. " ok

    WHEN OTHERS.

    MESSAGE ... " Please select exactly 1 row!

    ENDCASE.

    * Get the record to delete

    READ TABLE lt_sel_rows INTO lv_sel_row INDEX 1.

    IF sy-subrc <> 0.

    MESSAGE " something goes wrong!

    ENDIF.

    READ TABLE me->report_data INTO ls_del_rpt_rec INDEX lv_sel_row.

    IF sy-subrc <> 0.

    MESSAGE ... " something is wrong!

    ENDIF.

    * Delete record from the table

    delete me->report_data index lv_sel_row.

    * Refresh ALV

    me->salv_table->refresh( refresh_mode = if_salv_c_refresh=>full ).

    ENDMETHOD.

    ENDCLASS.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Nov 05, 2015 at 07:34 AM

    Here is another, copy-and-paste example.

    1. Copy into your report a standard GUI status for ALV.

    2. Add into GUI status in your program a button "DELETE" as depicted here:

    3. Report text:


    REPORT z_test_19.




    CLASS lcl_app DEFINITION .

    PUBLIC SECTION.

    METHODS: display_report.

    PRIVATE SECTION.

    CONSTANTS: btn_delete TYPE salv_de_function VALUE 'BTN_DELETE'.


    DATA: salv_table TYPE REF TO cl_salv_table,
    report_data TYPE TABLE OF sflight.

    METHODS:
    handle_added_function FOR EVENT added_function OF cl_salv_events_table
    IMPORTING
    e_salv_function.

    METHODS:
    handle_btn_delete.

    ENDCLASS.


    CLASS lcl_app IMPLEMENTATION.


    METHOD display_report.

    DATA: lr_events TYPE REF TO cl_salv_events_table.

    SELECT *
    FROM sflight
    INTO TABLE me->report_data.

    TRY.
    cl_salv_table=>factory(
    IMPORTING
    r_salv_table = me->salv_table
    CHANGING
    t_table = me->report_data ).
    CATCH cx_salv_msg .

    ENDTRY.

    * Event handling
    lr_events = me->salv_table->get_event( ).
    SET HANDLER me->handle_added_function FOR lr_events.


    me->salv_table->set_screen_status(
    report = sy-repid
    pfstatus = 'SALV_TABLE_STANDARD'
    set_functions = cl_salv_table=>c_functions_all
    ).

    me->salv_table->display( ).

    ENDMETHOD.

    METHOD handle_added_function.

    CASE e_salv_function.


    WHEN btn_delete.
    me->handle_btn_delete( ).
    ENDCASE.

    ENDMETHOD.


    METHOD handle_btn_delete.

    DATA: lr_selections TYPE REF TO cl_salv_selections,
    lt_sel_rows TYPE salv_t_row,
    lv_sel_row LIKE LINE OF lt_sel_rows,
    lv_n_rows TYPE i,
    lv_answer TYPE c LENGTH 1.

    lr_selections = me->salv_table->get_selections( ).
    lt_sel_rows = lr_selections->get_selected_rows( ).

    lv_n_rows = lines( lt_sel_rows ).
    CASE lv_n_rows.
    WHEN 1. " ok
    WHEN OTHERS.
    MESSAGE 'there is an error!' TYPE 'E'.
    ENDCASE.

    * Get the record to delete
    READ TABLE lt_sel_rows INTO lv_sel_row INDEX 1.
    IF sy-subrc <> 0.
    MESSAGE 'there is an error!' TYPE 'E'.
    ENDIF.



    * Delete record from the table
    DELETE me->report_data INDEX lv_sel_row.


    * Refresh ALV
    me->salv_table->refresh( refresh_mode = if_salv_c_refresh=>full ).


    ENDMETHOD.

    ENDCLASS.



    END-OF-SELECTION.

    DATA: gr_app TYPE REF TO lcl_app.

    CREATE OBJECT gr_app.
    gr_app->display_report( ).





    4. Enjoy your 'DELETE' button :-)


    pastedImage_1.png (13.1 kB)
    pastedImage_3.png (15.0 kB)
    pastedImage_1.png (18.3 kB)
    Add a comment
    10|10000 characters needed characters exceeded

    • Hi!

      No, you can't.

      But you can rewrite the method as following:

      METHOD handle_btn_delete.

      DATA: lr_selections TYPE REF TO cl_salv_selections,
      lt_sel_rows TYPE salv_t_row,
      lv_sel_row LIKE LINE OF lt_sel_rows,
      lv_n_rows TYPE i,
      lv_answer TYPE c LENGTH 1,
      lt_rows_to_del TYPE TABLE OF sflight.

      FIELD-SYMBOLS: <fs_selected_row> LIKE LINE OF me->report_data,
      <fs_row_index> LIKE LINE OF lt_sel_rows.

      lr_selections = me->salv_table->get_selections( ).
      lt_sel_rows = lr_selections->get_selected_rows( ).

      lv_n_rows = lines( lt_sel_rows ).
      CASE lv_n_rows.
      WHEN 0. " ok
      MESSAGE 'there is an error!' TYPE 'E'.
      WHEN OTHERS.
      " ok
      ENDCASE.

      * Get the record to delete
      LOOP AT lt_sel_rows ASSIGNING <fs_row_index>.
      READ TABLE me->report_data ASSIGNING <fs_selected_row> INDEX <fs_row_index>.
      CHECK sy-subrc = 0.
      APPEND <fs_selected_row> TO lt_rows_to_del.
      ENDLOOP.

      * Delete record from the table
      LOOP AT lt_rows_to_del ASSIGNING <fs_selected_row>.
      DELETE me->report_data WHERE carrid = <fs_selected_row>-carrid AND
      connid = <fs_selected_row>-connid AND
      fldate = <fs_selected_row>-fldate.
      ENDLOOP.

      * Refresh ALV
      me->salv_table->refresh( refresh_mode = if_salv_c_refresh=>full ).


      ENDMETHOD.

  • Posted on Nov 06, 2015 at 07:36 PM

    Hi, Nitanshu! Is your question answered? ;-)

    Add a comment
    10|10000 characters needed characters exceeded

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.