Functional ALV系列 (05) - ALV 作为数据编辑界面

2024-02-05 13:18

本文主要是介绍Functional ALV系列 (05) - ALV 作为数据编辑界面,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本篇介绍如何将 ALV 作为数据编辑界面来使用。关于 ALV 作为编辑界面的方法,我在 如何对SAP数据库表进行增删改查操作 这篇博文里已有详细说明,本文不再重复过程。本篇的目的是继续深入,讲解 ALV 作为界面的要点,并且提供一个通用的在 AVL 中修改表的程序。

FALV 作为编辑界面的要点

  • 编写一个能运行的用 ALV 显示数据的程序
  • 从 SAPLKKBL 拷贝 standard 工具栏到本程序,命名为 zstandard
  • 在工具栏中添加一个 Save 按钮
  • 设置 ALV 可编辑
  • 在程序中添加用于设置工具栏和处理用户命令的两个子例程并且进行应用

完成后的完整代码如下:

report  z_falv_009.type-pools: slis.data: gt_fieldcat type slis_t_fieldcat_alv,gs_fieldcat type slis_fieldcat_alv.data:gt_spfli type standard table of spfli,gs_spfli like line of gt_spfli.start-of-selection.perform frm_get_data.perform frm_disp_data.*&---------------------------------------------------------------------*
*&      Form  frm_get_data
*&---------------------------------------------------------------------*
form frm_get_data.select * from spfliinto table gt_spfli.
endform.                    "frm_get_data*&---------------------------------------------------------------------*
*&      Form  frm_disp_data
*&---------------------------------------------------------------------*
form frm_disp_data.call function 'Z_FALV_FIELD_CATALOG'exportingit_output     = gt_spfli[]tablesfield_catalog = gt_fieldcat[]." make ALV editable if field is not a keyloop at gt_fieldcat into gs_fieldcat.if gs_fieldcat-key = 'X'.gs_fieldcat-edit = ''.else.gs_fieldcat-edit = 'X'.endif.modify gt_fieldcat from gs_fieldcat.clear gs_fieldcat.endloop.call function 'REUSE_ALV_GRID_DISPLAY'exportingi_callback_program       = sy-repidi_callback_pf_status_set = 'FRM_GUI_STATUS'i_callback_user_command  = 'FRM_USER_COMMAND'it_fieldcat              = gt_fieldcat[]tablest_outtab                 = gt_spfli[].
endform.                    "frm_disp_data*&---------------------------------------------------------------------*
*&      Form  frm_gui_status
*&---------------------------------------------------------------------*
form frm_gui_status using ex_tab type slis_t_extab.set pf-status 'ZSTANDARD'.
endform.                    "frm_status_set*&---------------------------------------------------------------------*
*&      Form  frm_user_command
*&---------------------------------------------------------------------*
form frm_user_command using p_ucomm    type sy-ucomm        " user commandp_selfield type slis_selfield.  " select fielddata: r_alv_grid type ref to cl_gui_alv_grid.case p_ucomm.when '&SAVE'.if r_alv_grid is initial.call function 'GET_GLOBALS_FROM_SLVC_FULLSCR'importinge_grid = r_alv_grid.endif.if not r_alv_grid is initial.call method r_alv_grid->check_changed_data.endif.p_selfield-refresh = 'X'." save data to dbupdate spfli from table gt_spfli.if sy-subrc is initial.message 'Data was saved successfully.' type 'S'.else.message 'Data was not saved, errors ocured.' type 'S'.endif.endcase.
endform.                    "frm_user_command

在拷贝的工具栏中添加 Save 按钮(细节略)

设置 ALV 可编辑:

设置工具栏:

处理用户的命令:

锁对象

为了保证多用户操作数据的一致性问题,数据保存到数据库需要添加锁对象并且在程序中调用 FM 来加锁和释放。我的另一篇博文 ABAP - 锁对象及使用方法描述了如何创建和应用锁对象。在程序中应用锁对象的方法如下:

form frm_user_command using p_ucomm    type sy-ucomm        " user commandp_selfield type slis_selfield.  " select fielddata: r_alv_grid type ref to cl_gui_alv_grid.case p_ucomm.when '&SAVE'.if r_alv_grid is initial.call function 'GET_GLOBALS_FROM_SLVC_FULLSCR'importinge_grid = r_alv_grid.endif.if not r_alv_grid is initial.call method r_alv_grid->check_changed_data.endif.p_selfield-refresh = 'X'." save data to dbcall function 'ENQUEUE_EZ_SPFLI'exportingmode_spfli     = 'E'mandt          = sy-mandt_scope         = '2'exceptionsforeign_lock   = 1system_failure = 2others         = 3.if sy-subrc <> 0.message id sy-msgid type sy-msgty number sy-msgnowith sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.else.update spfli from table gt_spfli.if sy-subrc is initial.message 'Data was saved successfully.' type 'S'.else.message 'Data was not saved, errors ocured.' type 'S'.endif.call function 'DEQUEUE_EZ_SPFLI'exportingmode_spfli = 'E'mandt      = sy-mandt_scope     = '3'.endif.endcase.
endform.                    "frm_user_command

ALV 实现通用表编辑界面

通过动态编程,我们也可以实现对任意表的编辑和保存。新的知识点主要是利用 ABAP 的动态编程语法。在选择屏幕中,输入表名和选择条件:


SAP 展示选择的数据,双击其中一行,在对话框中显示该行:


在弹出的对话矿中,编辑数据后回到主界面,点击保存按钮将数据保存到数据库。

下面是完整代码:

report  z_falv_010.type-pools: slis.data: gt_fieldcat type slis_t_fieldcat_alv,gs_fieldcat type slis_fieldcat_alv.data: gt_index   type standard table of i with header line,table_name like dd02l-tabname.data: report_id like sy-repid.data: dy_table   type ref to data,dy_temptab type ref to data, " temporary table for saving to dbdy_line    type ref to data.field-symbols: <dyn_table>    type standard table,<dyn_wa>       type any,<dyn_field>    type any,<dyn_tab_temp> type standard table.*------------------------------------------------------
* Screen
*------------------------------------------------------
parameters: p_tab(30) type c obligatory, " table namep_cri     type string.       " criteria*------------------------------------------------------
* Events
*------------------------------------------------------
start-of-selection.report_id = sy-repid.table_name = p_tab. " table nameperform frm_check_if_table_exists using table_name.perform frm_get_data.perform frm_disp_data.*&---------------------------------------------------------------------*
*&      Form  frm_check_if_table_exists
*&---------------------------------------------------------------------*
form frm_check_if_table_exists using p_tabname like dd02l-tabname.data: l_tabname like dd02l-tabname.select single tabname from dd02l into l_tabnamewhere tabname = table_name.if not sy-subrc is initial.message ' No table was found.' type 'E'.leave program.endif.
endform.                    "frm_check_if_table_exists*&---------------------------------------------------------------------*
*&      Form  frm_get_data
*&---------------------------------------------------------------------*
form frm_get_data." Create internal table dynamically from table name input in selection screencreate data dy_table type standard table of (table_name).assign dy_table->* to <dyn_table>." Create another temporary tablecreate data dy_temptab type standard table of (table_name).assign dy_temptab->* to <dyn_tab_temp>." Create work area for the tablecreate data dy_line like line of <dyn_table>.assign dy_line->* to <dyn_wa>.select * from (table_name) into table <dyn_table>where (p_cri).refresh <dyn_tab_temp>.
endform.                    "frm_get_data*&---------------------------------------------------------------------*
*&      Form  frm_disp_data
*&---------------------------------------------------------------------*
form frm_disp_data.sort gt_fieldcat by col_pos." Display table in ALVcall function 'REUSE_ALV_LIST_DISPLAY'exportingi_callback_program       = report_idi_structure_name         = table_namei_callback_user_command  = 'FRM_USER_COMMAND'i_callback_pf_status_set = 'FRM_SET_PF_STATUS'tablest_outtab                 = <dyn_table>exceptionsprogram_error            = 1others                   = 2.
endform.                    "frm_disp_data*&---------------------------------------------------------------------*
*&      Form  frm_set_pf_status
*&---------------------------------------------------------------------*
form frm_set_pf_status using rt_extab type slis_t_extab.set pf-status 'Z_STANDARD'.
endform.                    "SET_PF_STATUS*&---------------------------------------------------------------------*
*&      Form  frm_user_command
*&---------------------------------------------------------------------*
form frm_user_command using r_ucomm     like sy-ucommrs_selfield type slis_selfield.data: li_tab type ref to data,l_line type ref to data.field-symbols:<l_tab> type table,<l_wa>  type any." Create tablecreate data li_tab type standard table of (table_name).assign li_tab->* to <l_tab>." Create work areacreate data l_line like line of <l_tab>.assign l_line->* to <l_wa>.case r_ucomm." When a record is selectedwhen '&IC1'." Read the selected recordread table <dyn_table> assigning <dyn_wa> index rs_selfield-tabindex.if sy-subrc = 0." Store the record in an internal tableappend <dyn_wa> to <l_tab>." Fetch the field catalog infocall function 'REUSE_ALV_FIELDCATALOG_MERGE'exportingi_program_name         = report_idi_structure_name       = table_namechangingct_fieldcat            = gt_fieldcatexceptionsinconsistent_interface = 1program_error          = 2others                 = 3.if sy-subrc = 0." Make all the fields input enabled except key fieldsgs_fieldcat-input = 'X'.modify gt_fieldcat from gs_fieldcat transporting inputwhere key is initial.endif." Display the record for editing purposecall function 'REUSE_ALV_LIST_DISPLAY'exportingi_callback_program    = report_idi_structure_name      = table_nameit_fieldcat           = gt_fieldcati_screen_start_column = 10i_screen_start_line   = 15i_screen_end_column   = 200i_screen_end_line     = 20tablest_outtab              = <l_tab>exceptionsprogram_error         = 1others                = 2.if sy-subrc = 0." Read the modified dataread table <l_tab> index 1 into <l_wa>." If the record is changed then track its index no." and populate it in an internal table for future actionif sy-subrc = 0 and <dyn_wa> <> <l_wa>.<dyn_wa> = <l_wa>.gt_index = rs_selfield-tabindex.append gt_index.endif.endif.endif.when 'SAVE'." Sort the index tablesort gt_index." Delete all duplicate recordsdelete adjacent duplicates from gt_index.loop at gt_index." Find out the changes in the internal table" and populate these changes in another internal tableread table <dyn_table> assigning <dyn_wa> index gt_index.if sy-subrc = 0.append <dyn_wa> to <dyn_tab_temp>.endif.endloop." Lock the tablecall function 'ENQUEUE_E_TABLE'exportingmode_rstable   = 'E'tabname        = table_nameexceptionsforeign_lock   = 1system_failure = 2others         = 3.if sy-subrc = 0." Modify the database table with the changesmodify (table_name) from table <dyn_tab_temp>.refresh <dyn_tab_temp>." Unlock the tablecall function 'DEQUEUE_E_TABLE'exportingmode_rstable = 'E'tabname      = table_name.message 'Data was saved successfully' type 'S'.endif.endcase.rs_selfield-refresh = 'X'.
endform.                    "user_command

源代码

05-Edit data in ALV

参考

ALV-Editing and saving the edited values in Database(OOPS)

这篇关于Functional ALV系列 (05) - ALV 作为数据编辑界面的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/681033

相关文章

如何使用C#串口通讯实现数据的发送和接收

