Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to regenerate additional rows after user clicks enter?

Former Member
0 Kudos

Hi all,

I am outputing the data through internal table(contains two fields 'Country' and 'Test')using ALV Classes.

My requirement is user can add another row.And

after appending another row user will enter country(say India) against the country

field and clicks enter.Then the program has to fetch all the tests(say 'test1' and 'test2' are there

for India) described for India from the database table and refresh the screen

with this additonal 2 rows(India, Test1 and India,Test2).

Can anyone please let me know how to do it?Remember I am using ALV Classes.

Thanks,

Balaji.

8 REPLIES 8

Former Member
0 Kudos

Hi Balaji,

I think ALV is not the best way to do this kind of requirements. ALV is for reporting purpose. So, i suggest you to use Dialog Programming with Table Control.

Regards,

Satish

0 Kudos

Satish,

Thanks for your reply.Can you please provide me with the code how to do using module pool and table control?

Thanks,

Balaji

uwe_schieferstein
Active Contributor
0 Kudos

Hello Balaji

The sample report <b>ZUS_SDN_ALVGRID_EDITABLE_7</b> shows you how to implement your requirements using ALV lists. Procedure:

1. Add a new row.

2. Enter value '003' in column "Sort Key" of new row.

3. Push ENTER button.

The report will add two additional rows to the ALV list and modify the first added row:

- '003' & 'K1'

- '003' & 'K2'

- '003' & 'K3'

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_ALVGRID_EDITABLE_7
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zus_sdn_alvgrid_editable_7.


TYPE-POOLS: abap.

DATA:
  gd_repid         TYPE syrepid,
  gd_okcode        TYPE ui_func,
*
  gs_layout        TYPE lvc_s_layo,
  gt_fcat          TYPE lvc_t_fcat,
  go_docking       TYPE REF TO cl_gui_docking_container,
  go_grid          TYPE REF TO cl_gui_alv_grid.


TYPES: BEGIN OF ty_s_outtab.
INCLUDE TYPE knb1.
TYPES: END OF ty_s_outtab.
types: ty_t_outtab    type standard table of ty_s_outtab
                      with default key.

DATA:
**  gs_outtab        TYPE ty_s_outtab,
  gt_outtab        TYPE STANDARD TABLE OF ty_s_outtab
                   WITH DEFAULT KEY.



*---------------------------------------------------------------------*
*       CLASS lcl_eventhandler DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_eventhandler DEFINITION.

  PUBLIC SECTION.

    CLASS-METHODS:
      handle_data_changed
        FOR EVENT data_changed OF cl_gui_alv_grid
        IMPORTING
          er_data_changed
          e_onf4
          e_onf4_before
          e_onf4_after
          e_ucomm
          sender,

      handle_data_changed_finished
        FOR EVENT data_changed_finished OF cl_gui_alv_grid
        IMPORTING
          e_modified
          et_good_cells,

      handle_user_command
        FOR EVENT user_command OF cl_gui_alv_grid
        IMPORTING
          e_ucomm,


      handle_toolbar
        FOR EVENT toolbar OF cl_gui_alv_grid
        IMPORTING
          e_object
          e_interactive.


ENDCLASS.                    "lcl_eventhandler DEFINITION


*---------------------------------------------------------------------*
*       CLASS lcl_eventhandler IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_eventhandler IMPLEMENTATION.

  METHOD handle_data_changed.
*   define local data

    cl_gui_cfw=>set_new_ok_code( 'REFRESH' ).

  ENDMETHOD.                    "handle_data_changed

  METHOD handle_data_changed_finished.
*   define local data
    DATA:
      ls_outtab      TYPE ty_s_outtab,
      ls_cell        TYPE lvc_s_modi.


  ENDMETHOD.                    "handle_data_changed_finished

  METHOD handle_user_command.

  ENDMETHOD.                    "handle_user_command

  METHOD handle_toolbar.
*   define local data
    DATA:
      ls_button    TYPE stb_button.

    ls_button-function  = 'DEFAULT'.
    ls_button-icon      = icon_mass_change.
    ls_button-quickinfo = 'Set default value for column'.
    APPEND ls_button TO e_object->mt_toolbar.

  ENDMETHOD.                    "handle_toolbar

ENDCLASS.                    "lcl_eventhandler IMPLEMENTATION




START-OF-SELECTION.


  SELECT * FROM knb1 INTO TABLE gt_outtab UP TO 20 ROWS
    WHERE bukrs = '1000'.





* Create docking container
  CREATE OBJECT go_docking
    EXPORTING
      parent                      = cl_gui_container=>screen0
      ratio                       = 90
    EXCEPTIONS
      OTHERS                      = 6.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* Create ALV grid
  CREATE OBJECT go_grid
    EXPORTING
      i_parent          = go_docking
    EXCEPTIONS
      OTHERS            = 5.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.



* Build fieldcatalog
  PERFORM build_fieldcatalog.

  PERFORM set_layout.

  SET HANDLER:
    lcl_eventhandler=>handle_toolbar               FOR go_grid,
    lcl_eventhandler=>handle_data_changed          FOR go_grid,
    lcl_eventhandler=>handle_data_changed_finished FOR go_grid.



* Display data
  CALL METHOD go_grid->set_table_for_first_display
    EXPORTING
      is_layout       = gs_layout
    CHANGING
      it_outtab       = gt_outtab
      it_fieldcatalog = gt_fcat
    EXCEPTIONS
      OTHERS          = 4.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

  go_grid->set_toolbar_interactive( ).

  CALL METHOD go_grid->register_edit_event
    EXPORTING
      i_event_id = cl_gui_alv_grid=>mc_evt_enter
    EXCEPTIONS
      error      = 1
      OTHERS     = 2.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* Link the docking container to the target dynpro
  gd_repid = syst-repid.
  CALL METHOD go_docking->link
    EXPORTING
      repid                       = gd_repid
      dynnr                       = '0100'
*      CONTAINER                   =
    EXCEPTIONS
      OTHERS                      = 4.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* ok-code field = GD_OKCODE
  CALL SCREEN '0100'.
* Flow logic (no elements on screen):
*  PROCESS BEFORE OUTPUT.
*    MODULE STATUS_0100.
**
*  PROCESS AFTER INPUT.
*    MODULE USER_COMMAND_0100.


END-OF-SELECTION.

*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'STATUS_0100'.
*  SET TITLEBAR 'xxx'.

  CALL METHOD go_grid->refresh_table_display
*      EXPORTING
*        IS_STABLE      =
*        I_SOFT_REFRESH =
    EXCEPTIONS
      finished       = 1
      OTHERS         = 2.
  IF sy-subrc <> 0.
*     MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*                WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

ENDMODULE.                 " STATUS_0100  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.

  TRANSLATE gd_okcode TO UPPER CASE.

* Fetch changes on ALV grid
  go_grid->check_changed_data( ).

  CASE gd_okcode.
    WHEN 'BACK' OR
         'END'  OR
         'CANC'.
      SET SCREEN 0. LEAVE SCREEN.

    WHEN 'REFRESH'.
      PERFORM refresh_outtab.

    WHEN OTHERS.
  ENDCASE.





  CLEAR: gd_okcode.

ENDMODULE.                 " USER_COMMAND_0100  INPUT


*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG_KNB1
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_fieldcatalog .
* define local data
  DATA:
    ls_fcat        TYPE lvc_s_fcat.



  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
*     I_BUFFER_ACTIVE              =
      i_structure_name             = 'KNB1'
*     I_CLIENT_NEVER_DISPLAY       = 'X'
*     I_BYPASSING_BUFFER           =
*     I_INTERNAL_TABNAME           =
    CHANGING
      ct_fieldcat                  = gt_fcat
    EXCEPTIONS
      inconsistent_interface       = 1
      program_error                = 2
      OTHERS                       = 3.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.




  " Set required fields editable
  LOOP AT gt_fcat INTO ls_fcat
                  WHERE ( fieldname = 'ZUAWA'  OR
                          fieldname = 'BUSAB' ).
    ls_fcat-edit    = abap_true.

    MODIFY gt_fcat FROM ls_fcat.
  ENDLOOP.

  DELETE gt_fcat WHERE ( fieldname = 'ZINRT' ).


ENDFORM.                    " BUILD_FIELDCATALOG


*&---------------------------------------------------------------------*
*&      Form  SET_LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM set_layout .

  CLEAR: gs_layout.

  gs_layout-cwidth_opt = abap_true.
  gs_layout-zebra      = abap_true.

**  gs_layout-stylefname = 'CELLTAB'.

ENDFORM.                    " SET_LAYOUT

*&---------------------------------------------------------------------*
*&      Form  REFRESH_OUTTAB
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM refresh_outtab .
* define local data
  DATA:
    ld_idx       TYPE i,
    ls_outtab    TYPE ty_s_outtab,
    ls_tmp       TYPE ty_s_outtab,
    lt_tmp       TYPE ty_t_outtab.

  " Simulate selection of data depending on value of ZUAWA
  ls_tmp-zuawa = '003'.
  ls_tmp-busab = 'K1'.  APPEND ls_tmp TO lt_tmp.
  ls_tmp-busab = 'K2'.  APPEND ls_tmp TO lt_tmp.
  ls_tmp-busab = 'K3'.  APPEND ls_tmp TO lt_tmp.

" Note: should be more elaborated than this simplified refresh routine...
  LOOP AT gt_outtab INTO ls_outtab
                    WHERE ( zuawa = '003'
                    AND     busab IS INITIAL ).
    ld_idx = syst-tabix.

    LOOP AT lt_tmp INTO ls_tmp.
      ls_outtab-busab = ls_tmp-busab.

      IF ( syst-tabix = 1 ).
        MODIFY gt_outtab FROM ls_outtab INDEX ld_idx.
      ELSE.
        APPEND ls_outtab TO gt_outtab.
      ENDIF.
    ENDLOOP.


  ENDLOOP.

ENDFORM.                    " REFRESH_OUTTAB

Regards

Uwe

0 Kudos

Hi,

Thank you very much for your code.I will check and come back to you if there is any help from your side.

Once again thanks a lot.

Thanks,

Balaji

0 Kudos

Hi Uwe,

Can you please execute this program in your ABAP Editor and check it.Its showing lot of errors.Please I need the code ASAP.

Please,I will be waiting for your updated program very very eagerly.

Thanks,

Balaji

0 Kudos

Hello Balaji

The program should run error-free on basis releases >= 6.20. Thus, I assume you are still working on 4.6c.

I have made a few changes which should allow compatibility to release 4.6c. If some errors still remain simply remove one by one.

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_ALVGRID_EDITABLE_7
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zus_sdn_alvgrid_editable_7.


TYPE-POOLS: abap.

include <icon>.  " NOTE: replace by TYPE-POOLS: icon. on >= 6.20

DATA:
  gd_repid         TYPE syrepid,
  gd_okcode        TYPE sy-ucomm,
*
  gs_layout        TYPE lvc_s_layo,
  gt_fcat          TYPE lvc_t_fcat,
  go_docking       TYPE REF TO cl_gui_docking_container,
  go_grid          TYPE REF TO cl_gui_alv_grid.


TYPES: BEGIN OF ty_s_outtab.
INCLUDE TYPE knb1.
TYPES: END OF ty_s_outtab.
types: ty_t_outtab    type standard table of ty_s_outtab
                      with default key.

DATA:
**  gs_outtab        TYPE ty_s_outtab,
  gt_outtab        TYPE STANDARD TABLE OF ty_s_outtab
                   WITH DEFAULT KEY.



*---------------------------------------------------------------------*
*       CLASS lcl_eventhandler DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_eventhandler DEFINITION.

  PUBLIC SECTION.

    CLASS-METHODS:
      handle_data_changed
        FOR EVENT data_changed OF cl_gui_alv_grid
        IMPORTING
          er_data_changed
          e_onf4
          e_onf4_before
          e_onf4_after
          e_ucomm
          sender,

      handle_data_changed_finished
        FOR EVENT data_changed_finished OF cl_gui_alv_grid
        IMPORTING
          e_modified
          et_good_cells,

      handle_user_command
        FOR EVENT user_command OF cl_gui_alv_grid
        IMPORTING
          e_ucomm,


      handle_toolbar
        FOR EVENT toolbar OF cl_gui_alv_grid
        IMPORTING
          e_object
          e_interactive.


ENDCLASS.                    "lcl_eventhandler DEFINITION


*---------------------------------------------------------------------*
*       CLASS lcl_eventhandler IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_eventhandler IMPLEMENTATION.

  METHOD handle_data_changed.
*   define local data

**    cl_gui_cfw=>set_new_ok_code( 'REFRESH' ). " not possible on 4.6c
    CALL METHOD cl_gui_cfw=>set_new_ok_code
      EXPORTING
        new_code = 'REFRESH'
*      IMPORTING
*        RC       =
        .


  ENDMETHOD.                    "handle_data_changed

  METHOD handle_data_changed_finished.
*   define local data
    DATA:
      ls_outtab      TYPE ty_s_outtab,
      ls_cell        TYPE lvc_s_modi.


  ENDMETHOD.                    "handle_data_changed_finished

  METHOD handle_user_command.

  ENDMETHOD.                    "handle_user_command

  METHOD handle_toolbar.
*   define local data
    DATA:
      ls_button    TYPE stb_button.

    ls_button-function  = 'DEFAULT'.
    ls_button-icon      = icon_mass_change.
    ls_button-quickinfo = 'Set default value for column'.
    APPEND ls_button TO e_object->mt_toolbar.

  ENDMETHOD.                    "handle_toolbar

ENDCLASS.                    "lcl_eventhandler IMPLEMENTATION




START-OF-SELECTION.


  SELECT * FROM knb1 INTO TABLE gt_outtab UP TO 20 ROWS
    WHERE bukrs = '1000'.





* Create docking container
  CREATE OBJECT go_docking
    EXPORTING
      parent                      = cl_gui_container=>screen0
      ratio                       = 90
    EXCEPTIONS
      OTHERS                      = 6.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* Create ALV grid
  CREATE OBJECT go_grid
    EXPORTING
      i_parent          = go_docking
    EXCEPTIONS
      OTHERS            = 5.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.



* Build fieldcatalog
  PERFORM build_fieldcatalog.

  PERFORM set_layout.

  SET HANDLER:
    lcl_eventhandler=>handle_toolbar               FOR go_grid,
    lcl_eventhandler=>handle_data_changed          FOR go_grid,
    lcl_eventhandler=>handle_data_changed_finished FOR go_grid.



* Display data
  CALL METHOD go_grid->set_table_for_first_display
    EXPORTING
      is_layout       = gs_layout
    CHANGING
      it_outtab       = gt_outtab
      it_fieldcatalog = gt_fcat
    EXCEPTIONS
      OTHERS          = 4.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

  go_grid->set_toolbar_interactive( ).

  CALL METHOD go_grid->register_edit_event
    EXPORTING
      i_event_id = cl_gui_alv_grid=>mc_evt_enter
    EXCEPTIONS
      error      = 1
      OTHERS     = 2.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* Link the docking container to the target dynpro
  gd_repid = syst-repid.
  CALL METHOD go_docking->link
    EXPORTING
      repid                       = gd_repid
      dynnr                       = '0100'
