OO ALV 实现下拉框
ooALV相关的:
it_fieldcatalog
TYPE lvc_t_fcat.
"显示数据列内表
wa_fieldcatalog-fieldname
=
&1.
"字段名称
wa_fieldcatalog-reptext
=
&2.
wa_fieldcatalog-outputlen
=
&3.
"显示长度
if &1 eq
'AAA'.
wa_fieldcatalog-edit
= 'X'.
"可编辑
wa_fieldcatalog-drdn_hndl = '1'.
"下拉框句柄1
wa_fieldcatalog-outputlen = 7.
endif.
if
&1 eq 'BBBB'.
wa_fieldcatalog-drdn_hndl = '2'.
"下拉框句柄2
endif.
append wa_fieldcatalog to
it_fieldcatalog.
clear wa_fieldcatalog.
alv_ctner TYPE REF TO
cl_gui_docking_container.
DATA: l_style
TYPE
i,
ls_layout
TYPE lvc_s_layo,
is_stable
TYPE lvc_s_stbl,
l_disvariant TYPE
disvariant.
IF alv_ctner IS INITIAL.
CREATE OBJECT
alv_ctner
EXPORTING
repid
=
sy-repid
dynnr
=
'9001'
side
=
cl_gui_docking_container=>dock_at_left
"ALV贴屏幕左边,从左边算屏幕宽度,
extension
= 900
"屏幕宽度
EXCEPTIONS
cntl_error
= 1
cntl_system_error
= 2
create_error
= 3
lifetime_error
= 4
lifetime_dynpro_dynpro_link = 5
OTHERS
= 6.
IF sy-subrc
<> 0.
MESSAGE s001(00) WITH '屏幕初始化失败'.
LEAVE
LIST-PROCESSING.
ENDIF.
PERFORM
frm_set_alv_toolbar.
CREATE OBJECT
g_alv_grid
EXPORTING
i_parent = alv_ctner.
PERFORM
frm_build_fieldcat.
"Set/Reset Ready for
Input Status
CALL METHOD
g_alv_grid->set_ready_for_input
EXPORTING
i_ready_for_input = 1.
CLEAR ls_layout.
ls_layout-cwidth_opt =
'X'. "优化列宽
CLEAR
l_disvariant.
l_disvariant-report =
sy-repid. "当前程序
CREATE OBJECT
g_event.
SET HANDLER
g_event->toolbar
FOR g_alv_grid.
SET HANDLER
g_event->handle_data_changed FOR g_alv_grid.
DATA: lt_dropdown TYPE
lvc_t_drop,
ls_dropdown TYPE lvc_s_drop.
"第一个下拉框:AAA字段
ls_dropdown-handle = '1'.
"指定下拉框句柄
ls_dropdown-value = '1 春天'.
APPEND ls_dropdown TO
lt_dropdown.
ls_dropdown-handle = '1'.
ls_dropdown-value = '2 夏天'.
APPEND ls_dropdown TO
lt_dropdown.
ls_dropdown-handle = '1'.
ls_dropdown-value = '3 冬天'.
APPEND ls_dropdown TO
lt_dropdown.
"第二个下拉框:BBB字段
ls_dropdown-handle = '2'.
"指定下拉框句柄
ls_dropdown-value = '0 启用'.
APPEND
ls_dropdown TO lt_dropdown.
ls_dropdown-handle = '2'.
ls_dropdown-value = '1 禁止'.
APPEND
ls_dropdown TO lt_dropdown.
CALL METHOD
g_alv_grid->set_drop_down_table
"调用alv类的下拉框方法
EXPORTING
it_drop_down = lt_dropdown.
CALL METHOD
g_alv_grid->set_table_for_first_display
EXPORTING
is_layout
= ls_layout
is_variant
=
l_disvariant
it_toolbar_excluding = gt_tools
CHANGING
it_fieldcatalog
= it_fieldcatalog[]
it_outtab
= g_it_show[].
至于下拉框,主要的地方如下:
*…………………………………………………………………………………………
* lvc_s_fcat
指定某字段为下拉框
*…………………………………………………………………………………………
DATA: wa_fieldcatalog
TYPE lvc_s_fcat,
"显示数据列内表工作区域
DEFINE mac_fieldlog.
* g_pos = g_pos + 1.
"第几列
* wa_fieldcatalog-col_pos
= g_pos.
"列
* wa_fieldcatalog-coltext
=
&2.
"显示名称,跟reptext效果基本相同
…… 其他相关逻辑 ……
"对AAA字段实现下拉框显示
"对BBB字段实现下拉框显示
END-OF-DEFINITION.
*…………………………………………………………………………………………
* 调用类,实现下拉框功能
*…………………………………………………………………………………………
DATA: g_alv_grid TYPE REF
TO cl_gui_alv_grid,
"ALV类
FORM zform_alv_show .
* 屏幕初始化
*
side
=
cl_gui_docking_container=>dock_at_right
”ALV贴屏幕右边,从屏幕右边开始算宽度
*
style
=
cl_gui_control=>ws_child
"可选参数,设置ALV是否可用手动拖动大小
*添加自定义工具条
*
*构建显示字段样式
* layout 设定样式
* event 绑定事件
*这里是下拉框相关逻辑…………………………………………
*end………………………………………………………………
* "显示内表数据
如上即可
有时候有你会发现点保存的时候报错:
比如你有个状态字段,在库表里是char1类型的,
你下拉框里显示:0:启用 1:禁用
当你定义alv字段的时候,直接参考库表的char1类型
点保存的时候会提示:
“条目0 启用 过长 大于1”
这个时候,在定义alv子段的时候,不参考char1类型
而是定义成string
就可以了
http://blog.sina.com.cn/sapliumeng