REUSE_ALV_GRID_DISPLAY- 03

light111Editable  GRID ALV with save and refresh

 

 

The previous post REUSE_ALV_GRID_DISPLAY- 02 shows how to make use of field catalog in grid alv. This post shows how to make edit few columns editable and change the field values and updating the data base and refreshing the grid display after change and save.


Code Snippet:  This part shows how to make few columns on alv as editable by settings of field catalog propery.


DATA: lt_spfli TYPE TABLE OF spfli.
DATA: ls_layout TYPE slis_layout_alv.
DATA: lt_fieldcat TYPE slis_t_fieldcat_alv.

START-OF-SELECTION.
PERFORM data_selection.
PERFORM build_layout.
PERFORM build_fieldcatalog.
PERFORM display_alv.
&———————————————————————
& Form DATA_SELECTION
&———————————————————————*
FORM data_selection .
SELECT * FROM spfli INTO TABLE lt_spfli.
ENDFORM. ” DATA_SELECTION

&———————————————————————
& Form BUILD_LAYOUT
&———————————————————————*
FORM build_layout.
ls_layout-zebra = abap_true.
ls_layout-colwidth_optimize = abap_true.
ENDFORM. ” BUILD_LAYOUT

&———————————————————————
& Form BUILD_FIELDCATALOG
&———————————————————————*
FORM build_fieldcatalog .
DATA: ls_fieldcat LIKE LINE OF lt_fieldcat.
CALL FUNCTION ‘REUSE_ALV_FIELDCATALOG_MERGE’
EXPORTING
i_program_name = sy-repid
“I_INTERNAL_TABNAME =
i_structure_name = ‘SPFLI’
CHANGING
ct_fieldcat = lt_fieldcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.

LOOP AT lt_fieldcat INTO ls_fieldcat.
CASE ls_fieldcat-fieldname.
WHEN ‘COUNTRYFR’ OR ‘COUNTRYTO’ OR ‘CITYFROM’ OR ‘CITYTO’.

” here we have for columns set ad editable
ls_fieldcat-edit = abap_true.
MODIFY lt_fieldcat FROM ls_fieldcat TRANSPORTING edit.
WHEN OTHERS.
ENDCASE.
ENDLOOP.

ENDFORM. ” BUILD_FIELDCATALOG
&———————————————————————
& Form DISPLAY_ALV
&———————————————————————*
FORM display_alv .
CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’
EXPORTING
i_callback_program = sy-repid
“i_structure_name = ‘SPFLI’
is_layout = ls_layout
it_fieldcat = lt_fieldcat
TABLES
t_outtab = lt_spfli
EXCEPTIONS
program_error = 1
OTHERS = 2.

ENDFORM. ” DISPLAY_ALV


Program Output


2.jpg


Code Snippet: This part shows how to capture the changed rows in the alv and then updating the data base and refreshing the alv.


DATA: lt_spfli TYPE TABLE OF spfli.
DATA: lt_spfli_old TYPE TABLE OF spfli.
DATA: lt_spfli_upd TYPE TABLE OF spfli.
DATA: ls_spfli_old TYPE spfli.
DATA: ls_spfli_new TYPE spfli.
DATA: ls_layout TYPE slis_layout_alv.
DATA: lt_fieldcat TYPE slis_t_fieldcat_alv.

START-OF-SELECTION.
PERFORM data_selection.
PERFORM build_layout.
PERFORM build_fieldcatalog.
PERFORM display_alv.
&———————————————————————
& Form DATA_SELECTION
&———————————————————————*
FORM data_selection .
SELECT * FROM spfli INTO TABLE lt_spfli.
” make of copy of the table, to do compare after change
lt_spfli_old = lt_spfli.
ENDFORM. ” DATA_SELECTION

&———————————————————————
& Form BUILD_LAYOUT
&———————————————————————*
FORM build_layout.
ls_layout-zebra = abap_true.
ls_layout-colwidth_optimize = abap_true.
ENDFORM. ” BUILD_LAYOUT