《如何使用C#串口通讯实现数据的发送和接收》本文详细介绍了如何使用C#实现基于串口通讯的数据发送和接收,通过SerialPort类,我们可以轻松实现串口通讯,并结合事件机制实现数据的传递和处理,感兴趣... 目录1. 概述2. 关键技术点2.1 SerialPort类2.2 异步接收数据2.3 数据解析2.

大数据spark3.5安装部署之local模式详解

《大数据spark3.5安装部署之local模式详解》本文介绍了如何在本地模式下安装和配置Spark,并展示了如何使用SparkShell进行基本的数据处理操作,同时,还介绍了如何通过Spark-su... 目录下载上传解压配置jdk解压配置环境变量启动查看交互操作命令行提交应用spark,一个数据处理框架

通过ibd文件恢复MySql数据的操作方法

《通过ibd文件恢复MySql数据的操作方法》文章介绍通过.ibd文件恢复MySQL数据的过程,包括知道表结构和不知道表结构两种情况,对于知道表结构的情况,可以直接将.ibd文件复制到新的数据库目录并... 目录第一种情况:知道表结构第二种情况:不知道表结构总结今天干了一件大事,安装1Panel导致原来服务

Jmeter如何向数据库批量插入数据

《Jmeter如何向数据库批量插入数据》:本文主要介绍Jmeter如何向数据库批量插入数据方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Jmeter向数据库批量插入数据Jmeter向mysql数据库中插入数据的入门操作接下来做一下各个元件的配置总结Jmete

MySQL InnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据

《MySQLInnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据》mysql的ibdata文件被误删、被恶意修改,没有从库和备份数据的情况下的数据恢复,不能保证数据库所有表数据... 参考:mysql Innodb表空间卸载、迁移、装载的使用方法注意!此方法只适用于innodb_fi

mysql通过frm和ibd文件恢复表_mysql5.7根据.frm和.ibd文件恢复表结构和数据

《mysql通过frm和ibd文件恢复表_mysql5.7根据.frm和.ibd文件恢复表结构和数据》文章主要介绍了如何从.frm和.ibd文件恢复MySQLInnoDB表结构和数据,需要的朋友可以参... 目录一、恢复表结构二、恢复表数据补充方法一、恢复表结构(从 .frm 文件)方法 1:使用 mysq

mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespace id不一致处理

《mysql8.0无备份通过idb文件恢复数据的方法、idb文件修复和tablespaceid不一致处理》文章描述了公司服务器断电后数据库故障的过程,作者通过查看错误日志、重新初始化数据目录、恢复备... 周末突然接到一位一年多没联系的妹妹打来电话,“刘哥,快来救救我”,我脑海瞬间冒出妙瓦底,电信火苲马扁.

golang获取prometheus数据(prometheus/client_golang包)

《golang获取prometheus数据(prometheus/client_golang包)》本文主要介绍了使用Go语言的prometheus/client_golang包来获取Prometheu... 目录1. 创建链接1.1 语法1.2 完整示例2. 简单查询2.1 语法2.2 完整示例3. 范围值

javaScript在表单提交时获取表单数据的示例代码

《javaScript在表单提交时获取表单数据的示例代码》本文介绍了五种在JavaScript中获取表单数据的方法:使用FormData对象、手动提取表单数据、使用querySelector获取单个字... 方法 1:使用 FormData 对象FormData 是一个方便的内置对象,用于获取表单中的键值

Rust中的BoxT之堆上的数据与递归类型详解

《Rust中的BoxT之堆上的数据与递归类型详解》本文介绍了Rust中的BoxT类型,包括其在堆与栈之间的内存分配,性能优势,以及如何利用BoxT来实现递归类型和处理大小未知类型,通过BoxT,Rus... 目录1. Box<T> 的基础知识1.1 堆与栈的分工1.2 性能优势2.1 递归类型的问题2.2