使用REUSE_ALV_GRID_DISPLAY_LVC显示ALV时,如何将列、行或者具体的某个单元格设置成可编辑能输入状态?这个其实很简单,下面一一讲解:
1, 列可编辑
只要将catalog中的字段edit设置成‘X',整列就变成能输入的了,LVC_S_FCAT-EDIT
代码:
ABAP
REPORT ztest_alv_lvc_edit.
TYPES:BEGIN OF gty_ekko,
ebeln TYPE ekko-ebeln,
verkf TYPE ekko-verkf,
END OF gty_ekko.
DATA:git_ekko TYPE STANDARD TABLE OF gty_ekko.
DATA:git_fcat TYPE lvc_t_fcat,
gwa_fcat LIKE LINE OF git_fcat,
gwa_layout TYPE lvc_s_layo.
DATA:l_pos TYPE i VALUE 1.
START-OF-SELECTION.
SELECT ebeln
verkf
INTO TABLE git_ekko
FROM ekko
UP TO 10 ROWS.
CLEAR: l_pos.
l_pos = l_pos + 1.
gwa_fcat-coltext = 'PO'.
gwa_fcat-scrtext_l = 'PO'.
gwa_fcat-scrtext_m = 'PO'.
gwa_fcat-scrtext_s = 'PO'.
gwa_fcat-fieldname = 'EBELN'.
gwa_fcat-col_pos = l_pos.
gwa_fcat-outputlen = '10'.
APPEND gwa_fcat TO git_fcat.
l_pos = l_pos + 1.
gwa_fcat-coltext = 'PO item'.
gwa_fcat-scrtext_l = 'PO item'.
gwa_fcat-scrtext_m = 'PO item'.
gwa_fcat-scrtext_s = 'PO item'.
gwa_fcat-fieldname = 'VERKF'.
gwa_fcat-col_pos = l_pos.
gwa_fcat-edit = 'X'. "设置列为可编辑状态
gwa_fcat-outputlen = '20'.
APPEND gwa_fcat TO git_fcat.
gwa_layout-zebra = 'X'.
gwa_layout-sel_mode = 'A'.
gwa_layout-cwidth_opt = 'X'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
is_layout_lvc = gwa_layout
it_fieldcat_lvc = git_fcat
TABLES
t_outtab = git_ekko[]
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc = 0.
* BREAK-POINT.
ENDIF.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
REPORTztest_alv_lvc_edit.
TYPES:BEGIN OFgty_ekko,
ebelnTYPEekko-ebeln,
verkfTYPEekko-verkf,
END OFgty_ekko.
DATA:git_ekkoTYPESTANDARD TABLE OFgty_ekko.
DATA:git_fcatTYPElvc_t_fcat,
gwa_fcatLIKELINEOFgit_fcat,
gwa_layoutTYPElvc_s_layo.
DATA:l_posTYPEiVALUE1.
START-OF-SELECTION.
SELECTebeln
verkf
INTOTABLEgit_ekko
FROMekko
UP TO10ROWS.
CLEAR:l_pos.
l_pos=l_pos+1.
gwa_fcat-coltext='PO'.
gwa_fcat-scrtext_l='PO'.
gwa_fcat-scrtext_m='PO'.
gwa_fcat-scrtext_s='PO'.
gwa_fcat-fieldname='EBELN'.
gwa_fcat-col_pos=l_pos.
gwa_fcat-outputlen='10'.
APPENDgwa_fcatTOgit_fcat.
l_pos=l_pos+1.
gwa_fcat-coltext='PO item'.
gwa_fcat-scrtext_l='PO item'.
gwa_fcat-scrtext_m='PO item'.
gwa_fcat-scrtext_s='PO item'.
gwa_fcat-fieldname='VERKF'.
gwa_fcat-col_pos=l_pos.
gwa_fcat-edit='X'."设置列为可编辑状态
gwa_fcat-outputlen='20'.
APPENDgwa_fcatTOgit_fcat.
gwa_layout-zebra='X'.
gwa_layout-sel_mode='A'.
gwa_layout-cwidth_opt='X'.
CALL FUNCTION'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program=sy-repid
is_layout_lvc=gwa_layout
it_fieldcat_lvc=git_fcat
TABLES
t_outtab=git_ekko[]
EXCEPTIONS
program_error=1
OTHERS=2.
IFsy-subrc=0.
*BREAK-POINT.
ENDIF.
运行结果:
2, 整个ALV可编辑
将layout中的EDIT字段赋值’X'(LVC_S_LAYO-EDIT),就可以将整个ALV设置成可输入状态。
还有一种比较笨的办法就是将所有列都设置成可输入。
代码:
ABAP
gwa_layout-zebra = 'X'.
gwa_layout-sel_mode = 'A'.
gwa_layout-cwidth_opt = 'X'.
gwa_layout-edit = 'X'. "将整个ALV设置成可编辑
gwa_glay-edt_cll_cb = 'X'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
is_layout_lvc = gwa_layout
it_fieldcat_lvc = git_fcat
* i_grid_settings = gwa_glay
TABLES
t_outtab = git_ekko[]
EXCEPTIONS
program_error = 1
OTHERS = 2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
gwa_layout-zebra='X'.
gwa_layout-sel_mode='A'.
gwa_layout-cwidth_opt='X'.
gwa_layout-edit='X'."将整个ALV设置成可编辑
gwa_glay-edt_cll_cb='X'.
CALL FUNCTION'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program=sy-repid
is_layout_lvc=gwa_layout
it_fieldcat_lvc=git_fcat
*i_grid_settings= gwa_glay
TABLES
t_outtab=git_ekko[]
EXCEPTIONS
program_error=1
OTHERS=2.
3, 单元格可编辑
如果要让单元格变成可输入,需要在alv输出内表中多定义一个保存单元格style的字段(类型:lvc_t_styl ),然后填入style属性(可编辑或者是不可编辑),最后还要在layout-stylefname中指定style的字段名。
代码如下:将EBELN设置成不可输入。
ABAP
TYPES:BEGIN OF gty_ekko,
ebeln TYPE ekko-ebeln,
verkf TYPE ekko-verkf,
style TYPE lvc_t_styl, "将该字段设置为保存编辑状态的字段
END OF gty_ekko.
1
2
3
4
5
TYPES:BEGIN OFgty_ekko,
ebelnTYPEekko-ebeln,
verkfTYPEekko-verkf,
styleTYPElvc_t_styl,"将该字段设置为保存编辑状态的字段
END OFgty_ekko.
ABAP
gwa_edit-fieldname = 'EBELN'.
gwa_edit-style = cl_gui_alv_grid=>mc_style_disabled."设置为不可输入
INSERT gwa_edit INTO TABLE git_edit.
1
2
3
gwa_edit-fieldname='EBELN'.
gwa_edit-style=cl_gui_alv_grid=>mc_style_disabled."设置为不可输入
INSERTgwa_editINTOTABLEgit_edit.
ABAP
gwa_layout-stylefname = 'STYLE'. "指定保存编辑状态的字段名
1
gwa_layout-stylefname='STYLE'."指定保存编辑状态的字段名
style属性可以在类CL_GUI_ALV_GRID中可以找到:
MC_STYLE_DISABLED:禁止输入
MC_STYLE_ENABLED:可编辑,可输入。
需要注意的是:MC_STYLE_ENABLED输入性不能单独使用,一定在EDIT = 'X'(layout-edit 或者是catalog-edit)的场合下,才可以利用MC_STYLE_ENABLED进行不可编辑到编辑切换,也就是不设置edit = 'X',MC_STYLE_ENABLED是不起作用的。
完整代码:
ABAP
REPORT ztest_alv_lvc_edit.
TYPES:BEGIN OF gty_ekko,
ebeln TYPE ekko-ebeln,
verkf TYPE ekko-verkf,
style TYPE lvc_t_styl, "设置编辑状态字段
END OF gty_ekko.
DATA:git_ekko TYPE STANDARD TABLE OF gty_ekko,
gwa_ekko TYPE gty_ekko.
DATA:git_fcat TYPE lvc_t_fcat,
gwa_fcat LIKE LINE OF git_fcat,
gwa_layout TYPE lvc_s_layo,
gwa_glay TYPE lvc_s_glay.
DATA:gwa_edit TYPE lvc_s_styl,
git_edit TYPE lvc_t_styl.
START-OF-SELECTION.
SELECT ebeln
verkf
INTO CORRESPONDING FIELDS OF TABLE git_ekko
FROM ekko
UP TO 10 ROWS.
gwa_edit-fieldname = 'VERKF'.
gwa_edit-style = cl_gui_alv_grid=>mc_style_disabled."设置为不可编辑
INSERT gwa_edit INTO TABLE git_edit.
INSERT LINES OF git_edit INTO TABLE gwa_ekko-style.
* 将除第三行的之外所有行的VERKF字段都设置成不能输入
DO 10 TIMES.
IF sy-index <> 3.
MODIFY git_ekko INDEX sy-index FROM gwa_ekko TRANSPORTING style.
ENDIF.
ENDDO.
PERFORM frm_set_catalog.
gwa_layout-zebra = 'X'.
gwa_layout-sel_mode = 'A'.
gwa_layout-cwidth_opt = 'X'.
gwa_layout-edit = 'X'. "将整个ALV设置成可编辑
gwa_layout-stylefname = 'STYLE'. "指定保存编辑状态的字段名
gwa_glay-edt_cll_cb = 'X'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
is_layout_lvc = gwa_layout
it_fieldcat_lvc = git_fcat
* i_grid_settings = gwa_glay
TABLES
t_outtab = git_ekko[]
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc = 0.
ENDIF.
*&---------------------------------------------------------------------*
*& Form FRM_SET_CATALOG
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
*
*----------------------------------------------------------------------*
FORM frm_set_catalog .
DATA:l_pos TYPE i VALUE 1.
CLEAR: l_pos.
l_pos = l_pos + 1.
gwa_fcat-coltext = 'PO'.
gwa_fcat-scrtext_l = 'PO'.
gwa_fcat-scrtext_m = 'PO'.
gwa_fcat-scrtext_s = 'PO'.
gwa_fcat-fieldname = 'EBELN'.
gwa_fcat-col_pos = l_pos.
gwa_fcat-outputlen = '10'.
APPEND gwa_fcat TO git_fcat.
l_pos = l_pos + 1.
gwa_fcat-coltext = 'PO item'.
gwa_fcat-scrtext_l = 'PO item'.
gwa_fcat-scrtext_m = 'PO item'.
gwa_fcat-scrtext_s = 'PO item'.
gwa_fcat-fieldname = 'VERKF'.
gwa_fcat-edit = 'X'. "将这列设置为可编辑
gwa_fcat-col_pos = l_pos.
gwa_fcat-outputlen = '20'.
APPEND gwa_fcat TO git_fcat.
ENDFORM.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
REPORTztest_alv_lvc_edit.
TYPES:BEGIN OFgty_ekko,
ebelnTYPEekko-ebeln,
verkfTYPEekko-verkf,
styleTYPElvc_t_styl,"设置编辑状态字段
END OFgty_ekko.
DATA:git_ekkoTYPESTANDARD TABLE OFgty_ekko,
gwa_ekkoTYPEgty_ekko.
DATA:git_fcatTYPElvc_t_fcat,
gwa_fcatLIKELINEOFgit_fcat,
gwa_layoutTYPElvc_s_layo,
gwa_glayTYPElvc_s_glay.
DATA:gwa_editTYPElvc_s_styl,
git_editTYPElvc_t_styl.
START-OF-SELECTION.
SELECTebeln
verkf
INTOCORRESPONDING FIELDS OF TABLEgit_ekko
FROMekko
UP TO10ROWS.
gwa_edit-fieldname='VERKF'.
gwa_edit-style=cl_gui_alv_grid=>mc_style_disabled."设置为不可编辑
INSERTgwa_editINTOTABLEgit_edit.
INSERTLINESOFgit_editINTOTABLEgwa_ekko-style.
* 将除第三行的之外所有行的VERKF字段都设置成不能输入
DO10TIMES.
IFsy-index<>3.
MODIFYgit_ekkoINDEXsy-indexFROMgwa_ekkoTRANSPORTINGstyle.
ENDIF.
ENDDO.
PERFORMfrm_set_catalog.
gwa_layout-zebra='X'.
gwa_layout-sel_mode='A'.
gwa_layout-cwidth_opt='X'.
gwa_layout-edit='X'."将整个ALV设置成可编辑
gwa_layout-stylefname='STYLE'."指定保存编辑状态的字段名
gwa_glay-edt_cll_cb='X'.
CALL FUNCTION'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program=sy-repid
is_layout_lvc=gwa_layout
it_fieldcat_lvc=git_fcat
*i_grid_settings= gwa_glay
TABLES
t_outtab=git_ekko[]
EXCEPTIONS
program_error=1
OTHERS=2.
IFsy-subrc=0.
ENDIF.
*&---------------------------------------------------------------------*
*&FormFRM_SET_CATALOG
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
*-->p1text
*
*----------------------------------------------------------------------*
FORMfrm_set_catalog.
DATA:l_posTYPEiVALUE1.
CLEAR:l_pos.
l_pos=l_pos+1.
gwa_fcat-coltext='PO'.
gwa_fcat-scrtext_l='PO'.
gwa_fcat-scrtext_m='PO'.
gwa_fcat-scrtext_s='PO'.
gwa_fcat-fieldname='EBELN'.
gwa_fcat-col_pos=l_pos.
gwa_fcat-outputlen='10'.
APPENDgwa_fcatTOgit_fcat.
l_pos=l_pos+1.
gwa_fcat-coltext='PO item'.
gwa_fcat-scrtext_l='PO item'.
gwa_fcat-scrtext_m='PO item'.
gwa_fcat-scrtext_s='PO item'.
gwa_fcat-fieldname='VERKF'.
gwa_fcat-edit='X'."将这列设置为可编辑
gwa_fcat-col_pos=l_pos.
gwa_fcat-outputlen='20'.
APPENDgwa_fcatTOgit_fcat.
ENDFORM.
4, 行可编辑
类似于单元格可编辑,只要将一行之外的所有行都变成不可输入,也就实现了行可编辑。逻辑如下:
1,通过layout-edit = ’X' 将整个行项目都变成可输入
2,将某行之外的所有行style设置成 cl_gui_alv_grid=>mc_style_disabled.
代码:
ABAP
REPORT ztest_alv_lvc_edit.
TYPES:BEGIN OF gty_ekko,
ebeln TYPE ekko-ebeln,
verkf TYPE ekko-verkf,
style TYPE lvc_t_styl, "设置编辑状态字段
END OF gty_ekko.
DATA:git_ekko TYPE STANDARD TABLE OF gty_ekko,
gwa_ekko TYPE gty_ekko.
DATA:git_fcat TYPE lvc_t_fcat,
gwa_fcat LIKE LINE OF git_fcat,
gwa_layout TYPE lvc_s_layo,
gwa_glay TYPE lvc_s_glay.
DATA:gwa_edit TYPE lvc_s_styl,
git_edit TYPE lvc_t_styl.
START-OF-SELECTION.
SELECT ebeln
verkf
INTO CORRESPONDING FIELDS OF TABLE git_ekko
FROM ekko
UP TO 10 ROWS.
gwa_edit-fieldname = 'EBELN'.
gwa_edit-style = cl_gui_alv_grid=>mc_style_disabled.
INSERT gwa_edit INTO TABLE git_edit.
gwa_edit-fieldname = 'VERKF'.
gwa_edit-style = cl_gui_alv_grid=>mc_style_disabled.
INSERT gwa_edit INTO TABLE git_edit.
INSERT LINES OF git_edit INTO TABLE gwa_ekko-style.
* 将除第三行之外的所有行都设置成不能输入
DO 10 TIMES.
IF sy-index <> 3.
MODIFY git_ekko INDEX sy-index FROM gwa_ekko TRANSPORTING style.
ENDIF.
ENDDO.
PERFORM frm_set_catalog.
gwa_layout-zebra = 'X'.
gwa_layout-sel_mode = 'A'.
gwa_layout-cwidth_opt = 'X'.
gwa_layout-edit = 'X'. "将整个ALV设置成可编辑
gwa_layout-stylefname = 'STYLE'. "指定保存编辑状态的字段名
gwa_glay-edt_cll_cb = 'X'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
is_layout_lvc = gwa_layout
it_fieldcat_lvc = git_fcat
* i_grid_settings = gwa_glay
TABLES
t_outtab = git_ekko[]
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc = 0.
ENDIF.
*&---------------------------------------------------------------------*
*& Form FRM_SET_CATALOG
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
*
*----------------------------------------------------------------------*
FORM frm_set_catalog .
DATA:l_pos TYPE i VALUE 1.
CLEAR: l_pos.
l_pos = l_pos + 1.
gwa_fcat-coltext = 'PO'.
gwa_fcat-scrtext_l = 'PO'.
gwa_fcat-scrtext_m = 'PO'.
gwa_fcat-scrtext_s = 'PO'.
gwa_fcat-fieldname = 'EBELN'.
gwa_fcat-col_pos = l_pos.
gwa_fcat-outputlen = '10'.
APPEND gwa_fcat TO git_fcat.
l_pos = l_pos + 1.
gwa_fcat-coltext = 'PO item'.
gwa_fcat-scrtext_l = 'PO item'.
gwa_fcat-scrtext_m = 'PO item'.
gwa_fcat-scrtext_s = 'PO item'.
gwa_fcat-fieldname = 'VERKF'.
gwa_fcat-col_pos = l_pos.
gwa_fcat-outputlen = '20'.
APPEND gwa_fcat TO git_fcat.
ENDFORM.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
REPORTztest_alv_lvc_edit.
TYPES:BEGIN OFgty_ekko,
ebelnTYPEekko-ebeln,
verkfTYPEekko-verkf,
styleTYPElvc_t_styl,"设置编辑状态字段
END OFgty_ekko.
DATA:git_ekkoTYPESTANDARD TABLE OFgty_ekko,
gwa_ekkoTYPEgty_ekko.
DATA:git_fcatTYPElvc_t_fcat,
gwa_fcatLIKELINEOFgit_fcat,
gwa_layoutTYPElvc_s_layo,
gwa_glayTYPElvc_s_glay.
DATA:gwa_editTYPElvc_s_styl,
git_editTYPElvc_t_styl.
START-OF-SELECTION.
SELECTebeln
verkf
INTOCORRESPONDING FIELDS OF TABLEgit_ekko
FROMekko
UP TO10ROWS.
gwa_edit-fieldname='EBELN'.
gwa_edit-style=cl_gui_alv_grid=>mc_style_disabled.
INSERTgwa_editINTOTABLEgit_edit.
gwa_edit-fieldname='VERKF'.
gwa_edit-style=cl_gui_alv_grid=>mc_style_disabled.
INSERTgwa_editINTOTABLEgit_edit.
INSERTLINESOFgit_editINTOTABLEgwa_ekko-style.
* 将除第三行之外的所有行都设置成不能输入
DO10TIMES.
IFsy-index<>3.
MODIFYgit_ekkoINDEXsy-indexFROMgwa_ekkoTRANSPORTINGstyle.
ENDIF.
ENDDO.
PERFORMfrm_set_catalog.
gwa_layout-zebra='X'.
gwa_layout-sel_mode='A'.
gwa_layout-cwidth_opt='X'.
gwa_layout-edit='X'."将整个ALV设置成可编辑
gwa_layout-stylefname='STYLE'."指定保存编辑状态的字段名
gwa_glay-edt_cll_cb='X'.
CALL FUNCTION'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program=sy-repid
is_layout_lvc=gwa_layout
it_fieldcat_lvc=git_fcat
*i_grid_settings= gwa_glay
TABLES
t_outtab=git_ekko[]
EXCEPTIONS
program_error=1
OTHERS=2.
IFsy-subrc=0.
ENDIF.
*&---------------------------------------------------------------------*
*&FormFRM_SET_CATALOG
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
*-->p1text
*
*----------------------------------------------------------------------*
FORMfrm_set_catalog.
DATA:l_posTYPEiVALUE1.
CLEAR:l_pos.
l_pos=l_pos+1.
gwa_fcat-coltext='PO'.
gwa_fcat-scrtext_l='PO'.
gwa_fcat-scrtext_m='PO'.
gwa_fcat-scrtext_s='PO'.
gwa_fcat-fieldname='EBELN'.
gwa_fcat-col_pos=l_pos.
gwa_fcat-outputlen='10'.
APPENDgwa_fcatTOgit_fcat.
l_pos=l_pos+1.
gwa_fcat-coltext='PO item'.
gwa_fcat-scrtext_l='PO item'.
gwa_fcat-scrtext_m='PO item'.
gwa_fcat-scrtext_s='PO item'.
gwa_fcat-fieldname='VERKF'.
gwa_fcat-col_pos=l_pos.
gwa_fcat-outputlen='20'.
APPENDgwa_fcatTOgit_fcat.
ENDFORM.
显示:
以上。