实现Table Maintain

通过类CL_GUI_ALV_GRID,实现Table Maintain功能。

实现效果:

1.创建Program;

2.创建空Screen 100;

3.创建GUI Status;

Program:实例代码

"cl_gui_alv_grid事件
CLASS zcl_tabm_event_handler DEFINITION.
PUBLIC SECTION.
"user command
CLASS-METHODS:m_user_command FOR EVENT user_command OF cl_gui_alv_grid
IMPORTING e_ucomm.
"data changed
CLASS-METHODS:m_data_changed FOR EVENT data_changed OF cl_gui_alv_grid
IMPORTING er_data_changed
e_onf4
e_onf4_before
e_onf4_after
e_ucomm.
ENDCLASS.
CLASS zcl_tabm_event_handler IMPLEMENTATION.
METHOD m_user_command.
DATA:lv_mes TYPE string.
lv_mes = 'function:' && e_ucomm.
MESSAGE lv_mes TYPE 'I'.
ENDMETHOD.
METHOD m_data_changed.
DATA:lt_mod_data TYPE lvc_t_modi.
DATA:ls_mod_data TYPE lvc_s_modi. lt_mod_data = er_data_changed->mt_mod_cells. "循環更改數據
LOOP AT lt_mod_data INTO ls_mod_data.
* "設置錯誤信息
* er_data_changed->add_protocol_entry(
* EXPORTING
* i_msgid = '00'
* i_msgty = 'E'
* i_msgno = '001'
* i_msgv1 = '錯誤信息!'
* i_fieldname = ls_mod_data-fieldname
* ).
"更新數據表
er_data_changed->modify_cell(
EXPORTING
i_row_id = ls_mod_data-row_id
i_fieldname = ls_mod_data-fieldname
i_value = ls_mod_data-value
).
ENDLOOP.
ENDMETHOD. ENDCLASS. CLASS zcl_tab_maintain DEFINITION.
PUBLIC SECTION.
"table name
DATA:lv_tabname TYPE ddobjname.
DATA:lo_container TYPE REF TO cl_gui_docking_container.
DATA:lo_alv TYPE REF TO cl_gui_alv_grid.
"table data
DATA:lo_table TYPE REF TO DATA.
"alv field字段
DATA:lt_fcat TYPE lvc_t_fcat.
DATA:ls_fcat TYPE lvc_s_fcat.
"alv layout
DATA:ls_layout TYPE lvc_s_layo.
"table status
DATA:lv_tab_status TYPE C LENGTH 1. "C為修改,空為顯示,A為添加
"保存table結構類型,line結構體類型
DATA:lo_tabledescr TYPE REF TO cl_abap_tabledescr.
DATA:lo_structdescr TYPE REF TO cl_abap_structdescr. METHODS:constructor IMPORTING iv_tabname TYPE ddobjname.
METHODS:m_init_alv.
METHODS:m_show_alv.
METHODS:m_get_fcat RETURNING VALUE(et_fcat) TYPE lvc_t_fcat.
METHODS:m_set_fcat IMPORTING it_fcat TYPE lvc_t_fcat.
METHODS:m_get_layout RETURNING VALUE(es_layout) TYPE lvc_s_layo.
METHODS:m_set_layout IMPORTING is_layout TYPE lvc_s_layo.
METHODS:m_set_status IMPORTING iv_status TYPE char1.
METHODS:m_get_status RETURNING VALUE(ev_status) TYPE char1.
METHODS:m_refresh_alv.
"some action
METHODS:m_edit_action.
METHODS:m_add_action.
METHODS:m_del_action.
METHODS:m_save_action.
PRIVATE SECTION.
METHODS:m_get_data IMPORTING iv_tab_name TYPE ddobjname.
METHODS:m_build_filedcat IMPORTING iv_structure_name TYPE ddobjname.
METHODS:m_build_layout.
METHODS:m_set_events.
ENDCLASS. CLASS zcl_tab_maintain IMPLEMENTATION.
METHOD:constructor.
DATA:lv_msg TYPE string.
DATA:lo_struct_type TYPE REF TO cl_abap_structdescr.
DATA:lo_table_type TYPE REF TO cl_abap_tabledescr.
"自定義字段component
DATA:lt_comp_tab TYPE cl_abap_structdescr=>component_table.
DATA:ls_comp_tab LIKE LINE OF lt_comp_tab.
DATA:lo_exception TYPE REF TO cx_root.
DATA:lv_gotstate TYPE ddgotstate.
TRY.
"創建表名
lv_tabname = iv_tabname.
"檢查table是否存在
CALL FUNCTION 'DDIF_TABL_GET'
EXPORTING
NAME = lv_tabname
IMPORTING
GOTSTATE = lv_gotstate.
IF SY-SUBRC <> 0 OR lv_gotstate IS INITIAL.
lv_msg = 'Table:' && lv_tabname && ` do not exist!`.
MESSAGE lv_msg TYPE 'E'.
ENDIF. "創建table
lo_struct_type ?= cl_abap_typedescr=>describe_by_name( lv_tabname ).
lt_comp_tab = lo_struct_type->get_components( ). "最終結構創建table
lo_structdescr = cl_abap_structdescr=>create( lt_comp_tab ).
lo_tabledescr = cl_abap_tabledescr=>create( lo_structdescr ). "創建table
CREATE DATA lo_table TYPE HANDLE lo_tabledescr. CATCH cx_root INTO lo_exception.
lv_msg = lo_exception->get_text( ).
MESSAGE lv_msg TYPE 'E'.
ENDTRY.
ENDMETHOD.
METHOD:m_init_alv.
"創建容器
CREATE OBJECT lo_container
EXPORTING
dynnr = '100'
repid = sy-repid
extension = 1000.
"創建alv
IF lo_alv IS INITIAL.
CREATE OBJECT lo_alv
EXPORTING i_parent = lo_container.
ENDIF.
"獲取數據
me->m_get_data( lv_tabname ).
"fieldcat設置
me->m_build_filedcat( lv_tabname ).
"layout設置
me->m_build_layout( ).
"設置event
me->m_set_events( ).
ENDMETHOD.
METHOD:m_show_alv.
FIELD-SYMBOLS:<f_table> TYPE STANDARD TABLE.
ASSIGN lo_table->* TO <f_table>.
"顯示alv
lo_alv->set_table_for_first_display(
EXPORTING
"layout
is_layout = ls_layout
CHANGING
it_fieldcatalog = lt_fcat
it_outtab = <f_table> ).
ENDMETHOD.
"獲取,設置fieldcat
METHOD:m_get_fcat.
et_fcat = lt_fcat.
ENDMETHOD.
METHOD:m_set_fcat.
lt_fcat = it_fcat.
ENDMETHOD.
"獲取,設置layout
METHOD:m_get_layout.
es_layout = ls_layout.
ENDMETHOD.
METHOD:m_set_layout.
ls_layout = is_layout.
ENDMETHOD.
"獲取table status
METHOD:m_set_status.
lv_tab_status = iv_status.
ENDMETHOD.
METHOD:m_get_status.
ev_status = lv_tab_status.
ENDMETHOD. "獲取table數據
METHOD:m_get_data.
FIELD-SYMBOLS:<f_table> TYPE STANDARD TABLE.
ASSIGN lo_table->* TO <f_table>.
"查詢數據
SELECT * INTO CORRESPONDING FIELDS OF TABLE <f_table> FROM (iv_tab_name).
ENDMETHOD.
"自動生成fieldcat
METHOD:m_build_filedcat.
DATA:lv_index TYPE I.
FIELD-SYMBOLS:<f_fcat> TYPE lvc_s_fcat.
"fieldcat使用用戶設置
IF lt_fcat IS NOT INITIAL.
RETURN.
ENDIF. "獲取fieldcatalog
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = iv_structure_name
CHANGING
ct_fieldcat = lt_fcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE 'Get fieldcatlog failed!' TYPE 'E'.
RETURN.
ENDIF.
LOOP AT lt_fcat ASSIGNING <f_fcat>.
lv_index = sy-tabix.
<f_fcat>-col_pos = lv_index.
<f_fcat>-tabname = iv_structure_name.
ENDLOOP. ENDMETHOD.
"自動生成layout
METHOD:m_build_layout.
"編輯設置
ls_layout-edit = ''. "是否可編輯,整體控制
"顯示設置
ls_layout-cwidth_opt = 'X'. "是否自動列寬
ls_layout-no_toolbar = ''. "是否隱藏toolbar.
"選擇模式,默認單選,A:行,列選擇;B:單行選擇;C:多行選擇;D:單元格選擇
ls_layout-sel_mode = 'A'.
ENDMETHOD.
METHOD:m_set_events.
"設置function相應事件
SET HANDLER zcl_tabm_event_handler=>m_user_command FOR lo_alv.
SET HANDLER zcl_tabm_event_handler=>m_data_changed FOR lo_alv. "註冊enter事件
lo_alv->register_edit_event(
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_enter
).
"註冊修改單元格事件
lo_alv->register_edit_event(
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_modified
).
ENDMETHOD.
METHOD:m_refresh_alv.
lo_alv->refresh_table_display( ).
ENDMETHOD.
"edit action
METHOD:m_edit_action.
"alv field字段
FIELD-SYMBOLS:<f_fcat> TYPE lvc_s_fcat.
"修改edit狀態
LOOP AT lt_fcat ASSIGNING <f_fcat>.
"特定欄位控制
* IF <f_fcat>-fieldname = ''.
* <f_fcat>-edit = 'X'.
* ENDIF.
IF lv_tab_status = ''.
<f_fcat>-edit = 'X'.
ELSEIF lv_tab_status = 'C'.
<f_fcat>-edit = ''.
ENDIF.
ENDLOOP.
"設置status
IF lv_tab_status = ''.
lv_tab_status = 'C'.
ELSEIF lv_tab_status = 'C'.
lv_tab_status = ''.
ENDIF. "刷新
me->m_show_alv( ).
ENDMETHOD.
"添加空行
METHOD:m_add_action.
DATA:lo_line TYPE REF TO DATA.
FIELD-SYMBOLS:<f_table> TYPE STANDARD TABLE.
FIELD-SYMBOLS:<f_line> TYPE ANY.
"alv field字段
FIELD-SYMBOLS:<f_fcat> TYPE lvc_s_fcat. ASSIGN lo_table->* TO <f_table>.
CREATE DATA lo_line LIKE LINE OF <f_table>.
ASSIGN lo_line->* TO <f_line>.
INSERT <f_line> INTO <f_table> INDEX 1. "設置編輯狀態
lv_tab_status = 'A'.
"修改edit狀態
LOOP AT lt_fcat ASSIGNING <f_fcat>.
"特定欄位控制
* IF <f_fcat>-fieldname = ''.
* <f_fcat>-edit = 'X'.
* ENDIF. IF <f_fcat>-edit = 'X'.
EXIT.
ENDIF.
<f_fcat>-edit = 'X'.
ENDLOOP.
"刷新
me->m_show_alv( ).
ENDMETHOD.
"刪除操作
METHOD:m_del_action.
FIELD-SYMBOLS:<f_table> TYPE STANDARD TABLE.
FIELD-SYMBOLS:<f_del_table> TYPE STANDARD TABLE.
FIELD-SYMBOLS:<f_line> TYPE ANY.
DATA:lt_rows TYPE lvc_t_row.
DATA:ls_rows TYPE lvc_s_row.
DATA:lo_del_table TYPE REF TO DATA.
"創建table
CREATE DATA lo_del_table TYPE HANDLE lo_tabledescr. "獲取選擇行
lo_alv->get_selected_rows(
IMPORTING
et_index_rows = lt_rows
).
"獲取table,刪除選擇行
ASSIGN lo_table->* TO <f_table>.
ASSIGN lo_del_table->* TO <f_del_table>.
"獲取刪除內表
LOOP AT lt_rows INTO ls_rows.
READ TABLE <f_table> INDEX ls_rows-index ASSIGNING <f_line>.
APPEND <f_line> TO <f_del_table>.
ENDLOOP.
"刪除當前內表
LOOP AT lt_rows INTO ls_rows.
DELETE <f_table> INDEX ls_rows-index.
ENDLOOP.
DELETE (lv_tabname) FROM TABLE <f_del_table>.
"刷新
me->m_show_alv( ).
ENDMETHOD.
"保存操作
METHOD:m_save_action.
FIELD-SYMBOLS:<f_table> TYPE STANDARD TABLE.
"alv field字段
FIELD-SYMBOLS:<f_fcat> TYPE lvc_s_fcat. "檢查邏輯
"會調用data changed事件
"會彈出檢查相關錯誤信息
"lo_alv->check_changed_data( ). "獲取table
ASSIGN lo_table->* TO <f_table>. "更新數據表
MODIFY (lv_tabname) FROM TABLE <f_table>.
IF sy-subrc = 0.
MESSAGE 'Save success!' TYPE 'S'.
ENDIF. "設置編輯狀態
lv_tab_status = ''.
"修改edit狀態
LOOP AT lt_fcat ASSIGNING <f_fcat>.
"特定欄位控制
* IF <f_fcat>-fieldname = ''.
* <f_fcat>-edit = 'X'.
* ENDIF.
<f_fcat>-edit = ''.
ENDLOOP.
"刷新
me->m_show_alv( ).
ENDMETHOD.
ENDCLASS. "table maintain類對象
DATA:lo_tab_maintain TYPE REF TO zcl_tab_maintain. SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE text-001.
PARAMETERS:p_tab TYPE ddobjname. "maintain table name
SELECTION-SCREEN END OF BLOCK blk1. START-OF-SELECTION.
PERFORM f_show_alv. FORM f_show_alv.
CREATE OBJECT lo_tab_maintain
EXPORTING
iv_tabname = p_tab.
lo_tab_maintain->m_init_alv( ).
lo_tab_maintain->m_show_alv( ).
CALL SCREEN 100.
ENDFORM. *&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'STATUS_100'.
* SET TITLEBAR 'xxx'.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
DATA:g_ok_code TYPE sy-ucomm.
CASE g_ok_code.
WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
LEAVE PROGRAM.
WHEN 'EDIT'.
PERFORM f_edit_action.
WHEN 'ADD'.
PERFORM f_add_action.
WHEN 'DELETE'.
PERFORM f_delete_action.
WHEN 'SAVE'.
PERFORM f_save_action.
WHEN OTHERS.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT "edit action
FORM f_edit_action.
lo_tab_maintain->m_edit_action( ).
ENDFORM. "add action
FORM f_add_action.
lo_tab_maintain->m_add_action( ).
ENDFORM. "delete action
FORM f_delete_action.
lo_tab_maintain->m_del_action( ).
ENDFORM. "save action
FORM f_save_action.
lo_tab_maintain->m_save_action( ).
ENDFORM.

ABAP学习(34):cl_gui_alv_grid实现Table Maintain的更多相关文章

  1. SAP ABAP学习路线图--标准教程

    SAP ABAP学习路线图--标准教程 摘自:http://www.cnblogs.com/clsoho/archive/2010/07/05/1771400.html

  2. golang学习笔记12 beego table name `xxx` repeat register, must be unique 错误问题

    golang学习笔记12 beego table name `xxx` repeat register, must be unique 错误问题 今天测试了重新建一个项目生成新的表,然后复制到旧的项目 ...

  3. OpenFlow Switch学习笔记(五)——Group Table、Meter Table及Counters

    本文主要详述OpenFlow Switch的另外两个主要组件——Group Table和Meter Table,它们在整个OpenFlow Swtich Processing中也起到了重要作用. 1. ...

  4. Unity插件之NGUI学习(8)—— Table和NGUI尺寸转换为世界坐标系尺寸

    依据 Unity插件之NGUI学习(2),创建一个UI Root,在UI Root下创建一个Texture作为背景图,并设置图片,在Wiget下调整大小:然后在UI Root下再创建一个Panel. ...

  5. HTML学习笔记——列表和table

    1>有序列表.无序列表和自定义列表 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...

  6. bootstrap学习笔记<四>(table表格)

    表格 bootstrap为table表格定制多个常用样式:基本样式,隔行变色样式,带边框样式,荧光棒样式,紧凑样式,响应样式. ☑  .table:基础表格 ☑  .table-striped:斑马线 ...

  7. jquery学习笔记(4)--实现table隔行变色以及单选框选中

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  8. SAP ABAP规划 使用LOOP READ TABLE该方法取代双LOOP内部表的方法

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWlueXVlemhhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  9. Lua学习(1)——table

    table类型实现了“关联数组”.“关联数组”是一种具有特殊索引方式的数组.不仅可以通过证书来索引它,还可以使用字符串或其他类型(除了nil)来索引它.table是Lua中主要的数据结构机制(事实也是 ...

  10. AngularJS学习之旅—AngularJS Table(十一)

    1.AngularJS 表格 1. ng-repeat 指令可以完美的显示表格 AngularJS 实例 <!DOCTYPE html> <html> <head> ...

随机推荐

  1. python之利用logging模块封装python日志类

    利用python自带的logging模块封装一个日志类,便于单元测试时调用该模块打印日志 说明: 日志,即记录程序在运行过程中的操作记录和出现的问题 日志调试信息分类等级,由低到高分别为:DEBUG ...

  2. OneinStack基础搭建typecheo轻量级博客

    利用OneinStack搭建运行环境 1.访问oneinstack官网https://oneinstack.com/auto/,选择自动安装-自定义安装,根据自己需求生成linux 执行命令(这里我安 ...

  3. io流转换为Multipart文件

    io流转换为Multipart文件 个人的话是运用到了minio文件服务器保存文件,前端(vue)异步上传文件后,由于要提升用户体验效果,先上传文件到后台服务器,返回视频在文件服务器的link()参数 ...

  4. c# 调用User32.dll

    获取当前窗口句柄:GetForegroundWindow()[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelli ...

  5. MSSQL执行超大.sql脚本

    1.打开mssql安装路径:找到Microsoft SQL Server Management Studio的图标,点击右键属性>打开文件位置 2.在安装路径下打开cmd控制台 3.输入命令: ...

  6. 【DM论文阅读杂记】推荐系统 注意力机制

    Paper Title Real-time Attention Based Look-alike Model for Recommender System Basic algorithm and ma ...

  7. C语言源文件如何编译为exe

    先观看https://blog.csdn.net/u014772182/article/details/43348465来配置gcc环境随后在cmd中cd到目标文件夹gcc main.c -o ste ...

  8. sentinel监控数据持久化&本地测试

    官方文档  https://sentinelguard.io/zh-cn/ wiki: 在生产环境中使用-Sentinel 推荐方案:持久化到 时序数据库InfluxDB  : 结合Grafana 可 ...

  9. 软件工程日报八——AlertDiatog的使用

    今天学习了AlertDiatog的相关内容 AlertDialog可以在当前的界面上显示一个对话框,这个对话框是置顶于所有界面元素之上的,能够屏蔽掉其他控件的交互能力,因此AlertDialog一般是 ...

  10. 问题记录_IDEA版本2021.2.3_debug时变量显示不全(只显示线程附近部分)

    问题记录_IDEA版本2021.2.3_debug时变量显示不全(只显示线程附近部分) 起因 把IDEA的版本从2019升级到了2021.2.3,在debug的时候发现变量的值能看到的很少,并不像20 ...