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: 

editable ALv and SAVE criteria

Former Member
0 Kudos

Dear All,

I had requirement like i had to fill some columns in editable alv. After entering data, when click save it has to save in a ZTABLE.

With Regards,

PrasadBabu.

11 REPLIES 11

I355602
Advisor
Advisor
0 Kudos

Hi,

Refer this program:-


  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
   EXPORTING
     i_callback_program                = v_rep_id       " report id
     i_callback_pf_status_set          = 'PF'           " for PF-STATUS
     i_callback_user_command           = 'USER_COMMAND' " for User-Command
     is_layout                         = wa_layout      " for layout
     it_fieldcat                       = it_field       " field catalog
     it_sort                           = it_sort        " sort info
    TABLES
      t_outtab                          = it_final      " internal table
   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.

*&---------------------------------------------------------------------*
*&      Form  pf
*&---------------------------------------------------------------------*
*       SUB-ROUTINE PF IS USED TO SET THE PF-STATUS OF THE SCREEN
*       ON WHICH THE ALV GRID IS DISPLAYED
*----------------------------------------------------------------------*
*       -->RT_EXTAB
*----------------------------------------------------------------------*
FORM pf USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS 'ZTG_STAT'.
ENDFORM.                    "pf

*&---------------------------------------------------------------------*
*&      Form  USER_COMMAND
*&---------------------------------------------------------------------*
*       SUB-ROUTINE USER_COMMAND IS USED TO HANDLE THE USER ACTION
*       AND EXECUTE THE APPROPIATE CODE
*----------------------------------------------------------------------*
*      -->LV_OKCODE   used to capture the function code
*                     of the user-defined push-buttons
*      -->L_SELFIELD   text
*----------------------------------------------------------------------*
FORM user_command USING lv_okcode LIKE sy-ucomm l_selfield TYPE slis_selfield.

* assign the function code to variable v_okcode
  lv_okcode = sy-ucomm.

* handle the code execution based on the function code encountered
  CASE lv_okcode.

* when the function code is SAVE then process the selected records
    WHEN 'SAVE'.

* to reflect the data changed into internal table
      DATA : ref_grid TYPE REF TO cl_gui_alv_grid. "new

      IF ref_grid IS INITIAL.
        CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
          IMPORTING
            e_grid = ref_grid.
      ENDIF.

      IF NOT ref_grid IS INITIAL.
        CALL METHOD ref_grid->check_changed_data.
      ENDIF.

"now at this time you have modified internal table

* refresh the ALV Grid output from internal table
      l_selfield-refresh = c_check.

"alv output is refreshed as per changes in internal table

"now you can include code to modify your ztable from internal table

  ENDCASE.

ENDFORM.                    "USER_COMMAND

Regards,

Tarun

Edited by: Tarun Gambhir on Mar 9, 2009 12:48 PM

0 Kudos

hi,

See the below example code


*&---------------------------------------------------------------------*
*&GLOBAL DECLARATION
*&---------------------------------------------------------------------*
TYPE-POOLS slis.
TABLES ekko.
*&---------------------------------------------------------------------*
*&TYPES DECLARATION
*&---------------------------------------------------------------------*
TYPES : BEGIN OF ty_ekko,
        lifnr TYPE ekko-lifnr,  " Vendor Account Number
        ebeln TYPE ekko-ebeln,  " Purchasing Document Number
        bsart TYPE ekko-bsart,  " Purchasing Document Type
        ernam TYPE ekko-ernam,  " Name of Person who Created the Object
        END OF ty_ekko.
 
*&---------------------------------------------------------------------*
*&DATA DECLARATION
*&---------------------------------------------------------------------*
 
DATA : it_ekko TYPE TABLE OF ty_ekko,
       it_fcat TYPE slis_t_fieldcat_alv,
       it_event TYPE slis_t_event,
 
 
       wa_fcat TYPE slis_fieldcat_alv,
       wa_event TYPE slis_alv_event.
