Forked from gb20/gist:096e711e1adc9eddf421ec3ad5b8be6f
Created
September 27, 2023 11:24
-
-
Save fanian/bad9308b79a384d65d4c150412728079 to your computer and use it in GitHub Desktop.
ALV EDIT TABLE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*&---------------------------------------------------------------------* | |
*& Report ZMM35BWART2MTYPE_MNT | |
*& | |
*&---------------------------------------------------------------------* | |
*& | |
*& | |
*&---------------------------------------------------------------------* | |
report zmm35bwart2mtype_mnt. | |
class zcl_zmm35bwart2mtype_model definition. | |
public section. | |
types: begin of t_data, | |
bwart type zmm35bwart2mtype-bwart, | |
shkzg type zmm35bwart2mtype-shkzg, | |
btext type t156t-btext, | |
mtype type zmm35bwart2mtype-mtype, | |
mtype_text type c length 10, | |
vtweg type vtweg, | |
vtweg_text type tvtwt-vtext, | |
end of t_data. | |
types t_data_tt type table of t_data with default key. | |
methods load. | |
methods save. | |
methods set_data importing it_data type t_data_tt. | |
methods get_data | |
returning value(et_data) type t_data_tt. | |
methods update_calc_fields changing cs_data type t_data. | |
methods is_changed | |
returning value(ev_result) type boole_d. | |
private section. | |
data: mt_data_curr type table of t_data with key bwart shkzg, | |
mt_data_orig type sorted table of t_data with unique key bwart shkzg. | |
methods convdata2zmm35bwart2mtype importing is_data type t_data | |
returning value(es_zmm35bwart2mtype) type zmm35bwart2mtype. | |
endclass. | |
data: begin of gs_scr100, | |
shown type boole_d, | |
edit type boole_d, | |
custom_cont type ref to cl_gui_custom_container, | |
grid type ref to cl_gui_alv_grid, | |
grid_tbl type zcl_zmm35bwart2mtype_model=>t_data_tt, | |
end of gs_scr100, | |
lo_model type ref to zcl_zmm35bwart2mtype_model. | |
perform main. | |
class zcl_zmm35bwart2mtype_model implementation. | |
method is_changed. | |
if me->mt_data_curr <> me->mt_data_orig. | |
ev_result = abap_true. | |
endif. | |
endmethod. | |
method get_data. | |
et_data = me->mt_data_curr. | |
endmethod. | |
method set_data. | |
me->mt_data_curr = it_data. | |
endmethod. | |
method load. | |
data: lt_zmm35bwart2mtype type sorted table of zmm35bwart2mtype with unique key bwart shkzg, | |
ls_zmm35bwart2mtype like line of lt_zmm35bwart2mtype, | |
lt_bwart type table of bwart, | |
lv_bwart like line of lt_bwart, | |
ls_data like line of me->mt_data_curr. | |
clear me->mt_data_curr. | |
select * | |
into table lt_zmm35bwart2mtype | |
from zmm35bwart2mtype. | |
select bwart | |
into table lt_bwart | |
from t156 | |
group by bwart. | |
sort lt_bwart. | |
loop at lt_bwart into lv_bwart. | |
clear ls_data. | |
ls_data-bwart = lv_bwart. | |
ls_data-shkzg = 'H'. | |
read table lt_zmm35bwart2mtype into ls_zmm35bwart2mtype | |
with table key bwart = ls_data-bwart | |
shkzg = ls_data-shkzg. | |
if sy-subrc = 0. | |
ls_data-mtype = ls_zmm35bwart2mtype-mtype. | |
ls_data-vtweg = ls_zmm35bwart2mtype-vtweg. | |
endif. | |
me->update_calc_fields( changing cs_data = ls_data ). | |
append ls_data to mt_data_curr. | |
clear ls_data. | |
ls_data-bwart = lv_bwart. | |
ls_data-shkzg = 'S'. | |
read table lt_zmm35bwart2mtype into ls_zmm35bwart2mtype | |
with table key bwart = ls_data-bwart | |
shkzg = ls_data-shkzg. | |
if sy-subrc = 0. | |
ls_data-mtype = ls_zmm35bwart2mtype-mtype. | |
ls_data-vtweg = ls_zmm35bwart2mtype-vtweg. | |
endif. | |
me->update_calc_fields( changing cs_data = ls_data ). | |
append ls_data to mt_data_curr. | |
endloop. | |
me->mt_data_orig = me->mt_data_curr. | |
endmethod. | |
method update_calc_fields. | |
data: lt_dd07v type table of dd07v, | |
ls_dd07v like line of lt_dd07v. | |
clear: cs_data-btext, cs_data-mtype_text. | |
if cs_data-bwart is not initial. | |
select single btext | |
into cs_data-btext | |
from t156t | |
where bwart = cs_data-bwart and | |
spras = sy-langu. | |
endif. | |
if cs_data-mtype is not initial. | |
call function 'FM_DOMAINVALUE_CHECK' | |
exporting | |
i_tabname = 'ZMM35BWART2MTYPE' | |
i_fieldname = 'MTYPE' | |
tables | |
t_dd07v = lt_dd07v | |
exceptions | |
input_error = 1 | |
value_not_allowed = 2 | |
others = 3. | |
read table lt_dd07v into ls_dd07v with key domvalue_l = cs_data-mtype. | |
cs_data-mtype_text = ls_dd07v-ddtext. | |
endif. | |
if cs_data-vtweg is not initial. | |
select single vtext | |
into cs_data-vtweg_text | |
from tvtwt | |
where vtweg = cs_data-vtweg and | |
spras = sy-langu. | |
endif. | |
endmethod. | |
method save. | |
data: lt_data_orig like me->mt_data_orig, | |
lt_data like me->mt_data_curr, | |
lt_ins type table of zmm35bwart2mtype, | |
lt_upd type table of zmm35bwart2mtype, | |
lt_del type table of zmm35bwart2mtype, | |
ls_curr like line of me->mt_data_curr, | |
ls_orig like line of me->mt_data_orig. | |
lt_data = me->mt_data_curr. | |
lt_data_orig = me->mt_data_orig. | |
delete lt_data where mtype is initial and vtweg is initial. | |
delete lt_data_orig where mtype is initial and vtweg is initial. | |
loop at lt_data into ls_curr. | |
read table lt_data_orig into ls_orig | |
with table key bwart = ls_curr-bwart | |
shkzg = ls_curr-shkzg. | |
if sy-subrc <> 0. | |
append me->convdata2zmm35bwart2mtype( ls_curr ) to lt_ins. | |
else. | |
delete lt_data_orig index sy-tabix. | |
if ls_curr <> ls_orig. | |
append me->convdata2zmm35bwart2mtype( ls_curr ) to lt_upd. | |
endif. | |
endif. | |
endloop. | |
loop at lt_data_orig into ls_orig. | |
append me->convdata2zmm35bwart2mtype( ls_orig ) to lt_del. | |
endloop. | |
insert zmm35bwart2mtype from table lt_ins. | |
update zmm35bwart2mtype from table lt_upd. | |
delete zmm35bwart2mtype from table lt_del. | |
me->mt_data_orig = me->mt_data_curr. | |
endmethod. | |
method convdata2zmm35bwart2mtype. | |
move-corresponding is_data to es_zmm35bwart2mtype. | |
endmethod. | |
endclass. | |
class lcl_event_reciever definition. | |
public section. | |
types: | |
begin of t_tabix_rowid, | |
tabix type sy-tabix, | |
rowid type i, | |
end of t_tabix_rowid. | |
class-methods: | |
handle_grid_100_data_changed | |
for event data_changed of cl_gui_alv_grid | |
importing er_data_changed. | |
endclass. "lcl_event_reciever DEFINITION | |
define add_fld_mod. | |
assign component &1 of structure <fs_record> to <fs_value>. | |
delete er_data_changed->mt_good_cells | |
where row_id = &2 and | |
fieldname = &1. | |
delete er_data_changed->mt_protocol | |
where row_id = &2 and | |
fieldname = &1. | |
er_data_changed->modify_cell( exporting i_fieldname = &1 | |
i_row_id = &2 | |
i_value = <fs_value> ). | |
end-of-definition. | |
class lcl_event_reciever implementation. | |
method handle_grid_100_data_changed. | |
data: ls_mod_cells like line of er_data_changed->mt_mod_cells, | |
ls_del_row type lvc_s_moce, | |
lt_tabix_rowid type sorted table of t_tabix_rowid with unique key tabix, | |
ls_tabix_rowid like line of lt_tabix_rowid. | |
field-symbols: | |
<fs_tab> like gs_scr100-grid_tbl, | |
<fs_record> like line of <fs_tab>, | |
<fs_record_old> like line of <fs_tab>, | |
<fs_value> type any. | |
assign er_data_changed->mp_mod_rows->* to <fs_tab>. | |
loop at er_data_changed->mt_mod_cells into ls_mod_cells. | |
read table lt_tabix_rowid transporting no fields | |
with table key tabix = ls_mod_cells-tabix. | |
if sy-subrc <> 0. | |
clear ls_tabix_rowid. | |
ls_tabix_rowid-tabix = ls_mod_cells-tabix. | |
ls_tabix_rowid-rowid = ls_mod_cells-row_id. | |
insert ls_tabix_rowid into table lt_tabix_rowid. | |
endif. | |
endloop. | |
loop at <fs_tab> assigning <fs_record>. | |
read table lt_tabix_rowid into ls_tabix_rowid index sy-tabix. | |
assert sy-subrc = 0. | |
read table gs_scr100-grid_tbl assigning <fs_record_old> index ls_tabix_rowid-rowid. | |
if sy-subrc <> 0 or <fs_record_old> <> <fs_record>. | |
lo_model->update_calc_fields( changing cs_data = <fs_record> ). | |
add_fld_mod 'BTEXT' ls_tabix_rowid-rowid. | |
add_fld_mod 'MTYPE_TEXT' ls_tabix_rowid-rowid. | |
add_fld_mod 'VTWEG_TEXT' ls_tabix_rowid-rowid. | |
endif. | |
endloop. | |
endmethod. | |
endclass. | |
*&---------------------------------------------------------------------* | |
*& Module STATUS_0100 OUTPUT | |
*&---------------------------------------------------------------------* | |
* text | |
*----------------------------------------------------------------------* | |
module status_0100 output. | |
set titlebar 'TITLE_100'. | |
set pf-status 'STATUS_100'. | |
if gs_scr100-shown is initial. | |
gs_scr100-grid_tbl = lo_model->get_data( ). | |
perform scr100_grid_init_ctrl. | |
gs_scr100-shown = abap_true. | |
endif. | |
endmodule. " STATUS_0100 OUTPUT | |
form scr100_grid_init_ctrl. | |
data: lt_fieldcat type lvc_t_fcat, | |
lt_fieldcat_orig type lvc_t_fcat, | |
ls_layout type lvc_s_layo, | |
lt_toolbar_excl type ui_functions, | |
ls_variant type disvariant, | |
lv_grid_editable. | |
field-symbols: | |
<fs_fieldcat> like line of lt_fieldcat. | |
lv_grid_editable = gs_scr100-edit. | |
if gs_scr100-custom_cont is initial. | |
create object gs_scr100-custom_cont | |
exporting | |
container_name = 'CUSTOM_CONT_100'. | |
endif. | |
if gs_scr100-grid is initial. | |
perform get_fcat_lvc | |
changing | |
gs_scr100-grid_tbl | |
lt_fieldcat. | |
else. | |
gs_scr100-grid->get_frontend_fieldcatalog( importing et_fieldcatalog = lt_fieldcat ). | |
lt_fieldcat_orig = lt_fieldcat. | |
endif. | |
loop at lt_fieldcat assigning <fs_fieldcat>. | |
if <fs_fieldcat>-fieldname = 'MTYPE' or | |
<fs_fieldcat>-fieldname = 'VTWEG'. | |
<fs_fieldcat>-edit = lv_grid_editable. | |
<fs_fieldcat>-ref_table = 'ZMM35BWART2MTYPE'. | |
endif. | |
endloop. | |
if gs_scr100-grid is initial. | |
create object gs_scr100-grid | |
exporting | |
i_parent = gs_scr100-custom_cont. | |
append gs_scr100-grid->mc_fc_loc_append_row to lt_toolbar_excl. | |
append gs_scr100-grid->mc_fc_loc_delete_row to lt_toolbar_excl. | |
append gs_scr100-grid->mc_fc_loc_insert_row to lt_toolbar_excl. | |
append gs_scr100-grid->mc_fc_loc_paste to lt_toolbar_excl. | |
append gs_scr100-grid->mc_fc_loc_paste_new_row to lt_toolbar_excl. | |
append gs_scr100-grid->mc_fc_loc_copy to lt_toolbar_excl. | |
append gs_scr100-grid->mc_fc_loc_copy_row to lt_toolbar_excl. | |
append gs_scr100-grid->mc_fc_loc_cut to lt_toolbar_excl. | |
append gs_scr100-grid->mc_fc_loc_move_row to lt_toolbar_excl. | |
set handler lcl_event_reciever=>handle_grid_100_data_changed for gs_scr100-grid. | |
call method gs_scr100-grid->register_edit_event | |
exporting | |
i_event_id = cl_gui_alv_grid=>mc_evt_enter. | |
ls_variant-report = sy-cprog. | |
ls_variant-handle = 'G100'. | |
call method gs_scr100-grid->set_table_for_first_display | |
exporting | |
is_layout = ls_layout | |
is_variant = ls_variant | |
i_save = 'A' | |
it_toolbar_excluding = lt_toolbar_excl | |
changing | |
it_fieldcatalog = lt_fieldcat[] | |
it_outtab = gs_scr100-grid_tbl. | |
else. | |
if lt_fieldcat_orig <> lt_fieldcat. | |
gs_scr100-grid->set_frontend_fieldcatalog( lt_fieldcat ). | |
endif. | |
endif. | |
if lv_grid_editable = abap_true. | |
gs_scr100-grid->set_ready_for_input( exporting i_ready_for_input = 1 ). | |
else. | |
gs_scr100-grid->set_ready_for_input( exporting i_ready_for_input = 0 ). | |
endif. | |
endform. | |
form get_fcat_lvc changing pt_outtab type standard table | |
ct_fcat_lvc type lvc_t_fcat . | |
data: | |
lr_salv_table type ref to cl_salv_table , | |
lr_columns_table type ref to cl_salv_columns_table , | |
lr_aggregations type ref to cl_salv_aggregations . | |
try. | |
cl_salv_table=>factory( importing r_salv_table = lr_salv_table | |
changing t_table = pt_outtab ) . | |
catch cx_salv_msg. | |
assert 1 = 0. | |
endtry. | |
lr_columns_table = lr_salv_table->get_columns( ) . | |
ct_fcat_lvc = cl_salv_controller_metadata=>get_lvc_fieldcatalog( | |
r_columns = lr_columns_table | |
r_aggregations = lr_aggregations ) . | |
endform . "get_fcat_lvc | |
form scr100_on_save. | |
data: lv_valid. | |
if gs_scr100-edit = abap_true. | |
gs_scr100-grid->check_changed_data( importing e_valid = lv_valid ). | |
check lv_valid = abap_true. | |
perform scr100_do_save. | |
endif. | |
endform. | |
form scr100_on_modechg. | |
if gs_scr100-edit = abap_true. | |
gs_scr100-edit = abap_false. | |
else. | |
gs_scr100-edit = abap_true. | |
endif. | |
gs_scr100-shown = abap_false. | |
lo_model->load( ). | |
endform. | |
form scr100_do_save. | |
lo_model->set_data( gs_scr100-grid_tbl ). | |
if lo_model->is_changed( ) = abap_true. | |
lo_model->save( ). | |
commit work. | |
gs_scr100-grid_tbl = lo_model->get_data( ). | |
gs_scr100-grid->refresh_table_display( ). | |
message 'Изменения сохранены.' type 'S'. | |
else. | |
message 'Изменения отсутствуют.' type 'S'. | |
endif. | |
endform. | |
form scr100_on_exit. | |
if sy-calld = abap_true. | |
leave program. | |
else. | |
leave to screen 0. | |
endif. | |
endform. "scr103_on_exit | |
*&---------------------------------------------------------------------* | |
*& Module USER_COMMAND_0100 INPUT | |
*&---------------------------------------------------------------------* | |
* text | |
*----------------------------------------------------------------------* | |
module user_command_0100 input. | |
case sy-ucomm. | |
when 'SAVE'. | |
perform scr100_on_save. | |
when 'BACK'. | |
perform scr100_on_exit. | |
when 'MODECHG'. | |
perform scr100_on_modechg. | |
endcase. | |
endmodule. " USER_COMMAND_0100 INPUT | |
module exit_command input. | |
case sy-ucomm. | |
when 'EXIT' or 'CANCEL'. | |
leave program. | |
endcase. | |
endmodule. | |
form main. | |
create object lo_model. | |
lo_model->load( ). | |
call screen 100. | |
endform. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment