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: 

Update Database from editable ALV

d067595
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello,

I'm working on a program based on the Model View Controller pattern.

The model selects data from a database table into an internal table and this table is later displayed in an ALV grid by the view.

This is all working fine.

Now I made two columns editable with a field catalog. Now I enabled the "save"-button on the PF-STATUS to save the changes made in the ALV into the database table.

The code isn't working and I just can't find out why.

So this is my method to react on user command (it's a controller-method):

METHOD react_to_user_command.

CASE iv_ok_code.
WHEN 'BACK'.
LEAVE TO SCREEN 0.
WHEN 'EXIT'.
LEAVE PROGRAM.
WHEN 'SAVE'.

DATA gt_curto_pds_chg_new TYPE gtab_curto_pds_chg.

gt_curto_pds_chg_new = go_controller->go_view->get_data( ).
go_controller->go_model->update_data(

it_curto_pds_chg_new = gt_curto_pds_chg_new ).

ENDCASE.
ENDMETHOD.

This is the get_data( )-method:

METHOD get_data.
go_alv->check_changed_data( ).
rt_curto_pds_chg = gt_curto_pds_chg.
ENDMETHOD.

And this is my update_data( )-method:

METHOD update_data.

DATA: ls_curto_pds_chg_new TYPE curto_pds_chg,
ls_curto_pds_chg TYPE curto_pds_chg.


LOOP AT it_curto_pds_chg_new INTO ls_curto_pds_chg_new.


READ TABLE gt_curto_pds_chg

INTO ls_curto_pds_chg

WITH KEY ch_id = ls_curto_pds_chg_new-ch_id.


IF ls_curto_pds_chg_new <> ls_curto_pds_chg.
UPDATE curto_pds_chg FROM ls_curto_pds_chg_new.
ENDIF.

ENDLOOP.

ENDMETHOD.

Before I implemented the whole update thing I just wrote the WHEN 'SAVE' statement and let it call the method display( ) again just to test if it would show the new or the old table in the ALV. It showed the new table so I thought I could go on and implement the whole thing.

Debugging it now I can see that the table I get from Get_data( ) is already a not updated one and I just don't know why since the table is a changing parameter when building the ALV. I give you my code for the display( )-method as well.

METHOD display.

DATA: lt_fieldcat_fm TYPE slis_t_fieldcat_alv, "field catalog table for FM
ls_fieldcat_fm TYPE slis_fieldcat_alv, "field catalog structure for loop
lt_fieldcat_cl TYPE lvc_t_fcat, "field catalog table for alv method
ls_fieldcat_cl TYPE lvc_s_fcat. "field catalog structure for loop



* builds a field catalog based on the structure of curto_pds_chg
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'CURTO_PDS_CHG'
CHANGING
ct_fieldcat = lt_fieldcat_fm
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.



* loops the field catalog given from the function module into one
* with a structure the alv display method accepts
LOOP AT lt_fieldcat_fm INTO ls_fieldcat_fm.
MOVE-CORRESPONDING ls_fieldcat_fm TO ls_fieldcat_cl.
APPEND ls_fieldcat_cl TO lt_fieldcat_cl.
ENDLOOP.



* makes two columns editable (CHANGE & PROCESS)
DATA: ls_fieldcat TYPE REF TO lvc_s_fcat.
READ TABLE lt_fieldcat_cl
WITH KEY fieldname = 'CHANGE' REFERENCE INTO ls_fieldcat.
ls_fieldcat->edit = 'X'.
READ TABLE lt_fieldcat_cl
WITH KEY fieldname = 'PROCESS' REFERENCE INTO ls_fieldcat.
ls_fieldcat->edit = 'X'.




* builds alv grid
IF go_container IS INITIAL.

CREATE OBJECT go_container
EXPORTING
container_name = 'CONTAINER'.


CREATE OBJECT go_alv
EXPORTING
i_parent = go_container.


CALL METHOD go_alv->set_table_for_first_display
EXPORTING
i_structure_name = 'CURTO_PDS_CHG'
CHANGING
it_outtab = gt_curto_pds_chg
it_fieldcatalog = lt_fieldcat_cl.
ELSE.
go_alv->refresh_table_display( ).
ENDIF.


ENDMETHOD.

Can anybody spot the mistake why my database table is not updating?

1 ACCEPTED SOLUTION

Chintu6august
Contributor
0 Kudos

Hello,

make another internal table which can hold the data before you display.

In PBO:

Write the code to register the edit and the changes you have made on the editable field will reflect on the internal table which you passed to display in grid alv.

CALL METHOD o_grid_alv->register_edit_even
  EXPORTING i_event_id = cl_gui_alv_grid=>mc_evt_modified

the "save"-button on the PF-STATUS will be handled in PAI.

in PAI :

case sy-ucomm.

when 'SAVE'.

call method check_data_changed.

then compare the two internal table and update the database table with the changed records.

endcase.

you can refer the following link

https://wiki.scn.sap.com/wiki/pages/viewpage.action?pageId=37472

thank you!!

2 REPLIES 2

Chintu6august
Contributor
0 Kudos

Hello,

make another internal table which can hold the data before you display.

In PBO:

Write the code to register the edit and the changes you have made on the editable field will reflect on the internal table which you passed to display in grid alv.

CALL METHOD o_grid_alv->register_edit_even
  EXPORTING i_event_id = cl_gui_alv_grid=>mc_evt_modified

the "save"-button on the PF-STATUS will be handled in PAI.

in PAI :

case sy-ucomm.

when 'SAVE'.

call method check_data_changed.

then compare the two internal table and update the database table with the changed records.

endcase.

you can refer the following link

https://wiki.scn.sap.com/wiki/pages/viewpage.action?pageId=37472

thank you!!

d067595
Product and Topic Expert
Product and Topic Expert
0 Kudos

Thank you those two lines of code totally solved my problem! 🙂