*&---------------------------------------------------------------------*
*&SELECTION-SCREEN
*&---------------------------------------------------------------------*
SELECT-OPTIONS s_lifnr FOR ekko-lifnr.
*&---------------------------------------------------------------------*
*&INITIALIZATION : EVENT
*&---------------------------------------------------------------------*
INITIALIZATION.
  PERFORM f_prep_fcat.
  PERFORM f_fill_events.
*&---------------------------------------------------------------------*
*&START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM f_fetch_data.
*&---------------------------------------------------------------------*
*&END-OF-SELECTION
*&---------------------------------------------------------------------*
END-OF-SELECTION.
 
  PERFORM f_display_data.
 
*&---------------------------------------------------------------------*
*&      Form  f_prep_fcat
*&---------------------------------------------------------------------*
FORM f_prep_fcat .
  wa_fcat-col_pos = 1.
  wa_fcat-fieldname = 'LIFNR'.
  wa_fcat-seltext_m = 'VENDOR NUMBER'.
  wa_fcat-edit = 'X'.   " this is for editing the fields
  wa_fcat-outputlen = 30.
  APPEND wa_fcat TO it_fcat.
 
  wa_fcat-col_pos = 2.
  wa_fcat-fieldname = 'EBELN'.
  wa_fcat-seltext_m = 'PURCHASING DOCUMENT #'.
  wa_fcat-edit = 'X'.  " this is for editing the fields
  wa_fcat-outputlen = 30.
  APPEND wa_fcat TO it_fcat.
 
 
  wa_fcat-col_pos = 3.
  wa_fcat-fieldname = 'BSART'.
  wa_fcat-seltext_m = 'DOCUMENT TYPE'.
  wa_fcat-edit = 'X'.      " this is for editing the fields
  wa_fcat-outputlen = 30.
  APPEND wa_fcat TO it_fcat.
 
 
  wa_fcat-col_pos = 4.
  wa_fcat-fieldname = 'ERNAM'.
  wa_fcat-seltext_m = 'PERSON RESPONSIBLE'.
  wa_fcat-edit = 'X'.            " this is for editing the fields
  wa_fcat-outputlen = 30.
  APPEND wa_fcat TO it_fcat.
 
ENDFORM.                    " f_prep_fcat
 
*&---------------------------------------------------------------------*
*&      Form  f_fill_events
*&---------------------------------------------------------------------*
FORM f_fill_events .
 
  wa_event-name = 'USER_COMMAND'.
  wa_event-form = 'SAVE'.
  APPEND wa_event TO it_event.
 
ENDFORM.                    " f_fill_events
*&---------------------------------------------------------------------*
*&      Form  f_fetch_data
*&---------------------------------------------------------------------*
FORM f_fetch_data .
  SELECT lifnr
         ebeln
         bsart
         ernam FROM ekko INTO TABLE it_ekko WHERE lifnr IN s_lifnr.
 
*  SORT it_ekko BY lifnr DESCENDING .
ENDFORM.                    " f_fetch_data
*&---------------------------------------------------------------------*
*&      Form  f_display_data
*&---------------------------------------------------------------------*
FORM f_display_data .
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      it_fieldcat        = it_fcat
      it_events          = it_event
      i_save             = 'X'
    TABLES
      t_outtab           = it_ekko
    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.
 
ENDFORM.                    " f_display_data
 
*&---------------------------------------------------------------------*
*&      Form  SAVE
*&---------------------------------------------------------------------*
FORM save   USING   r_ucomm LIKE sy-ucomm
                    rs_selfield TYPE slis_selfield.
  IF r_ucomm = '&DATA_SAVE'.  "check the functioncode of the save button..it may be different
    BREAK-POINT.
" do your work here
  ENDIF.
ENDFORM.                    "save

Thanks & Regards

Former Member
0 Kudos

i did up to wat u have been explained. here it is selecting only one modified cell. but i want here to select including modified cell and at the same time whole output data into ztable.

regards,

prasadbabu.

0 Kudos

Hi,

>

> i did up to wat u have been explained. here it is selecting only one modified cell. but i want here to select including modified cell and at the same time whole output data into ztable.

>

> regards,

> prasadbabu.

Use code in user_cmmand in alv grid display:-