*      CONTAINER                   =
    EXCEPTIONS
      OTHERS                      = 4.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* ok-code field = GD_OKCODE
  CALL SCREEN '0100'.
* Flow logic (no elements on screen):
*  PROCESS BEFORE OUTPUT.
*    MODULE STATUS_0100.
**
*  PROCESS AFTER INPUT.
*    MODULE USER_COMMAND_0100.


END-OF-SELECTION.

*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'STATUS_0100'.
*  SET TITLEBAR 'xxx'.

  CALL METHOD go_grid->refresh_table_display
*      EXPORTING
*        IS_STABLE      =
*        I_SOFT_REFRESH =
    EXCEPTIONS
      finished       = 1
      OTHERS         = 2.
  IF sy-subrc <> 0.
*     MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*                WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

ENDMODULE.                 " STATUS_0100  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.

  TRANSLATE gd_okcode TO UPPER CASE.

* Fetch changes on ALV grid
  go_grid->check_changed_data( ).

  CASE gd_okcode.
    WHEN 'BACK' OR
         'END'  OR
         'CANC'.
      SET SCREEN 0. LEAVE SCREEN.

    WHEN 'REFRESH'.
      PERFORM refresh_outtab.

    WHEN OTHERS.
  ENDCASE.





  CLEAR: gd_okcode.

ENDMODULE.                 " USER_COMMAND_0100  INPUT


*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG_KNB1
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_fieldcatalog .
* define local data
  DATA:
    ls_fcat        TYPE lvc_s_fcat.



  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
*     I_BUFFER_ACTIVE              =
      i_structure_name             = 'KNB1'
*     I_CLIENT_NEVER_DISPLAY       = 'X'
*     I_BYPASSING_BUFFER           =
*     I_INTERNAL_TABNAME           =
    CHANGING
      ct_fieldcat                  = gt_fcat
    EXCEPTIONS
      inconsistent_interface       = 1
      program_error                = 2
      OTHERS                       = 3.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.




  " Set required fields editable
  LOOP AT gt_fcat INTO ls_fcat
                  WHERE ( fieldname = 'ZUAWA'  OR
                          fieldname = 'BUSAB' ).
    ls_fcat-edit    = abap_true.

    MODIFY gt_fcat FROM ls_fcat.
  ENDLOOP.

  DELETE gt_fcat WHERE ( fieldname = 'ZINRT' ).


ENDFORM.                    " BUILD_FIELDCATALOG


*&---------------------------------------------------------------------*
*&      Form  SET_LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM set_layout .

  CLEAR: gs_layout.

  gs_layout-cwidth_opt = abap_true.
  gs_layout-zebra      = abap_true.

**  gs_layout-stylefname = 'CELLTAB'.

ENDFORM.                    " SET_LAYOUT

*&---------------------------------------------------------------------*
*&      Form  REFRESH_OUTTAB
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM refresh_outtab .
* define local data
  DATA:
    ld_idx       TYPE i,
    ls_outtab    TYPE ty_s_outtab,
    ls_tmp       TYPE ty_s_outtab,
    lt_tmp       TYPE ty_t_outtab.

  " Simulate selection of data depending on value of ZUAWA
  ls_tmp-zuawa = '003'.
  ls_tmp-busab = 'K1'.  APPEND ls_tmp TO lt_tmp.
  ls_tmp-busab = 'K2'.  APPEND ls_tmp TO lt_tmp.
  ls_tmp-busab = 'K3'.  APPEND ls_tmp TO lt_tmp.


  LOOP AT gt_outtab INTO ls_outtab
                    WHERE ( zuawa = '003'
                    AND     busab IS INITIAL ).
    ld_idx = syst-tabix.

    LOOP AT lt_tmp INTO ls_tmp.
      ls_outtab-busab = ls_tmp-busab.

      IF ( syst-tabix = 1 ).
        MODIFY gt_outtab FROM ls_outtab INDEX ld_idx.
      ELSE.
        APPEND ls_outtab TO gt_outtab.
      ENDIF.
    ENDLOOP.


  ENDLOOP.



