2.6 Colors and Input Enable to ALV Column
本实例显示根据上下文配置ALV列是否可输入,显示不同背景颜色。
1.创建项目,声明使用ALV组件
2.设置MAIN视图组件
Properties页签:声明使用ALV组件及接口控制器
Layout页签:创建ViewContainerUIElement作为容器显示ALV
Context页签:创建Node_Sflight节点;
在NODE_SFLIGHT节点下再创建Attribute;
Read_only类型为wdy_boolean;
Cell_design类型为WDUI_TABLE_CELL_DESIGN;
注意想要在NODE_SFLIGHT创建额外的Attribute,需要将Dictionary Structure指定的Sflight结构删掉。
Methods页签
实现WDDOINIT方法;
获取数据库Sflight表数据;
将数据绑定到上下文节点Node_sflight;
将上下文节点数据设置到ALV;
配置ALV列颜色和列是否可编辑;
实例代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
method WDDOINIT . DATA:lo_node TYPE REF TO if_wd_context_node. DATA:lo_element TYPE REF TO if_wd_context_element. DATA:lt_sflight TYPE wd_this->Elements_node_sflight. DATA:ls_sflight TYPE wd_this->Element_node_sflight. "查询数据 SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_sflight FROM sflight. "为read_only,cell_desigh赋值 "cell_design类型:cl_wd_table_column的e_cell_design对应静态变量 LOOP AT lt_sflight INTO ls_sflight. IF ls_sflight-carrid = 'AA'. ls_sflight-read_only = abap_true. ls_sflight-cell_design = cl_wd_table_column=>e_cell_design-COLOR_RED. ELSEIF ls_sflight-carrid = 'AZ'. ls_sflight-read_only = abap_false. ls_sflight-cell_design = cl_wd_table_column=>e_cell_design-COLOR_YELLOW. ELSE. ls_sflight-read_only = abap_true. ls_sflight-cell_design = cl_wd_table_column=>e_cell_design-COLOR_GREEN. ENDIF. MODIFY lt_sflight FROM ls_sflight. ENDLOOP. "获取node_sflight节点 lo_node = wd_context->get_child_node( 'NODE_SFLIGHT' ). "绑定数据给上下文节点 lo_node->bind_table( lt_sflight ). "实例化ALV组件 DATA:lo_cmp_usage type ref to if_wd_component_usage. lo_cmp_usage = wd_this->wd_cpuse_alv( ). if lo_cmp_usage->has_active_component( ) is initial. lo_cmp_usage->create_component( ). endif. "获取ALV接口控制器 DATA:lo_INTERFACECONTROLLER TYPE REF TO IWCI_SALV_WD_TABLE . lo_INTERFACECONTROLLER = wd_this->wd_cpifc_alv( ). "设置alv绑定上下文节点数据 lo_interfacecontroller->set_data( * only_if_new_descr = " wdy_boolean r_node_data = lo_node " ref to if_wd_context_node ). "获取config model DATA:lv_value TYPE ref to cl_salv_wd_config_table. lv_value = lo_interfacecontroller->get_model( ). "根据read_only值设置carrid字段是否可输入 DATA:lo_column TYPE REF TO cl_salv_wd_column. DATA:lo_input_field TYPE REF TO cl_salv_wd_uie_input_field. CREATE OBJECT lo_input_field EXPORTING value_fieldname = 'PRICE'. lo_input_field->SET_READ_ONLY_FIELDNAME( 'READ_ONLY' ). "获取列 lo_column = lv_value->IF_SALV_WD_COLUMN_SETTINGS~GET_COLUMN( 'PRICE' ). "设置price列为输入框 lo_column->SET_CELL_EDITOR( lo_input_field ). "设置carrid列cell design lo_column = lv_value->IF_SALV_WD_COLUMN_SETTINGS~GET_COLUMN( 'CARRID' ). lo_column->SET_CELL_DESIGN_FIELDNAME( 'CELL_DESIGN' ). "将read_only,cell_design列不显示, lv_value->IF_SALV_WD_COLUMN_SETTINGS~DELETE_COLUMN( 'READ_ONLY' ). lv_value->IF_SALV_WD_COLUMN_SETTINGS~DELETE_COLUMN( 'CELL_DESIGN' ). endmethod.
3.设置Windows,嵌入ALV视图到容器中
4.创建Web Dynpro Application,运行测试
CARRID列根据Cell_Design功能列值显示不同颜色;
PRICE列根据READ_ONLY功能列值控制是否可输入;