*&---------------------------------------------------------------------*
*&      Form  USER_COMMAND
*&---------------------------------------------------------------------*
*       SUB-ROUTINE USER_COMMAND IS USED TO HANDLE THE USER ACTION
*       AND EXECUTE THE APPROPIATE CODE
*----------------------------------------------------------------------*
*      -->LV_OKCODE   used to capture the function code
*                     of the user-defined push-buttons
*      -->L_SELFIELD   text
*----------------------------------------------------------------------*
FORM user_command USING lv_okcode LIKE sy-ucomm l_selfield TYPE slis_selfield.
 
* assign the function code to variable v_okcode
  lv_okcode = sy-ucomm.
 
* handle the code execution based on the function code encountered
  CASE lv_okcode.
 
* when the function code is SAVE then process the selected records
    WHEN 'SAVE'.
 
* to reflect the data changed into internal table
      DATA : ref_grid TYPE REF TO cl_gui_alv_grid. "new
 
      IF ref_grid IS INITIAL.
        CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
          IMPORTING
            e_grid = ref_grid.
      ENDIF.
 
      IF NOT ref_grid IS INITIAL.
        CALL METHOD ref_grid->check_changed_data.
      ENDIF.
 
"now at this time you have modified internal table
 
* refresh the ALV Grid output from internal table
      l_selfield-refresh = c_check.
 
"alv output is refreshed as per changes in internal table
 
"now you can include code to modify your ztable from internal table
      MODIFY <ztable> FROM it_final. "modify ztable the changes in alv
 
  ENDCASE.
 
ENDFORM.                    "USER_COMMAND

The rest code is same as in the previous reply by me.

Regards,

Tarun

Former Member
0 Kudos

Hi Prasad,

Please refer to below link.

Regards,

Phani.

Former Member
0 Kudos

Hi,

You can refer the given below link

https://wiki.sdn.sap.com/wiki/display/Snippets/A%2bSimple%2bProgram%2bfor%2bEditable%2bALV%

Thanks.

Arun

Edited by: Arun Kayal on Mar 9, 2009 8:20 AM

Former Member
0 Kudos

Hi,

In your internal table add an extra field say for an example

data : begin of fs,
"all your fields come here,
check(1) type c,
end of fs.

"Now in the layout just before calling the FM reuse_ALV_grid_display write the statement given below..
" Layout should be the variable you are using for the layout of your screen.

layout-sel_mode = 'A'.
layout-box_fname = 'CHECK'.

Now in 

case r_ucomm.
when '&DATA_SAVE&.
   loop at your_internal_table into work_area where check = 'X'.
         " Processing logic
  endloop.
endcase.

This works,

Regards,

Siddarth

Former Member
0 Kudos

Hi Prasad ,

useful link..

https://www.sdn.sap.com/irj/scn/wiki?path=/display/snippets/alv-editingandsavingtheeditedvaluesin+Database%2528OOPS%2529

Regards,

Mdi.Deeba

Former Member
0 Kudos

hi,

Follow the steps below.

1.First in the fieldcatolog u have to set the attribute EDIT = 'X' for the columns u need to edit

2 have to create class defenition and implementation

3.create a button save using the method toolbar of the grid.

3.then implement method er_data_changed which will capture the changed data cells when pressed save and u can manipulate.

but the event data changed in editable alv will capture only the changed data suppose u r not changing any data and click on save means it will invoke handle user command

A sample code is attached below

*--->class defenition and implementation

*class defenition

CLASS cl_eventhandler DEFINITION.

PUBLIC SECTION.

METHODS:

handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid

IMPORTING e_object e_interactive,

handle_user_command FOR EVENT user_command OF cl_gui_alv_grid

IMPORTING e_ucomm.

METHODS:

handle_data_changed FOR EVENT data_changed OF cl_gui_alv_grid

IMPORTING er_data_changed e_ucomm.

ENDCLASS. "cl_eventhandler DEFINITION

*class implementation

CLASS cl_eventhandler IMPLEMENTATION.

METHOD handle_toolbar.

DATA: ls_toolbar TYPE stb_button.

CLEAR ls_toolbar.

MOVE 'SAVE' TO ls_toolbar-function.

MOVE icon_print TO ls_toolbar-icon.