ENDFORM.                    " REFRESH_OUTTAB

Regards

Uwe

0 Kudos

Hi Uwe,

Thanks for your solution,which helped me a lot.Can you also please let me know about the code when the user deletes the row.

Requirement is same as the previous.Here user can also delete the row,which has to updated in the database table.How to handle when the row is deleted??

Please I need it uregently

Thanks,

Balaji

uwe_schieferstein
Active Contributor
0 Kudos

Hello Balaji

I would use a simplified approach by storing the PBO output before displaying the editable ALV list. As soon as the user wants to save the data (enter 'SAVE' in command window) the report compares the PBO data with the current PAI data.

To see the effect simply add a single row as previously described (resulting in three new rows) and delete two other rows. Inserted and deleted rows will be displayed afterwards.

*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_ALVGRID_EDITABLE_7
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zus_sdn_alvgrid_editable_7.


TYPE-POOLS: abap.

INCLUDE <icon>.  " NOTE: replace by TYPE-POOLS: icon. on >= 6.20

DATA:
  gd_repid         TYPE syrepid,
  gd_okcode        TYPE sy-ucomm,
*
  gs_layout        TYPE lvc_s_layo,
  gt_fcat          TYPE lvc_t_fcat,
  go_docking       TYPE REF TO cl_gui_docking_container,
  go_grid          TYPE REF TO cl_gui_alv_grid.


TYPES: BEGIN OF ty_s_outtab.
INCLUDE TYPE knb1.
TYPES: END OF ty_s_outtab.
TYPES: ty_t_outtab    TYPE STANDARD TABLE OF ty_s_outtab
                      WITH DEFAULT KEY.

DATA:
  gt_outtab        TYPE ty_t_outtab,
  gt_outtab_pbo    TYPE ty_t_outtab.



*---------------------------------------------------------------------*
*       CLASS lcl_eventhandler DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_eventhandler DEFINITION.

  PUBLIC SECTION.

    CLASS-METHODS:
      handle_data_changed
        FOR EVENT data_changed OF cl_gui_alv_grid
        IMPORTING
          er_data_changed
          e_onf4
          e_onf4_before
          e_onf4_after
          e_ucomm
          sender,

      handle_data_changed_finished
        FOR EVENT data_changed_finished OF cl_gui_alv_grid
        IMPORTING
          e_modified
          et_good_cells,

      handle_user_command
        FOR EVENT user_command OF cl_gui_alv_grid
        IMPORTING
          e_ucomm,


      handle_toolbar
        FOR EVENT toolbar OF cl_gui_alv_grid
        IMPORTING
          e_object
          e_interactive.


ENDCLASS.                    "lcl_eventhandler DEFINITION


*---------------------------------------------------------------------*
*       CLASS lcl_eventhandler IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_eventhandler IMPLEMENTATION.

  METHOD handle_data_changed.
*   define local data

**    cl_gui_cfw=>set_new_ok_code( 'REFRESH' ). " not possible on 4.6c
    CALL METHOD cl_gui_cfw=>set_new_ok_code
      EXPORTING
        new_code = 'REFRESH'
*      IMPORTING
*        RC       =
        .


  ENDMETHOD.                    "handle_data_changed

  METHOD handle_data_changed_finished.
*   define local data
    DATA:
      ls_outtab      TYPE ty_s_outtab,
      ls_cell        TYPE lvc_s_modi.


  ENDMETHOD.                    "handle_data_changed_finished

  METHOD handle_user_command.

  ENDMETHOD.                    "handle_user_command

  METHOD handle_toolbar.
*   define local data
    DATA:
      ls_button    TYPE stb_button.

    ls_button-function  = 'DEFAULT'.
    ls_button-icon      = icon_mass_change.
    ls_button-quickinfo = 'Set default value for column'.
    APPEND ls_button TO e_object->mt_toolbar.

  ENDMETHOD.                    "handle_toolbar

ENDCLASS.                    "lcl_eventhandler IMPLEMENTATION




START-OF-SELECTION.


  SELECT * FROM knb1 INTO TABLE gt_outtab UP TO 20 ROWS
    WHERE bukrs = '1000'.

  gt_outtab_pbo = gt_outtab.  " store PBO data



* Create docking container
  CREATE OBJECT go_docking
    EXPORTING
      parent                      = cl_gui_container=>screen0
      ratio                       = 90
    EXCEPTIONS
      OTHERS                      = 6.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* Create ALV grid
  CREATE OBJECT go_grid
    EXPORTING
      i_parent          = go_docking
    EXCEPTIONS
      OTHERS            = 5.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.



* Build fieldcatalog
  PERFORM build_fieldcatalog.

  PERFORM set_layout.

  SET HANDLER:
    lcl_eventhandler=>handle_toolbar               FOR go_grid,
    lcl_eventhandler=>handle_data_changed          FOR go_grid,
    lcl_eventhandler=>handle_data_changed_finished FOR go_grid.



* Display data
  CALL METHOD go_grid->set_table_for_first_display
    EXPORTING
      is_layout       = gs_layout
    CHANGING
      it_outtab       = gt_outtab
      it_fieldcatalog = gt_fcat
    EXCEPTIONS
      OTHERS          = 4.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

  go_grid->set_toolbar_interactive( ).

  CALL METHOD go_grid->register_edit_event
    EXPORTING
      i_event_id = cl_gui_alv_grid=>mc_evt_enter
    EXCEPTIONS
      error      = 1
      OTHERS     = 2.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* Link the docking container to the target dynpro
  gd_repid = syst-repid.
  CALL METHOD go_docking->link
    EXPORTING
      repid                       = gd_repid
      dynnr                       = '0100'
*      CONTAINER                   =
    EXCEPTIONS
      OTHERS                      = 4.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* ok-code field = GD_OKCODE
  CALL SCREEN '0100'.
* Flow logic (no elements on screen):
*  PROCESS BEFORE OUTPUT.
*    MODULE STATUS_0100.
**
*  PROCESS AFTER INPUT.
*    MODULE USER_COMMAND_0100.


END-OF-SELECTION.

*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'STATUS_0100'.
*  SET TITLEBAR 'xxx'.

  CALL METHOD go_grid->refresh_table_display
*      EXPORTING
*        IS_STABLE      =
*        I_SOFT_REFRESH =
    EXCEPTIONS
      finished       = 1
      OTHERS         = 2.
  IF sy-subrc <> 0.
*     MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*                WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

ENDMODULE.                 " STATUS_0100  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.

  TRANSLATE gd_okcode TO UPPER CASE.

* Fetch changes on ALV grid
  go_grid->check_changed_data( ).

  CASE gd_okcode.
    WHEN 'BACK' OR
         'END'  OR
         'CANC'.
      SET SCREEN 0. LEAVE SCREEN.

    WHEN 'REFRESH'.
      PERFORM refresh_outtab.

    WHEN 'SAVE'.
      PERFORM save_data.

    WHEN OTHERS.
  ENDCASE.





  CLEAR: gd_okcode.

ENDMODULE.                 " USER_COMMAND_0100  INPUT


*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG_KNB1
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_fieldcatalog .
* define local data
  DATA:
    ls_fcat        TYPE lvc_s_fcat.



  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
*     I_BUFFER_ACTIVE              =
      i_structure_name             = 'KNB1'
