abapalv

Read ALV changes after user input?


I've got a report which outputs the data of my internal table via an ALV grid. The output itself consists of some information and two check boxes for each row. The user can check these boxes if necessary and now I need to read the table back in order to know what boxes were checked. The corresponding rows will be processed differently afterwards depending on which of the two boxes got checked.

I already tried the method get_actual_view, which I don't know how to use correct and the method get_selected_rows, which seems to get the index of the row selected by the user, but not its contents.

How can I read the table back after the user checked the boxes (and press a button to continue, which would trigger the coding in the report to read the data, process it and write it back into the grid)?


Solution

  • You need to call the method CHECK_CHANGED_DATA of CL_GUI_ALV_GRID to transfer the inputs from the ALV grid to the internal table (it works for all kinds of input fields in the ALV, i.e. not limited to checkboxes).

    Minimal example (add a breakpoint before/after CHECK_CHANGED_DATA, run the program, edit some data, for instance the smoker checkbox, and see how the input is reflected into the internal table; NB: if the demo table SBOOK is empty, run the program SAPBC_DATA_GENERATOR). The program compiles with ABAP 7.40 or above. NB: click here to get explanations about the dummy parameter and CL_GUI_CONTAINER=>SCREEN0).

    REPORT.
    DATA go_alv TYPE REF TO cl_gui_alv_grid.
    DATA gt_sbook TYPE TABLE OF sbook.
    
    PARAMETERS dummy.
    
    AT SELECTION-SCREEN OUTPUT.
      DATA: lt_fcat TYPE lvc_t_fcat,
            ls_fcat TYPE lvc_s_fcat.
      IF go_alv IS NOT BOUND.
        CREATE OBJECT go_alv
          EXPORTING
            i_parent = cl_gui_container=>screen0.
        SELECT * FROM sbook UP TO 100 ROWS INTO TABLE gt_sbook.
        lt_fcat = CORRESPONDING #( CAST cl_abap_structdescr(
              cl_abap_structdescr=>describe_by_name( 'SBOOK' ) )->get_ddic_field_list( ) ).
        ls_fcat-checkbox = abap_true.
        MODIFY lt_fcat FROM ls_fcat TRANSPORTING checkbox WHERE fieldname = 'SMOKER'.
        go_alv->set_table_for_first_display(
            EXPORTING
              is_layout        = VALUE #( edit = 'X' )
            CHANGING
              it_fieldcatalog  = lt_fcat
              it_outtab        = gt_sbook ).
      ENDIF.
    
    AT SELECTION-SCREEN ON EXIT-COMMAND.
      go_alv->check_changed_data( ). " <=== transfer screen data to GT_SBOOK
      go_alv->free( ).