2008年(49)
分类:
2008-03-24 11:18:40
*&---------------------------------------------------------------------*
*& Include Z_MY_GRID_DEFINE
*&---------------------------------------------------------------------*
*************************************************************************
* 在PAI时,get_modified_cells能够找到修改了的单元, 不能在PBO里用.
* (把grid改为edit模式, 那么在grid上修改了的内容,会在下一个PBO的时候自动更新内表),该句错误,
更正:改为edit模式,pbo的时候内表并不会自动更新,而是要要在ALV的class刷新(ALV自身的刷新并不会触发屏幕的PAI和PBO,如双击ALV,或者点击ALV自身的功能按钮)之后,再触发屏幕的PAI就可以看到内表自动改变了。
* 本程序用了get_changed_cells函数来获得改变了的cell,从而达到"主键不能被修改"的效果
* 本程序没有写保存更新的代码
*************************************************************************
DATA BEGIN OF my_wa.
INCLUDE STRUCTURE zstudent.
DATA: delete_button(3),
update_button(5),
cell_color TYPE lvc_t_scol,
cell_style TYPE lvc_t_styl.
DATA END OF my_wa.
DATA my_tab LIKE TABLE OF my_wa WITH KEY stu_no.
DATA BEGIN OF my_tab2 OCCURS 0.
INCLUDE STRUCTURE zstudent.
DATA END OF my_tab2.
*----------------------------------------------------------------------*
* CLASS my_grid DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS my_grid DEFINITION INHERITING FROM cl_gui_alv_grid.
PUBLIC SECTION.
METHODS handle_button_click FOR EVENT button_click OF cl_gui_alv_grid
IMPORTING
es_col_id
es_row_no .
***** 因为get_modified_cells是protected的方法,所以要在派生类中的public方法中来调
METHODS get_changed_cells EXPORTING cell_tab TYPE LVC_T_MOCE.
ENDCLASS. "my_grid DEFINITION
*----------------------------------------------------------------------*
* CLASS my_grid IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS my_grid IMPLEMENTATION.
METHOD handle_button_click.
MESSAGE i000(o0) WITH 'Rowid:' es_row_no-row_id '----Columnid:' es_col_id-fieldname.
IF NOT my_tab IS INITIAL.
IF es_col_id-fieldname <> 'DELETE_BUTTON'.
EXIT.
ENDIF.
READ TABLE my_tab INTO my_wa INDEX es_row_no-row_id.
DELETE my_tab INDEX es_row_no-row_id.
DELETE FROM zstudent WHERE stu_no = my_wa-stu_no.
CLEAR my_wa.
ENDIF.
ENDMETHOD. "handle_button_click
METHOD get_changed_cells.
CALL METHOD me->get_modified_cells
IMPORTING
CELL_TABLE = cell_tab.
ENDMETHOD.
ENDCLASS. "my_grid IMPLEMENTATION
*&---------------------------------------------------------------------*
*& Report Z_ALV_CELL_COLOR_STYLE_DEMO
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT z_alv_cell_color_style_demo.
INCLUDE z_my_grid_define.
DATA: cell_color_wa TYPE LINE OF lvc_t_scol,
cell_style_wa TYPE LINE OF lvc_t_styl,
layout TYPE lvc_s_layo,
fcat_tab TYPE lvc_t_fcat,
fcat_wa TYPE lvc_s_fcat,
container TYPE REF TO cl_gui_custom_container,
grid TYPE REF TO my_grid,
ok_code TYPE sy-ucomm,
save_ok TYPE sy-ucomm,
cell_tab TYPE lvc_t_moce,
cell_tab_wa LIKE LINE OF cell_tab.
INITIALIZATION.
***************** 选择数据到内表 设定layout fieldcat ******************
SELECT * FROM zstudent INTO CORRESPONDING FIELDS OF my_wa.
my_wa-delete_button = '删 除'.
my_wa-update_button = '保存更新'.
IF my_wa-stu_name = 'LEE'.
cell_color_wa-fname = 'STU_AGE'.
cell_color_wa-color-col = 6.
APPEND cell_color_wa TO my_wa-cell_color.
CLEAR cell_color_wa.
ENDIF.
cell_style_wa-fieldname = 'DELETE_BUTTON'.
cell_style_wa-style = cl_gui_alv_grid=>mc_style_button.
APPEND cell_style_wa TO my_wa-cell_style.
cell_style_wa-fieldname = 'UPDATE_BUTTON'.
cell_style_wa-style = cl_gui_alv_grid=>mc_style_button.
APPEND cell_style_wa TO my_wa-cell_style.
APPEND my_wa TO my_tab.
MOVE-CORRESPONDING my_wa TO my_tab2.
APPEND my_tab2.
CLEAR my_wa.
ENDSELECT.
layout-ctab_fname = 'CELL_COLOR'.
layout-stylefname = 'CELL_STYLE'.
layout-edit = 'X'.
* layout-EDIT_MODE = 'X'.
fcat_wa-fieldname = 'STU_NO'.
fcat_wa-scrtext_m = '学号'.
fcat_wa-just = 'L'.
APPEND fcat_wa TO fcat_tab.
CLEAR fcat_wa.
fcat_wa-fieldname = 'STU_NAME'.
fcat_wa-scrtext_m = '姓名'.
APPEND fcat_wa TO fcat_tab.
CLEAR fcat_wa.
fcat_wa-fieldname = 'STU_AGE'.
fcat_wa-scrtext_m = '年龄'.
fcat_wa-just = 'L'.
APPEND fcat_wa TO fcat_tab.
CLEAR fcat_wa.
fcat_wa-fieldname = 'DELETE_BUTTON'.
fcat_wa-outputlen = 8.
fcat_wa-just = 'C'.
APPEND fcat_wa TO fcat_tab.
fcat_wa-fieldname = 'UPDATE_BUTTON'.
fcat_wa-outputlen = 8.
fcat_wa-just = 'C'.
APPEND fcat_wa TO fcat_tab.
START-OF-SELECTION.
CALL SCREEN 9000.
*&---------------------------------------------------------------------*
*& Module STATUS_9000 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE status_9000 OUTPUT.
SET PF-STATUS 'STATUS_9000'.
SET TITLEBAR 'TITLEBAR_9000'.
IF container IS INITIAL.
CREATE OBJECT container
EXPORTING
container_name = 'MY_CONTAINER'.
CREATE OBJECT grid
EXPORTING
i_parent = container
i_appl_events = 'X'.
CALL METHOD grid->set_table_for_first_display
EXPORTING
is_layout = layout
CHANGING
it_outtab = my_tab
it_fieldcatalog = fcat_tab.
SET HANDLER grid->handle_button_click FOR grid.
ELSE.
LOOP AT cell_tab INTO cell_tab_wa.
IF cell_tab_wa-col_id = 1.
READ TABLE my_tab2 INDEX cell_tab_wa-row_id.
READ TABLE my_tab INTO my_wa INDEX cell_tab_wa-row_id.
my_wa-stu_no = my_tab2-stu_no.
DELETE my_tab INDEX cell_tab_wa-row_id.
INSERT my_wa INTO my_tab INDEX cell_tab_wa-row_id.
ENDIF.
ENDLOOP.
CALL METHOD grid->refresh_table_display.
ENDIF.
ENDMODULE. " STATUS_9000 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_9000 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE user_command_9000 INPUT.
CALL METHOD grid->get_changed_cells
IMPORTING
cell_tab = cell_tab.
save_ok = ok_code.
CLEAR ok_code.
CASE save_ok.
WHEN 'BACK'.
SET SCREEN 0.
ENDCASE.
ENDMODULE. " USER_COMMAND_9000 INPUT