MOVE 'Save data' TO ls_toolbar-quickinfo.

MOVE 'Save data' TO ls_toolbar-text.

MOVE ' ' TO ls_toolbar-disabled.

APPEND ls_toolbar TO e_object->mt_toolbar.

CLEAR ls_toolbar.

CLEAR ls_toolbar.

MOVE 'DISPLAY' TO ls_toolbar-function.

MOVE icon_print TO ls_toolbar-icon.

MOVE 'Display errors' TO ls_toolbar-quickinfo.

MOVE 'Display errors' TO ls_toolbar-text.

MOVE ' ' TO ls_toolbar-disabled.

APPEND ls_toolbar TO e_object->mt_toolbar.

CLEAR ls_toolbar.

ENDMETHOD. "handle_toolbar

METHOD handle_user_command.

CLEAR it_err.

CASE e_ucomm.

WHEN 'SAVE'.

perfrom wht u want to do when changes not made in any cell and press on save

endcase.

endmethod.

METHOD handle_data_changed.

er_data_changed->mt_mod_cells INTO ls_good

thsi will return the changed cells withn values

endmethod.

Former Member
0 Kudos

Hi ,

Use method CHECK_CHANGED_DATA of class cl_gui_alv_grid ,in the PAI of custom screen.

This method will capture the existing data of ALV output.This includes both changed and unchanged data.Once you get this changed data you can process this as per your requirement.

Hope this helps.

Edited by Tejaswini Khante

Former Member
0 Kudos

go through this program...

type-pools: slis.

tables:zacttable.

--


structure declaration--

data:begin of it_itab occurs 0,

kunnr type kunnr, "customer number

land1 type land1, "country key

name1 type name1, "name

ort01 type ort01, "city

pstlz type pstlz, "postal code

end of it_itab,

--


field catalog for first display--

it_fieldcat type slis_t_fieldcat_alv,

wa_fieldcat type slis_fieldcat_alv,

--


field catalog for second display--

it_fieldcat1 type slis_t_fieldcat_alv,

wa_fieldcat1 type slis_fieldcat_alv,

--


work area for layout designing--

wa_layout type slis_layout_alv,

--


variable declaration--

v_kunnr type zacttable-kunnr,

--


table for headings--

t_header type slis_t_listheader,

ok_code like sy-ucomm,

wa_header type slis_listheader.

--


selection screen--

selection-screen begin of block b1 with frame title text-000.

select-options:so_kunnr for v_kunnr.

selection-screen end of block b1.

&----


*& start-of-selection event

&----


start-of-selection.

--


perform for retriving the data--

perform data_retrieval.

--


perform for field catalog--

perform build_fieldcatalog1.

--


perform for layout designing--

perform build_layout.

--


perform for alv grid display--

perform grid_display_alv.

&----


*& Form DATA_RETRIEVAL

&----


  • form to retrive the data

----


form data_retrieval .

select kunnr land1 name1 ort01 pstlz from zacttable into table it_itab

where kunnr in so_kunnr.

endform. " DATA_RETRIEVAL

&----


*& Form BUILD_FIELDCATALOG1

&----


  • form to build catalog

----


form build_fieldcatalog1 .

wa_fieldcat-fieldname = 'KUNNR'.

wa_fieldcat-seltext_l = 'CUSTOMER NUMBER'.

append wa_fieldcat to it_fieldcat.

wa_fieldcat-fieldname = 'LAND1'.

wa_fieldcat-seltext_l = 'COUNTRY KEY'.

append wa_fieldcat to it_fieldcat.

wa_fieldcat-fieldname = 'NAME1'.

wa_fieldcat-seltext_l = 'NAME'.

append wa_fieldcat to it_fieldcat.

wa_fieldcat-fieldname = 'ORT01'.

wa_fieldcat-seltext_l = 'CITY'.

append wa_fieldcat to it_fieldcat.

wa_fieldcat-fieldname = 'PSTLZ'.

wa_fieldcat-seltext_l = 'POSTAL CODE'.

append wa_fieldcat to it_fieldcat.

endform. " BUILD_FIELDCATALOG1

&----


*& Form BUILD_LAYOUT