&———————————————————————
& Form BUILD_FIELDCATALOG
&———————————————————————*
FORM build_fieldcatalog .
DATA: ls_fieldcat LIKE LINE OF lt_fieldcat.
CALL FUNCTION ‘REUSE_ALV_FIELDCATALOG_MERGE’
EXPORTING
i_program_name = sy-repid
“I_INTERNAL_TABNAME =
i_structure_name = ‘SPFLI’
CHANGING
ct_fieldcat = lt_fieldcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.

LOOP AT lt_fieldcat INTO ls_fieldcat.
CASE ls_fieldcat-fieldname.
WHEN ‘COUNTRYFR’ OR ‘COUNTRYTO’ OR ‘CITYFROM’ OR ‘CITYTO’.
ls_fieldcat-edit = abap_true.
MODIFY lt_fieldcat FROM ls_fieldcat TRANSPORTING edit.
WHEN OTHERS.
ENDCASE.
ENDLOOP.

ENDFORM. ” BUILD_FIELDCATALOG
&———————————————————————
& Form DISPLAY_ALV
&———————————————————————*
FORM display_alv .

CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY’
EXPORTING
i_callback_program = sy-repid
i_callback_user_command = ‘EDIT_AND_SAVE’ ” this has to be used to handle SAVE
“i_structure_name = ‘SPFLI’
is_layout = ls_layout
it_fieldcat = lt_fieldcat
TABLES
t_outtab = lt_spfli
EXCEPTIONS
program_error = 1
OTHERS = 2.

ENDFORM. ” DISPLAY_ALV

FORM edit_and_save USING r_ucomm LIKE sy-ucomm
                                                       rs_selfield TYPE slis_selfield.
CASE r_ucomm.
WHEN ‘&DATA_SAVE’.
” pick those records where there are changes,
” by compairing each line of alv to the old table
LOOP AT lt_spfli INTO ls_spfli_new.
READ TABLE lt_spfli_old INTO ls_spfli_old INDEX sy-tabix.
CHECK sy-subrc IS INITIAL.
IF ls_spfli_old <> ls_spfli_new.
APPEND ls_spfli_new TO lt_spfli_upd.
CLEAR: ls_spfli_new, ls_spfli_old.
ENDIF.
ENDLOOP.
” the table lt_splfi_upt contains all records,
” where there is change
CHECK lt_spfli_upd IS NOT INITIAL.
” do sense ro lock the table record before updating
UPDATE spfli FROM TABLE lt_spfli_upd.
IF sy-subrc IS INITIAL.
COMMIT WORK.
” clear the update table
CLEAR: lt_spfli_upd.
” now we have to sync the alv table to the old table
lt_spfli_old = lt_spfli.
ELSE.
ROLLBACK WORK.
” in case update fails then we need to, store back
” old table to the lav table
lt_spfli = lt_spfli_old.
ENDIF.
” we need to refresh the alv screen(mostly if it is a roolback case)
rs_selfield-refresh = abap_true.
WHEN OTHERS.
ENDCASE.
ENDFORM.


Program Output: Very first we have the below output. We need to change the departure city with carrid= JL and connid = 407 and current departure city = TOKOYO

3

Check the data base table.

4

press F4 and choose departure city as HIROSHIMA

5

Data changes on ALV and now click on SAVE button to update the data base.

6

After save we have the alv.

7

Check the database and departure city updated.8

Let’s check out few things in debugging specially the update fail case. Put the breakpoint.

9

Current departure city= HIROSHMIA now press F4 and change the value.

10

Select OSAKA.

11

On ALV the value is changed to OSAKA and on SAVE , if update is successful then the departure city remains as OSAKA and if the update fails then the city name should be HIROSHIMA which was before the change. Click on SAVE button.

12

Make subrc value as 4 in debugger so that we say we have a update failure.

13

Now a rollback happens and in program the old table is rewritten over the alv table.

14

Press F8 .

15

So we have the alv list refresh again and display the old data which was before change as the update failed.

16

In DB we have the correct data now. No update happened.

17


 

Advertisements

One thought on “REUSE_ALV_GRID_DISPLAY- 03

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s