*     I_CLIENT_NEVER_DISPLAY       = 'X'
*     I_BYPASSING_BUFFER           =
*     I_INTERNAL_TABNAME           =
    CHANGING
      ct_fieldcat                  = gt_fcat
    EXCEPTIONS
      inconsistent_interface       = 1
      program_error                = 2
      OTHERS                       = 3.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.




  " Set required fields editable
  LOOP AT gt_fcat INTO ls_fcat
                  WHERE ( fieldname = 'ZUAWA'  OR
                          fieldname = 'BUSAB' ).
    ls_fcat-edit    = abap_true.

    MODIFY gt_fcat FROM ls_fcat.
  ENDLOOP.

  DELETE gt_fcat WHERE ( fieldname = 'ZINRT' ).


ENDFORM.                    " BUILD_FIELDCATALOG


*&---------------------------------------------------------------------*
*&      Form  SET_LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM set_layout .

  CLEAR: gs_layout.

  gs_layout-cwidth_opt = abap_true.
  gs_layout-zebra      = abap_true.

**  gs_layout-stylefname = 'CELLTAB'.

ENDFORM.                    " SET_LAYOUT

*&---------------------------------------------------------------------*
*&      Form  REFRESH_OUTTAB
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM refresh_outtab .
* define local data
  DATA:
    ld_idx       TYPE i,
    ls_outtab    TYPE ty_s_outtab,
    ls_tmp       TYPE ty_s_outtab,
    lt_tmp       TYPE ty_t_outtab.

  " Simulate selection of data depending on value of ZUAWA
  ls_tmp-zuawa = '003'.
  ls_tmp-busab = 'K1'.  APPEND ls_tmp TO lt_tmp.
  ls_tmp-busab = 'K2'.  APPEND ls_tmp TO lt_tmp.
  ls_tmp-busab = 'K3'.  APPEND ls_tmp TO lt_tmp.


  LOOP AT gt_outtab INTO ls_outtab
                    WHERE ( zuawa = '003'
                    AND     busab IS INITIAL ).
    ld_idx = syst-tabix.

    LOOP AT lt_tmp INTO ls_tmp.
      ls_outtab-busab = ls_tmp-busab.

      IF ( syst-tabix = 1 ).
        MODIFY gt_outtab FROM ls_outtab INDEX ld_idx.
      ELSE.
        APPEND ls_outtab TO gt_outtab.
      ENDIF.
    ENDLOOP.


  ENDLOOP.



ENDFORM.                    " REFRESH_OUTTAB


*&---------------------------------------------------------------------*
*&      Form  SAVE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM save_data .
* define local data
  DATA:
    ls_outtab    TYPE ty_s_outtab,
    ls_pbo       TYPE ty_s_outtab,
    lt_insert    TYPE ty_t_outtab,
    lt_delete    TYPE ty_t_outtab.

" Compare PBO with PAI -> find deleted records
  LOOP AT gt_outtab_pbo INTO ls_pbo.

    READ TABLE gt_outtab INTO ls_outtab
         WITH KEY table_line = ls_pbo.
    IF ( syst-subrc NE 0 ).
      APPEND ls_outtab TO lt_delete.
    ENDIF.

  ENDLOOP.


" Compare PAI with PBO data -> find new records
  LOOP AT gt_outtab INTO ls_outtab.

    READ TABLE gt_outtab_pbo INTO ls_pbo
         WITH KEY table_line = ls_outtab.
    IF ( syst-subrc NE 0 ).
      APPEND ls_outtab TO lt_insert.
    ENDIF.
  ENDLOOP.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_structure_name = 'KNB1'
      i_grid_title     = 'New Records'
    TABLES
      t_outtab         = lt_insert
    EXCEPTIONS
      program_error    = 1
      OTHERS           = 2.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_structure_name = 'KNB1'
      i_grid_title     = 'Deleted Records'
    TABLES
      t_outtab         = lt_delete
    EXCEPTIONS
      program_error    = 1
      OTHERS           = 2.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


  gt_outtab_pbo = gt_outtab.  " do not forget !!!

ENDFORM.                    " SAVE_DATA

Regards

Uwe