Gyan Factory

Gyan Factory
SAP Technical Project Support

Thursday, January 7, 2016

Editable ALV with save functionality uisng OOABAP

Step1.  The code :
----------------------------------------------
CLASS lcl_event_handle DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS : hndl_data_changed
                                FOR EVENT data_changed
                                OF cl_gui_alv_grid
                                IMPORTING er_data_changed.
ENDCLASS.
CLASS lcl_event_handle IMPLEMENTATION.
  METHOD hndl_data_changed.
    DATA : lt_scarr TYPE TABLE OF scarr.
    FIELD-SYMBOLS : <fs> TYPE any .
    ASSIGN er_data_changed->mp_mod_rows->* TO <fs>.
    lt_scarr = <fs>.
    IF lt_scarr IS NOT INITIAL.
      MODIFY scarr FROM TABLE lt_scarr.
      IF sy-subrc = 0.
        COMMIT WORK.
        MESSAGE 'Database update successful' TYPE 'S'.
      ENDIF.
    ENDIF.

  ENDMETHOD.
ENDCLASS.


CLASS lcl_main DEFINITION.
  PUBLIC SECTION.

    CLASS-DATA : lo_grid   TYPE REF TO cl_gui_alv_grid,
                          lo_cust   TYPE REF TO cl_gui_custom_container,
                          lo_handle TYPE REF TO lcl_event_handle,
                          lt_scarr  TYPE TABLE OF scarr,
                          ls_layout TYPE lvc_s_layo.

    CLASS-METHODS: run,
                               get_data,
                               prepare_layout,
                               display_alv,
                              user_command.

ENDCLASS.


CLASS lcl_main IMPLEMENTATION.
  METHOD run.
    CALL METHOD lcl_main=>get_data.
    CALL METHOD lcl_main=>prepare_layout.
    CALL METHOD lcl_main=>display_alv.
  ENDMETHOD.

  METHOD get_data.
    SELECT * FROM scarr INTO TABLE lt_scarr.
  ENDMETHOD.

  METHOD prepare_layout.
    ls_layout-edit = abap_true.
  ENDMETHOD.

  METHOD display_alv.
    IF lo_cust IS NOT BOUND.
      CREATE OBJECT lo_cust
        EXPORTING
          container_name = 'CONT'.

    ENDIF.

    IF lo_grid IS NOT BOUND.
      CREATE OBJECT lo_grid
        EXPORTING
          i_parent = lo_cust.

    ENDIF.

    IF lo_handle IS NOT BOUND.
      CREATE OBJECT lo_handle.
    ENDIF.

    IF lo_handle IS BOUND.
      SET HANDLER lo_handle->hndl_data_changed FOR lo_grid.
    ENDIF.

    CALL METHOD lo_grid->set_table_for_first_display
      EXPORTING
        i_structure_name = 'SCARR'
        is_layout        = ls_layout
      CHANGING
        it_outtab        = lt_scarr.

  ENDMETHOD.

  METHOD user_command.
    CASE sy-ucomm.
      WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
        LEAVE TO SCREEN 0.
      WHEN 'SAVE'.
        DATA lv_valid.
        DATA lv_refresh.
        CALL METHOD lo_grid->check_changed_data
          IMPORTING
            e_valid = lv_valid.  " Entries are Consistent
      WHEN OTHERS.
    ENDCASE.
  ENDMETHOD.

ENDCLASS.


START-OF-SELECTION.
  CALL SCREEN 0001.
*&---------------------------------------------------------------------*
*&      Module  STATUS_0001  OUTPUT
*&---------------------------------------------------------------------*
MODULE status_0001 OUTPUT.
  SET PF-STATUS 'STATUS'.
  SET TITLEBAR 'TITLE'.
  CALL METHOD lcl_main=>run.
ENDMODULE.
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0001  INPUT
*&---------------------------------------------------------------------*

MODULE user_command_0001 INPUT.
  CALL METHOD lcl_main=>user_command.
ENDMODULE.

--------------------------------------------------------------------------------------------
Step2. Program code with screen shots.






Step2. Double click on the SET PF-STATUS 'STATUS' , and provide below function keys and activate it.


Step3. Double click on the line SET TITLEBAR 'TITLE' and define it.


Step4. The flow logic of the Screen.


Step5. Screen design with custom container.


Step6. Execute the program.


Step7.  The ALV displayed in editable mode.


Step8. The back end table records.


Step9.  Change some text and save it. line no-4,5, 6 & 9.


Step10. The DB table is updated after save.






--------------------------------------------------------------------------------------

No comments:

Post a Comment