SALV Table 14 – Editable Checkbox

By | January 18, 2010 | SALV Table, SALV Tutorial, Tutorials | 23,788 | 5

Editable Checkbox in the SALV table

In the series of the SALV Model Table display in SAP ABAP, today we will see how to get the editable checkbox in ALV Grid. You can find all the Previous discussion at Tutorials > SALV Table Display .

To get the selectable (editable) checkbox, we need to get the specific column from the column object. After this, we need to set the cell type as IF_SALV_C_CELL_TYPE=>CHECKBOX_HOTSPOT by using the method SET_CELL_TYPE. To update the values in the checkbox, we need to handle the event LINK_CLICK. This event LINK_CLICK is triggered when we click on the hotspot enabled checkbox. In the event handler method, we need to change the value of the checkbox field and call the REFRESH method to refresh the value on the ALV.

*&---------------------------------------------------------------------*
*& SALV Table, editable checkbox
*&
*&---------------------------------------------------------------------*
REPORT  zsalv_editable_checkbox.
*
*----------------------------------------------------------------------*
*       CLASS lcl_report DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_report DEFINITION.
*
  PUBLIC SECTION.
*
*   Final output table
    TYPESBEGIN OF ty_vbak,
           vbeln TYPE vbak-vbeln,
           erdat TYPE erdat,
           auart TYPE auart,
           kunnr TYPE kunnr,
           check TYPE flag,
           END   OF ty_vbak.
*
    DATAt_vbak TYPE STANDARD TABLE OF ty_vbak.
*
*   ALV reference
    DATAo_alv TYPE REF TO cl_salv_table.
*
    METHODS:
*     data selection
      get_data,
*
*     Generating output
      generate_output.
*
*
ENDCLASS.                    "lcl_report DEFINITION
*
CLASS lcl_event_handler DEFINITION.
*
  PUBLIC SECTION.
    METHODS:
      on_link_click FOR EVENT link_click OF cl_salv_events_table
        IMPORTING row column.
*
ENDCLASS.                    "lcl_event_handler DEFINITION
*
*
START-OF-SELECTION.
  DATAlo_report TYPE REF TO lcl_report.
*
  CREATE OBJECT lo_report.
*
  lo_report->get_data).
*
  lo_report->generate_output).
*
*----------------------------------------------------------------------*
*       CLASS lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_report IMPLEMENTATION.
*
  METHOD get_data.
*   data selection
    SELECT vbeln erdat auart kunnr
           INTO  TABLE t_vbak
           FROM  vbak
           UP TO 20 ROWS.
*
  ENDMETHOD.                    "get_data
*
*.......................................................................
  METHOD generate_output.
*
* exception class
    DATAlx_msg TYPE REF TO cx_salv_msg.
    TRY.
        cl_salv_table=>factory(
          IMPORTING
            r_salv_table o_alv
          CHANGING
            t_table      t_vbak ).
      CATCH cx_salv_msg INTO lx_msg.
    ENDTRY.
*
*...Get all the Columns
    DATAlo_cols TYPE REF TO cl_salv_columns.
    lo_cols o_alv->get_columns).
*
*   set the Column optimization
    lo_cols->set_optimize'X' ).
*
*...Process individual columns
    DATAlo_column TYPE REF TO cl_salv_column_list.
*
*   Change the properties of the Columns KUNNR
    TRY.
        lo_column ?= lo_cols->get_column'CHECK' ).
        lo_column->set_cell_typeif_salv_c_cell_type=>checkbox_hotspot ).
        lo_column->set_output_length10 ).
      CATCH cx_salv_not_found.                          "#EC NO_HANDLER
    ENDTRY.
*
*   Get the event object
    DATAlo_events TYPE REF TO cl_salv_events_table.
    lo_events o_alv->get_event).
*
*   Instantiate the event handler object
    DATAlo_event_handler TYPE REF TO lcl_event_handler.
    CREATE OBJECT lo_event_handler.
*
*   event handler
    SET HANDLER lo_event_handler->on_link_click FOR lo_events.
*
*   Displaying the ALV
*   Here we will call the DISPLAY method to get the output on the screen
    o_alv->display).
*
  ENDMETHOD.                    "generate_output
*
ENDCLASS.                    "lcl_report IMPLEMENTATION
*
*----------------------------------------------------------------------*
*       CLASS lcl_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_event_handler IMPLEMENTATION.
*
  METHOD on_link_click.
*
*   Get the value of the checkbox and set the value accordingly
*   Refersh the table
    FIELD-SYMBOLS<lfa_data> LIKE LINE OF lo_report->t_vbak.
    READ TABLE lo_report->t_vbak ASSIGNING <lfa_data> INDEX row.
    CHECK sy-subrc IS INITIAL.
    IF <lfa_data>-check IS INITIAL.
      <lfa_data>-check 'X'.
    ELSE.
      CLEAR <lfa_data>-check.
    ENDIF.
    lo_report->o_alv->refresh).
  ENDMETHOD.                    "on_link_click
*
ENDCLASS.                    "lcl_event_handler IMPLEMENTATION


Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{272 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 272 articles.

Load comments

5 Comments

  • Anonymous

    Really nice example.

    Thank you very much for your posts, here and on SDN.

  • Anonymous

    how to add a select all button to check all item?

  • Anonymous

    Once you select items using check box, where do you handle the lines selected in the program. For example deleted document for the checked item in ALV.

  • Hello,

    You asked:

    Once you select items using check box, where do you handle the lines selected in the program. For example deleted document for the checked item in ALV.

    You can handle that in the event handler method of the event LINK_CLICK e.g. on_link_click. You would know which is selected by using parameter ROW.

    Regards,
    Naimesh Patel

  • Anonymous

    How do you save the entries that was entered in the checkbox/es?

    I created a custom PF-STATUS and enabled the save button but I don't know how to put a code behind the function code 'SAVE'.

    Please let me know how to do this. Thanks!

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.