主程序界面添加alv组件SALV_WD_TABLE,在componentcontrol里导入alv组件,在组件使用->alv->interface里将context中的DATA与对应节点数据绑定,在视图里导入alv组件SALV_WD_TABLE(两个),这样就完成了最基本的ALV框架实现.如果想让ALV按自己的意愿显示,可以在视图初始化
的时候调用ALV初始化方法.INIT_ALV().在该方法里可以自定义实现某些工能
method init_alv .
data: lr_comp_alv type ref to if_wd_component_usage ,
lr_comp_if_alv type ref to iwci_salv_wd_table ,
lr_config type ref to cl_salv_wd_config_table .
data: lr_column_settings type ref to if_salv_wd_column_settings,
lr_column type ref to cl_salv_wd_column ,
lr_column_header type ref to cl_salv_wd_column_header .
data: lt_column type salv_wd_t_column_ref ,
ls_column type salv_wd_s_column_ref .
****
data: lr_input_field type ref to cl_salv_wd_uie_input_field,
lr_checkbox type ref to cl_salv_wd_uie_checkbox,
lr_image type ref to cl_salv_wd_uie_image,
lr_dropdown_by_key type ref to cl_salv_wd_uie_dropdown_by_key.
data lr_column_id type string.
****
data: lr_field_amnt type ref to cl_salv_wd_field .
data: lv_aggr_rule type ref to cl_salv_wd_aggr_rule ,
lr_sort_rule type ref to cl_salv_wd_sort_rule .
* call method lr_config->if_salv_wd_table_settings~set_selection_mode
* exporting
* value = cl_wd_table=>e_selection_mode-multi_no_lead.
lr_comp_alv = wd_this->wd_cpuse_alv( ) .”根据ALV名字来定
if lr_comp_alv->has_active_component( ) is initial.
lr_comp_alv->create_component( ).
endif.
data alv2_cnfg type ref to cl_salv_wd_config_table.
lr_comp_if_alv = wd_this->wd_cpifc_alv( ).”根据ALV名字来定
lr_config = lr_comp_if_alv->get_model( ).
alv2_cnfg = lr_comp_if_alv->get_model( ).
lr_column_settings ?= alv2_cnfg .
lt_column = lr_column_settings->get_columns( ).
* Set header
data: lr_header type ref to cl_salv_wd_header ,
l_header type string .
lr_header = alv2_cnfg->if_salv_wd_table_settings~get_header( ).
l_header = '测试ALV报表' .
lr_header->set_text( l_header ) .
*=========================================================
data: l_value type ref to cl_salv_wd_config_table.
l_value = lr_comp_if_alv->get_model( ).
* l_value->if_salv_wd_table_settings~set_fixed_table_layout( abap_true ). "使列宽不可自动调节
* l_value->if_salv_wd_table_settings~set_width( '1000PX' ) . "设置宽度
l_value->if_salv_wd_table_settings~set_visible_row_count( '5' ). "显示的行数
l_value->if_salv_wd_table_settings~set_read_only( abap_true ).
l_value->if_salv_wd_table_settings~set_cell_action_event_enabled( abap_true ).
l_value->if_salv_wd_table_settings~set_design( cl_wd_table=>e_design-alternating ).
* DATA: lr_button TYPE REF TO cl_salv_wd_uie_button.
* lr_column = l_value->if_salv_wd_column_settings~get_column( 'CARRID' ).
* CREATE OBJECT lr_button.
* lr_button->set_text_fieldname( 'TITLE' ).
* lr_column->set_cell_editor( lr_button ).
* DATA: lr_link TYPE REF TO cl_salv_wd_uie_link_to_action.
* lr_column = l_value->if_salv_wd_column_settings~get_column( 'CONNID'
*).
* CREATE OBJECT lr_link.
* lr_link->set_text_fieldname( 'DOCNO' ).
* lr_column->set_cell_editor( lr_link ).
l_value->if_salv_wd_table_settings~set_selection_mode( cl_wd_table=>e_selection_mode-none ).
* l_value->if_salv_wd_table_settings~set_selection_mode( cl_wd_table=>e_selection_mode-single_no_lead ).
* l_value->if_salv_wd_table_settings~set_selection_mode( cl_wd_table=>e_selection_mode-single ).
l_value->if_salv_wd_function_settings~set_enabled( abap_true ) .
l_value->if_salv_wd_table_settings~delete_header( ) .
l_value->if_salv_wd_std_functions~set_display_as_allowed( abap_false ) .
l_value->if_salv_wd_std_functions~set_sort_headerclick_allowed( abap_false ) .
l_value->if_salv_wd_std_functions~set_edit_check_available( abap_false ) .
l_value->if_salv_wd_std_functions~set_edit_insert_row_allowed( abap_false ) .
l_value->if_salv_wd_std_functions~set_export_allowed( abap_false ) ."设置是否可以导出
l_value->if_salv_wd_std_functions~set_view_list_allowed( abap_false )."设置列选择输出是否可更改
l_value->if_salv_wd_std_functions~set_pdf_allowed( abap_false ) .
l_value->if_salv_wd_std_functions~set_edit_append_row_allowed( abap_false ) .
l_value->if_salv_wd_std_functions~set_edit_delete_row_allowed( abap_false ) .
l_value->if_salv_wd_std_functions~set_filter_filterline_allowed( abap_false )."设置筛选是否可见
l_value->if_salv_wd_std_functions~set_filter_complex_allowed( abap_false ).
l_value->if_salv_wd_std_functions~set_dialog_settings_allowed( abap_false )."设置设置按钮条
data: lr_table_settings type ref to if_salv_wd_table_settings.
lr_table_settings ?= l_value.
lr_table_settings->set_data_check( '01' ).
lr_table_settings->set_read_only( abap_false ).
loop at lt_column into ls_column.
lr_column_id = ls_column-id.
lr_column = ls_column-r_column.
case ls_column-id.
when 'DOCNO'.
lr_column_header = ls_column-r_column->create_header( ).
lr_column_header->set_text( '公文号' ).
* DATA: lr_link_to_action TYPE REF TO cl_salv_wd_uie_link_to_action.
* CREATE OBJECT lr_link_to_action.
* lr_link_to_action->set_text_fieldname( ls_column-id ).
* ls_column-r_column->set_cell_editor( lr_link_to_action ).
when 'TITLE'.
lr_column_header = ls_column-r_column->create_header( ).
lr_column_header->set_text( '模板名称' ).
data: lr_link_to_action type ref to cl_salv_wd_uie_link_to_action.
create object lr_link_to_action.
lr_link_to_action->set_text_fieldname( ls_column-id ).
* lr_link_to_action->set_read_only( abap_true ).
ls_column-r_column->set_cell_editor( lr_link_to_action ).
* DATA lr_dropdown TYPE REF TO cl_salv_wd_uie_dropdown_by_idx.
* CREATE OBJECT lr_dropdown_by_key
* EXPORTING
* selected_key_fieldname = ls_column-id.
** lr_dropdown_by_key->set_read_only( abap_true ).
* lr_dropdown_by_key->set_key_visible( abap_false ).
* lr_column->set_cell_editor( lr_dropdown_by_key ).
DATA lr_dropdown TYPE REF TO cl_salv_wd_uie_dropdown_by_idx.
when 'PLANTP'.
lr_column_header = ls_column-r_column->create_header( ).
lr_column_header->set_text( '计划类型' ).
CREATE OBJECT lr_dropdown_by_key
EXPORTING
selected_key_fieldname = ls_column-id.
lr_dropdown_by_key->set_read_only( abap_true ).
lr_dropdown_by_key->set_key_visible( abap_false ).
lr_column->set_cell_editor( lr_dropdown_by_key ).
when 'WTYPE'.
lr_column_header = ls_column-r_column->create_header( ).
lr_column_header->set_text( '物业类型' ).
CREATE OBJECT lr_dropdown_by_key
EXPORTING
selected_key_fieldname = ls_column-id.
lr_dropdown_by_key->set_read_only( abap_true ).
lr_dropdown_by_key->set_key_visible( abap_false ).
lr_column->set_cell_editor( lr_dropdown_by_key ).
when 'BTYPE'.
lr_column_header = ls_column-r_column->create_header( ).
lr_column_header->set_text( '建筑类型' ).
CREATE OBJECT lr_dropdown_by_key
EXPORTING
selected_key_fieldname = ls_column-id.
lr_dropdown_by_key->set_read_only( abap_true ).
lr_dropdown_by_key->set_key_visible( abap_false ).
lr_column->set_cell_editor( lr_dropdown_by_key ).
* DATA: lr_button3 TYPE REF TO cl_salv_wd_uie_button.
* CREATE OBJECT lr_button3.
* lr_button3->set_text_fieldname( 'VALUE3' ).
* ls_column-r_column->set_cell_editor( lr_button3 ).
when 'MTYPE'.
lr_column_header = ls_column-r_column->create_header( ).
lr_column_header->set_text( '专业类型' ).
CREATE OBJECT lr_dropdown_by_key
EXPORTING
selected_key_fieldname = ls_column-id.
lr_dropdown_by_key->set_read_only( abap_true ).
lr_dropdown_by_key->set_key_visible( abap_false ).
lr_column->set_cell_editor( lr_dropdown_by_key ).
when 'UNAME'.
lr_column_header = ls_column-r_column->create_header( ).
lr_column_header->set_text( '创建人' ).
* DATA: lr_input_field TYPE REF TO cl_salv_wd_uie_input_field.
* CREATE OBJECT lr_input_field
* EXPORTING
* value_fieldname = ls_column-id.
* ls_column-r_column->set_cell_editor( lr_input_field ).
when 'UTIME'.
lr_column_header = ls_column-r_column->create_header( ).
lr_column_header->set_text( '创建时间' ).
when 'DOWNLOAD'.
lr_column_header = ls_column-r_column->create_header( ).
lr_column_header->set_text( '下载' ).
data: lr_button3 type ref to cl_salv_wd_uie_button.
create object lr_button3.
lr_button3->set_text_fieldname( 'DOWNLOAD' ).
ls_column-r_column->set_cell_editor( lr_button3 ).
when others.
ls_column-r_column->set_visible( cl_wd_uielement=>e_visible-none ).
Endcase.
修改按钮可增如下:
data LR_BUTTONUI TYPE REF TO CL_SALV_WD_FE_BUTTON.
DATA LR_SEPARATOR TYPE REF TO CL_SALV_WD_FE_SEPARATOR.
DATA LV_FUNCTION TYPE REF TO CL_SALV_WD_FUNCTION.
IF WD_THIS->READONLY = 'X'.
CREATE OBJECT LR_BUTTONUI.
LR_BUTTONUI->SET_TEXT( '创建行' ).
LV_FUNCTION = LR_CONFIG->IF_SALV_WD_FUNCTION_SETTINGS~CREATE_FUNCTION( ID = 'CREATE' ).
LV_FUNCTION->SET_EDITOR( LR_BUTTONUI ).
lv_function->set_visible( '01' ).
CREATE OBJECT LR_BUTTONUI.
LR_BUTTONUI->SET_TEXT( '删除行' ).
LV_FUNCTION = LR_CONFIG->IF_SALV_WD_FUNCTION_SETTINGS~CREATE_FUNCTION( ID = 'DELETE' ).
LV_FUNCTION->SET_EDITOR( LR_BUTTONUI ).
lv_function->set_visible( '01' ).
CREATE OBJECT LR_BUTTONUI.
LR_BUTTONUI->SET_TEXT( '保存' ).
LV_FUNCTION = LR_CONFIG->IF_SALV_WD_FUNCTION_SETTINGS~CREATE_FUNCTION( ID = 'SAVE' ).
LV_FUNCTION->SET_EDITOR( LR_BUTTONUI ).
lv_function->set_visible( '01' ).
ELSE.
CREATE OBJECT LR_BUTTONUI.
LR_BUTTONUI->SET_TEXT( '创建行' ).
LV_FUNCTION = LR_CONFIG->IF_SALV_WD_FUNCTION_SETTINGS~CREATE_FUNCTION( ID = 'CREATE' ).
LV_FUNCTION->SET_EDITOR( LR_BUTTONUI ).
CREATE OBJECT LR_BUTTONUI.
LR_BUTTONUI->SET_TEXT( '删除行' ).
LV_FUNCTION = LR_CONFIG->IF_SALV_WD_FUNCTION_SETTINGS~CREATE_FUNCTION( ID = 'DELETE' ).
LV_FUNCTION->SET_EDITOR( LR_BUTTONUI ).
CREATE OBJECT LR_BUTTONUI.
LR_BUTTONUI->SET_TEXT( '保存' ).
LV_FUNCTION = LR_CONFIG->IF_SALV_WD_FUNCTION_SETTINGS~CREATE_FUNCTION( ID = 'SAVE' ).
LV_FUNCTION->SET_EDITOR( LR_BUTTONUI ).
ENDIF.
endloop.
endmethod.
如果想在对数据修改之后触发事件,可以自定义function对应ALV的DATA_CHECK方法,
ON_CELL_CLICK事件设置成cell_action
method on_click .
data lo_api_controller type ref to if_wd_controller.
data lo_message_manager type ref to if_wd_message_manager.
lo_api_controller ?= wd_this->wd_get_api( ).
data:
lo_node type ref to if_wd_context_node,
lo_elem type ref to if_wd_context_element,
ls_context type wd_this->element_zt206_l,
lt_context type wd_this->elements_zt206_l,
ls_context2 type wd_this->element_zt207_l,
lt_context2 type wd_this->elements_zt207_l,
lw_string type string,
lw_xstring type xstring.
data filename type string."定义下载文件名
"获取选择行
data lo_nd_zt205_r type ref to if_wd_context_node.
data lt_zt205_r type wd_this->elements_zt205_r.
data ls_zt205_r type wd_this->element_zt205_r.
lo_nd_zt205_r = wd_context->get_child_node( name = wd_this->wdctx_zt205_r ).
lo_nd_zt205_r->get_static_attributes_table( importing table = lt_zt205_r ).
read table lt_zt205_r into ls_zt205_r index r_param->index.
"定义显示用的表名
data lo_nd_zt206_l type ref to if_wd_context_node.
data lt_zt206_l type wd_this->elements_zt206_l.
data ls_zt206_l type wd_this->element_zt206_l.
data lo_nd_zt207_l type ref to if_wd_context_node.
data lt_zt207_l type wd_this->elements_zt207_l.
data ls_zt207_l type wd_this->element_zt207_l.
"设置可见属性
data lo_el_context type ref to if_wd_context_element.
data lv_visible_t type wd_this->element_context-visible_t.
data lv_visible_t2 type wd_this->element_context-visible_t2.
data lv_visible_t3 type wd_this->element_context-visible_t3.
"根据选择列内容执行显示或下载
case r_param->column.
when 'TITLE'.
lo_el_context = wd_context->get_element( ).
lv_visible_t = '02'.
lo_el_context->set_attribute(
name = `VISIBLE_T`
value = lv_visible_t ).
case ls_zt205_r-plantp.
when 1 or 3.
lo_el_context = wd_context->get_element( ).
lv_visible_t2 = '02'.
lo_el_context->set_attribute(
name = `VISIBLE_T2`
value = lv_visible_t2 ).
lo_el_context = wd_context->get_element( ).
lv_visible_t2 = '01'.
lo_el_context->set_attribute(
name = `VISIBLE_T3`
value = lv_visible_t3 ).
lo_nd_zt206_l = wd_context->get_child_node( name = wd_this->wdctx_zt206_l ).
select * from ztpm206 into corresponding fields of table lt_zt206_l where title = ls_zt205_r-title and docno = ls_zt205_r-docno.
loop at lt_zt206_l into ls_zt206_l.
if sy-tabix = 1."单行显示主要信心
else.
clear ls_zt206_l-docno.
clear ls_zt206_l-btype.
clear ls_zt206_l-wtype.
clear ls_zt206_l-title.
modify lt_zt206_l from ls_zt206_l.
endif.
endloop.
lo_nd_zt206_l->bind_table( new_items = lt_zt206_l set_initial_elements = abap_true ).
when 2 or 4.
lo_el_context = wd_context->get_element( ).
lv_visible_t2 = '01'.
lo_el_context->set_attribute(
name = `VISIBLE_T2`
value = lv_visible_t2 ).
lo_el_context = wd_context->get_element( ).
lv_visible_t3 = '02'.
lo_el_context->set_attribute(
name = `VISIBLE_T3`
value = lv_visible_t3 ).
lo_nd_zt207_l = wd_context->get_child_node( name = wd_this->wdctx_zt207_l ).
select * from ztpm207 into corresponding fields of table lt_zt207_l where title = ls_zt205_r-title and docno = ls_zt205_r-docno.
loop at lt_zt207_l into ls_zt207_l.
if sy-tabix = 1.
else.
clear ls_zt207_l-docno.
clear ls_zt207_l-title.
modify lt_zt207_l from ls_zt207_l.
endif.
endloop.
lo_nd_zt207_l->bind_table( new_items = lt_zt207_l set_initial_elements = abap_true ).
endcase.
when 'DOWNLOAD'.
case ls_zt205_r-plantp.
when 1 or 3.
select * from ztpm206 into corresponding
fields of table lt_context
where docno = ls_zt205_r-docno and title = ls_zt205_r-title.
loop at lt_context into ls_context.
concatenate lw_string
ls_context-docno
ls_context-wtype
ls_context-btype
ls_context-wid
ls_context-wdesc
ls_context-node
ls_context-pvalue
ls_context-refer
ls_context-remark
cl_abap_char_utilities=>newline into lw_string
separated by cl_abap_char_utilities=>horizontal_tab.
endloop.
when 2 or 4.
select * from ztpm207 into corresponding
fields of table lt_context2
where docno = ls_zt205_r-docno and title = ls_zt205_r-title.
loop at lt_context2 into ls_context2.
concatenate lw_string
ls_context2-docno
ls_context2-wid
ls_context2-wtask
ls_context2-refer
ls_context2-pvalue
ls_context2-remark
cl_abap_char_utilities=>newline into lw_string
separated by cl_abap_char_utilities=>horizontal_tab.
endloop.
when others.
endcase.
* convert the string => Xstring
call function 'SCMS_STRING_TO_XSTRING'
exporting
text = lw_string
* MIMETYPE = ' '
* ENCODING =
importing
buffer = lw_xstring
exceptions
failed = 1
others = 2 .
concatenate ls_zt205_r-title '.TXT' into filename.
* export the XString to Excel
wdr_task=>client_window->client->attach_file_to_response(
**path to the word file
i_filename = filename
** String Variable
i_content = lw_xstring
** File Type
i_mime_type = 'NOTEPAD' ).
endcase.
endmethod.