SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

ALV Grid – Insert row with default / previous row values



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ALV Grid / ALV Tree / ALV List
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Tue Jan 15, 2008 2:31 pm    Post subject: ALV Grid – Insert row with default / previous row values Reply with quote

ALV Grid – Insert row function
Srilatha T

Sometimes we need to assign some default values when we create a new row in a grid using standard ALV Append row button. In our scenario we will see how to assign default values to Airline Code (CARRID), Flight Connection Number (CONNID) and Flight date (FLDATE) when a new row is created. To do that we need to handle DATA_CHANGED event in the program like mentioned below.

Definition of a class:

Code:
*---------------------------------------------------------------------*
*       CLASS lcl_event_receiver DEFINITION
*---------------------------------------------------------------------*
*       ........                                                                 *
*---------------------------------------------------------------------*
CLASS LCL_EVENT_RECEIVER DEFINITION.
*.............
  PUBLIC SECTION.

METHODS:
     handle_data_changed
     FOR EVENT data_changed OF cl_gui_alv_grid
     IMPORTING er_data_changed
                       e_ucomm.
ENDCLASS.                    "lcl_event_receiver DEFINITION

Implementation of a class:

Code:
**---------------------------------------------------------**
CLASS LCL_EVENT_RECEIVER IMPLEMENTATION.

  METHOD HANDLE_DATA_CHANGED.
    DATA: dl_ins_row TYPE lvc_s_moce.   " Insert Row
      FIELD-SYMBOLS: <fs> TYPE table.    " Output table

* Loop at the inserted rows table and assign default values
    LOOP AT er_data_changed->mt_inserted_rows INTO dl_ins_row.
      ASSIGN er_data_changed->mp_mod_rows->* TO <fs>.
      loop at <fs> into ls_outtab.
        ls_outtab-carrid  = 'LH'.
        ls_outtab-connid  = '400'.
        ls_outtab-fldate  = sy-datum.
        MODIFY <fs> FROM ls_outtab INDEX sy-tabix.
      endloop.
    endloop.

  ENDMETHOD.                    "handle_data_changed

ENDCLASS.                    "lcl_event_receiver IMPLEMENTATION
**---------------------------------------------------------**

Register the events to trigger DATA_CHANGED event when a new row is created.

Code:
    CALL METHOD OBJ_GRID->REGISTER_EDIT_EVENT
      EXPORTING
        I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_ENTER.

    CALL METHOD OBJ_GRID->REGISTER_EDIT_EVENT
      EXPORTING
        I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_MODIFIED.


Testing:
1) Execute the report and press Append Row button.


2) Row is created with default values.


Sometimes we need to default previous row values to the newly created row. To do that we need to set the attribute AUTO_VALUE while populating field catalog. In our scenario we default values to Airline Code (CARRID), Flight Connection Number (CONNID) and Flight date (FLDATE) when a new row is created.

Code:
  DATA LS_FCAT TYPE LVC_S_FCAT.

  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      I_STRUCTURE_NAME = 'SFLIGHT'
    CHANGING
      CT_FIELDCAT      = PT_FIELDCAT.

 LOOP AT PT_FIELDCAT INTO LS_FCAT.
     IF LS_FCAT-FIELDNAME = 'CARRID'   OR
          LS_FCAT-FIELDNAME = 'CONNID'  OR
          LS_FCAT-FIELDNAME = 'FLDATE'.

* Use field AUTO_VALUE of the fieldcatalog to preset values when new
* lines are added.
         LS_FCAT-EDIT = 'X'.

      LS_FCAT-AUTO_VALUE = 'X'.

         LS_FCAT-CHECKTABLE = '!'.   "do not check foreign key relations
       MODIFY PT_FIELDCAT FROM LS_FCAT.
     ENDIF.
 ENDLOOP.


Testing:
1) Execute the report and press Append Row button.

2) Row is created with previous row values.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ALV Grid / ALV Tree / ALV List All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.