&----


  • form to design layout

----


form build_layout .

wa_layout-zebra ='X'.

wa_layout-no_vline = ' '.

wa_layout-no_hline = ' '.

wa_layout-colwidth_optimize = 'X'.

endform. " BUILD_LAYOUT

&----


*& Form GRID_DISPLAY_ALV

&----


  • form for display the data

----


form grid_display_alv .

call function 'REUSE_ALV_GRID_DISPLAY'

exporting

i_callback_program = sy-repid

i_callback_top_of_page = 'TOP-OF-PAGE1'

i_callback_pf_status_set = 'SET_PF_STATUS'

i_callback_user_command = 'AT_USER_COMMAND'

is_layout = wa_layout

it_fieldcat = it_fieldcat

i_save = ' '

tables

t_outtab = it_itab[]

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.

endform. " GRID_DISPLAY_ALV

&----


*& form top-of-page1

&----


*& Form for top-of-page event

&----


form top-of-page1.

wa_header-typ = 'H'.

wa_header-info = 'DETAILS OF CUSTOMERS CREATED BY ME'.

append wa_header to t_header.

clear wa_header.

call function 'REUSE_ALV_COMMENTARY_WRITE'

exporting

it_list_commentary = t_header

i_logo = 'SUN_TEST_LOGO1'.

refresh t_header.

endform. "top-of-page

&----


*& form set_pf_status

&----


*& Form for pf-status event

&----


form set_pf_status using rt_extab type slis_t_extab.

set pf-status 'CHAITHU'.

"Copy of 'STANDARD' pf_status from fgroup SALV

endform. "set_pf_status

&----


*& form at_user_command

&----


*& Form for at user-command event.

&----


form at_user_command using r_ucomm like sy-ucomm

rs_selfield type slis_selfield.

case r_ucomm.

when 'CHAITU'.

if rs_selfield-fieldname eq 'KUNNR'.

read table it_itab index rs_selfield-tabindex.

endif.

call screen '0200' starting at 10 10 ending at 80 30.

  • CLEAR R_UCOMM.

message i002.

when 'CREATE'.

call screen '0100' starting at 10 10 ending at 80 30.

message i000.

endcase.

endform. "at_user_command

&----


*& Module STATUS_0100 OUTPUT

&----


  • setting status for screen 100.it is for creating the new record

----


module status_0100 output.

set pf-status 'STATUS1'.

endmodule. " STATUS_0100 OUTPUT

&----


*& Module USER_COMMAND_0100 INPUT

&----


  • for creating the new record

----


module user_command_0100 input.

case sy-ucomm.

when 'EXEC'.

ok_code = sy-ucomm.

move-corresponding it_itab to zacttable.

append it_itab .

if sy-subrc eq 0.

  • MESSAGE I000.

else.

message i001.

endif.

  • PERFORM GRID_DISPLAY_ALV.

insert zacttable.

  • clear ok_code.

leave to screen 0.

  • LEAVE PROGRAM.

when 'EXIT'.

leave to screen 0.

endcase.

endmodule. " USER_COMMAND_0100 INPUT

&----


*& Module STATUS_0200 OUTPUT

&----


  • setting status for screen 200.it is for changing record

----


module status_0200 output.

set pf-status 'STATUS2'.

endmodule. " STATUS_0200 OUTPUT

&----


*& Module USER_COMMAND_0200 INPUT

&----


  • for modyfing the existing record

----


module user_command_0200 input.

case sy-ucomm.

when 'CHNG'.

ok_code = sy-ucomm.

modify it_itab index sy-tabix.

move-corresponding it_itab to zacttable.

clear it_itab.

if sy-subrc eq 0.

  • message i002.

endif.

  • PERFORM GRID_DISPLAY_ALV.

modify zacttable.

  • clear ok_code.

leave to screen 0.

  • LEAVE PROGRAM.

when 'EXIT'.

leave to screen 0.

endcase.

endmodule. " USER_COMMAND_0200 INPUT

for this program the output is alv report.and if u select one record and if u click on change button then u will get a screen then u can change any fields in that and then if u click on save button then that record automatically saved in the ztable.