1.ICON图标
2.ALV状态栏增加按钮:创建 / 修改 / 保存
2.1添加按钮
进入控制器COMPONENTCONTROLLER
方法INIT_ALV初始化,新增4个按钮:新增/修改/删除/保存 ,代码红色部分。
method init_alv .
data:
lo_nd_zsmm_mat type ref to if_wd_context_node,
lo_cmp_alv type ref to if_wd_component_usage,
lo_cmpif_alv type ref to iwci_salv_wd_table,
lo_config type ref to cl_salv_wd_config_table.* alv component usage
lo_cmp_alv = wd_this->wd_cpuse_alv_mat( ).
if lo_cmp_alv->has_active_component( ) is initial.
lo_cmp_alv->create_component( ).
endif.* set data node
lo_nd_zsmm_mat = wd_context->get_child_node( name = wd_this->wdctx_zsmm_material ).
lo_cmpif_alv = wd_this->wd_cpifc_alv_mat( ).
lo_cmpif_alv->set_data( lo_nd_zsmm_mat ).* configure alv
lo_config = lo_cmpif_alv->get_model( ).* table settings
lo_config->if_salv_wd_table_settings~set_fixed_table_layout( value = abap_true ).
lo_config->if_salv_wd_table_settings~set_visible_row_count( ).
lo_config->if_salv_wd_table_settings~set_width( '100%' ).
lo_config->if_salv_wd_table_settings~set_footer_visible( if_salv_wd_c_table_settings=>footer_visible_on_demand ).
lo_config->if_salv_wd_table_settings~set_scrollable_col_count( ).
lo_config->if_salv_wd_table_settings~set_read_only( abap_false ).
lo_config->if_salv_wd_table_settings~set_data_check( if_salv_wd_c_table_settings=>data_check_on_cell_event ). lo_config->if_salv_wd_std_functions~set_view_list_allowed( abap_false ).
lo_config->if_salv_wd_std_functions~set_pdf_allowed( abap_false ).
lo_config->if_salv_wd_std_functions~set_edit_check_available( abap_false ).
lo_config->if_salv_wd_std_functions~set_edit_insert_row_allowed( abap_false ).
lo_config->if_salv_wd_std_functions~set_edit_append_row_allowed( abap_false ).
lo_config->if_salv_wd_std_functions~set_edit_delete_row_allowed( abap_false ).* table toolbar
data:
lo_fun_add type ref to cl_salv_wd_function,
lo_btn_add type ref to cl_salv_wd_fe_button,
lo_fun_chg type ref to cl_salv_wd_function,
lo_btn_chg type ref to cl_salv_wd_fe_button,
lo_fun_del type ref to cl_salv_wd_function,
lo_btn_del type ref to cl_salv_wd_fe_button,
lo_fun_save type ref to cl_salv_wd_function,
lo_btn_save type ref to cl_salv_wd_fe_button. lo_fun_add = lo_config->if_salv_wd_function_settings~create_function( 'BT_ADD' ).
create object lo_btn_add.
lo_btn_add->set_text( wd_assist->get_text( key = 'B01' ) ).
lo_btn_add->set_image_source( value = '~Icon/AddRow' ).
lo_fun_add->set_editor( lo_btn_add ). lo_fun_chg = lo_config->if_salv_wd_function_settings~create_function( 'BT_CHG' ).
create object lo_btn_chg.
lo_btn_chg->set_text( wd_assist->get_text( key = 'B02' ) ).
lo_btn_chg->set_image_source( value = '~Icon/EditChangedItem' ).
lo_fun_chg->set_editor( lo_btn_chg ). lo_fun_del = lo_config->if_salv_wd_function_settings~create_function( 'BT_DEL' ).
create object lo_btn_del.
lo_btn_del->set_text( wd_assist->get_text( key = 'B03' ) ).
lo_btn_del->set_image_source( value = '~Icon/DeletedItem' ).
lo_fun_del->set_editor( lo_btn_del ). lo_fun_save = lo_config->if_salv_wd_function_settings~create_function( 'BT_SAVE' ).
create object lo_btn_save.
lo_btn_save->set_text( wd_assist->get_text( key = 'B04' ) ).
lo_btn_save->set_image_source( value = '~Icon/Save' ).
lo_fun_save->set_editor( lo_btn_save ).* table columns and column header
data:
lt_columns type salv_wd_t_column_ref,
ls_column type salv_wd_s_column_ref,
lo_column type ref to cl_salv_wd_column,
lo_header type ref to cl_salv_wd_column_header,
lo_dropdown_by_key type ref to cl_salv_wd_uie_dropdown_by_key,
lo_input_field type ref to cl_salv_wd_uie_input_field,
lo_text_view type ref to cl_salv_wd_uie_text_view,
lv_field_name type string,
lv_length type i. lt_columns = lo_config->if_salv_wd_column_settings~get_columns( ). loop at lt_columns into ls_column.
lo_column = ls_column-r_column.
lo_header = lo_column->get_header( ).
lo_header->set_ddic_binding_field( if_salv_wd_c_column_settings=>ddic_bind_none ). case ls_column-id.
when 'MATNR'.
"lo_column->set_width( value = '18' ).
lo_header->set_text( value = wd_assist->get_text( key = 'A01' ) ).
create object lo_input_field exporting value_fieldname = ls_column-id.
lo_input_field->set_read_only_fieldname( value = 'FG_READ' ).
lo_column->set_cell_editor( lo_input_field ). when 'ERNAM'.
"lo_column->set_width( value = '12' ).
lo_header->set_text( value = wd_assist->get_text( key = 'A02' ) ). when 'ERSDA'.
"lo_column->set_width( value = '8' ).
lo_header->set_text( value = wd_assist->get_text( key = 'A03' ) ). when 'MTART'.
"lo_column->set_width( value = '4' ).
lo_header->set_text( value = wd_assist->get_text( key = 'A04' ) ).
create object lo_input_field
exporting
value_fieldname = ls_column-id.
lo_input_field->set_read_only_fieldname( value = 'FG_READ' ).
lo_column->set_cell_editor( lo_input_field ). when 'MBRSH'.
"lo_column->set_width( value = '1' ).
lo_header->set_text( value = wd_assist->get_text( key = 'A05' ) ).
create object lo_input_field
exporting
value_fieldname = ls_column-id.
lo_input_field->set_read_only_fieldname( value = 'FG_READ' ).
lo_column->set_cell_editor( lo_input_field ). when 'MATKL'.
"lo_column->set_width( value = '9' ).
lo_header->set_text( value = wd_assist->get_text( key = 'A06' ) ).
create object lo_input_field
exporting
value_fieldname = ls_column-id.
lo_input_field->set_read_only_fieldname( value = 'FG_READ' ).
lo_column->set_cell_editor( lo_input_field ). when 'MEINS'.
"lo_column->set_width( value = '3' ).
lo_header->set_text( value = wd_assist->get_text( key = 'A07' ) ).
create object lo_input_field
exporting
value_fieldname = ls_column-id.
lo_input_field->set_read_only_fieldname( value = 'FG_READ' ).
lo_column->set_cell_editor( lo_input_field ). when 'GROES'.
"lo_column->set_width( value = '32' ).
lo_header->set_text( value = wd_assist->get_text( key = 'A08' ) ).
create object lo_input_field
exporting
value_fieldname = ls_column-id.
lo_input_field->set_read_only_fieldname( value = 'FG_READ' ).
lo_column->set_cell_editor( lo_input_field ). when 'SPART'.
"lo_column->set_width( value = '2' ).
lo_header->set_text( value = wd_assist->get_text( key = 'A09' ) ).
create object lo_input_field
exporting
value_fieldname = ls_column-id.
lo_input_field->set_read_only_fieldname( value = 'FG_READ' ).
lo_column->set_cell_editor( lo_input_field ). when 'WERKS'.
"lo_column->set_width( value = '4' ).
lo_header->set_text( value = wd_assist->get_text( key = 'A10' ) ).
create object lo_input_field
exporting
value_fieldname = ls_column-id.
lo_input_field->set_read_only_fieldname( value = 'FG_READ' ).
lo_column->set_cell_editor( lo_input_field ). when 'EKGRP'.
"lo_column->set_width( value = '3' ).
lo_header->set_text( value = wd_assist->get_text( key = 'A11' ) ).
create object lo_input_field
exporting
value_fieldname = ls_column-id.
lo_input_field->set_read_only_fieldname( value = 'FG_READ' ).
lo_column->set_cell_editor( lo_input_field ). when others.
lo_column->set_visible( value = cl_wd_uielement=>e_visible-blank ). endcase. endloop.
endmethod.
ALV按钮增加后需要触发,则需要添加ON_FUNCTION事件,如下图BT_ALV_FUN:
BT_ALV_FUN事件代码:
method bt_alv_fun . case r_param->id.
when 'BT_ADD'. bt_add( ). "新增
when 'BT_CHG'. bt_chg( ). "修改
when 'BT_DEL'. bt_del( ). "删除
when 'BT_SAVE'. bt_save( ). "保存
when others.
endcase.endmethod.
上述代码中不同的按钮调用不同的方法,该方法需要在控制器中进行定义:
BT_ADD代码部分:
method bt_add .
data:
lo_nd_zsmm_mat type ref to if_wd_context_node,
lo_el_zsmm_mat type ref to if_wd_context_element,
ls_zsmm_mat type wd_this->element_zsmm_material. lo_nd_zsmm_mat = wd_context->get_child_node( name = wd_this->wdctx_zsmm_material ). ls_zsmm_mat-fg_read = abap_false. "设置ALV字段值只读
ls_zsmm_mat-ersda = sy-datum. "日期
ls_zsmm_mat-ernam = sy-uname. "用户 lo_el_zsmm_mat = lo_nd_zsmm_mat->bind_structure(
new_item = ls_zsmm_mat "new_item 结构新增 new_items 内表新增
set_initial_elements = abap_false "abap_false保留原始记录并新增行
index = "第1行索引
). lo_nd_zsmm_mat->set_lead_selection( lo_el_zsmm_mat ). data:
lo_config type ref to cl_salv_wd_config_table,
ls_field type salv_wd_s_field_ref,
lt_field type salv_wd_t_field_ref. lo_config = wd_this->wd_cpifc_alv_mat( )->get_model( ). lt_field = cl_salv_wd_model_table_util=>if_salv_wd_table_util_fields~get_filtered_fields( lo_config ). "过滤清空
loop at lt_field into ls_field.
ls_field-r_field->if_salv_wd_filter~delete_filter_rules( ).
endloop. wd_this->wd_cpifc_alv_mat( )->refresh( ). "设置焦点
wd_this->wd_cpifc_alv_mat( )->set_focus( column = 'MATNR' index = ).
endmethod.
BT_CHG代码部分:
method bt_chg . data:
lo_nd_zsmm_mat type ref to if_wd_context_node,
lt_el_zsmm_mat type wdr_context_element_set,
lo_el_zsmm_mat type ref to if_wd_context_element,
ls_zsmm_mat type wd_this->element_zsmm_material,
lt_zsmm_mat type wd_this->elements_zsmm_material,
lo_api_controller type ref to if_wd_controller,
lo_message_manager type ref to if_wd_message_manager.
data:
lv_iserror type abap_bool. clear: wd_this->gv_message. lo_api_controller ?= wd_this->wd_get_api( ). "消息
lo_message_manager = lo_api_controller->get_message_manager( ). lo_nd_zsmm_mat = wd_context->get_child_node( name = wd_this->wdctx_zsmm_material ). "获取选择行数据
lt_el_zsmm_mat = lo_nd_zsmm_mat->get_selected_elements( including_lead_selection = abap_true ). if lt_el_zsmm_mat is initial.
wd_this->gv_message = wd_assist->get_text( key = 'M01' ).
lo_message_manager->report_error_message( message_text = wd_this->gv_message ).
return.
endif. clear: lt_zsmm_mat, lv_iserror. loop at lt_el_zsmm_mat into lo_el_zsmm_mat.
clear: ls_zsmm_mat. lo_el_zsmm_mat->get_static_attributes( importing static_attributes = ls_zsmm_mat ). if ls_zsmm_mat-groes is initial. wd_this->gv_message = wd_assist->get_text( key = 'M02' ).
lo_message_manager->report_error_message( message_text = wd_this->gv_message ). lv_iserror = abap_true.
exit.
endif.
append ls_zsmm_mat to lt_zsmm_mat.
endloop. if lv_iserror eq abap_true.
return.
endif. loop at lt_el_zsmm_mat into lo_el_zsmm_mat.
clear: ls_zsmm_mat.
lo_el_zsmm_mat->get_static_attributes( importing static_attributes = ls_zsmm_mat ).
ls_zsmm_mat-fg_read = abap_false.
lo_el_zsmm_mat->set_static_attributes( static_attributes = ls_zsmm_mat ).
endloop.
endmethod.
BT_DEL代码部分:
method bt_del .
data:
lo_window_manager type ref to if_wd_window_manager,
lo_popup_window type ref to if_wd_window,
lo_cmp_api type ref to if_wd_component,
lt_text type string_table,
lv_titlebar type string. lo_cmp_api = wd_this->wd_get_api( ).
lo_window_manager = lo_cmp_api->get_window_manager( ).
lv_titlebar = wd_assist->get_text( key = 'M03' ).
wd_this->gv_message = wd_assist->get_text( key = 'M04' ).
append wd_this->gv_message to lt_text. call method lo_window_manager->create_popup_to_confirm
exporting
text = lt_text
button_kind = if_wd_window=>co_buttons_yesno
message_type = if_wd_window=>co_msg_type_warning
close_button = abap_false
window_title = lv_titlebar
window_position = if_wd_window=>co_center
default_button = if_wd_window=>co_button_yes
receiving
result = lo_popup_window.
try.
lo_popup_window->popup_to_confirm->subscribe_to_button_event(
controller = lo_cmp_api
handler_name = 'HNDL_POP_YES'
button = if_wd_window=>co_button_yes ).
catch cx_wdr_rt_exception.
endtry. try.
lo_popup_window->popup_to_confirm->subscribe_to_button_event(
controller = lo_cmp_api
handler_name = 'HNDL_POP_NO'
button = if_wd_window=>co_button_no ).
catch cx_wdr_rt_exception.
endtry. lo_popup_window->open( ).
endmethod.
上述代码中增加了弹出窗口POPUP_TO_CONFIRM,则需要针对不同的选择结果添加不同的触发事件。
选择’是’,则调用HNDL_POP_YES,选择’否’,则调用HNDL_POP_NO。
两个事件需要在控制器中添加:
HNDL_POP_YES事件代码:
method hndl_pop_yes . data:
lo_nd_zsmm_mat type ref to if_wd_context_node,
lt_el_zsmm_mat type wdr_context_element_set,
lo_el_zsmm_mat type ref to if_wd_context_element,
ls_zsmm_mat type wd_this->element_zsmm_material,
lt_zsmm_mat type wd_this->elements_zsmm_material,
lo_api_controller type ref to if_wd_controller,
lo_message_manager type ref to if_wd_message_manager.
data:
lv_iserror type abap_bool.
field-symbols:
<fs_zsmm_mat> type wd_this->element_zsmm_material. clear: wd_this->gv_message. lo_api_controller ?= wd_this->wd_get_api( ). "消息
lo_message_manager = lo_api_controller->get_message_manager( ). lo_nd_zsmm_mat = wd_context->get_child_node( name = wd_this->wdctx_zsmm_material ).
lo_el_zsmm_mat = lo_nd_zsmm_mat->get_element( ). "获取选择行数据
* lt_el_zsmm_mat = lo_nd_zsmm_mat->get_selected_elements( including_lead_selection = abap_true ).
* if lt_el_zsmm_mat is initial.
* wd_this->gv_message = wd_assist->get_text( key = 'M01' ).
* lo_message_manager->report_error_message( message_text = wd_this->gv_message ).
* return.
* endif. clear: lt_zsmm_mat, lv_iserror.
lo_el_zsmm_mat->get_static_attributes( importing static_attributes = ls_zsmm_mat ).
lo_nd_zsmm_mat->get_static_attributes_table( importing table = lt_zsmm_mat ). loop at lt_zsmm_mat assigning <fs_zsmm_mat>
where matnr = ls_zsmm_mat-matnr.
delete lt_zsmm_mat.
endloop. lo_nd_zsmm_mat->bind_table( lt_zsmm_mat ).
lo_message_manager->report_success( message_text = wd_assist->get_text( key = 'M05') ).endmethod.
HNDL_POP_NO触发事件暂不做处理。
BT_SAVE保存方法暂不做处理。
2.2测试
选择WebDynpro应用程序,点击执行按钮。
界面新增了4个按钮:新建/修改/删除/保存。
查询记录:
新建:
修改:
删除:
上述步骤基本完成ALV控件的新建/修改/删除操作。