Creating Context Nodes and Data Binding for DataGrid
DataGrid uses a new approach to processing data. For this reason, a new type of context node is required in which data can be stored. The context node is two-dimenstional. This type is not supported by the ABAP Workbench. For this reason you have to create this context node dynamically and bind it to the relevant property.
You create the context nodes and bind them to the properties in the hook method WDDOMODIFYVIEW.
The following source code is an example of the implementation:
method WDDOMODIFYVIEW .
data l_data_grid type ref to cl_wd_data_grid.
data l_segment type ref to cl_wd_data_grid_segment.
data lt_segments type cl_wd_data_grid_segment=>tt_data_grid_segment.
data l_cell type ref to cl_wd_data_grid_cell.
data l_cells type cl_wd_data_grid_cell=>tt_data_grid_cell.
if first_time = abap_true.
wd_assist->fill_data( ).
l_data_grid ?= view->get_element( 'DATA_GRID' ).
lt_segments = l_data_grid->get_segments( ).
loop at lt_segments into l_segment.
l_segment->bind_data_source( wd_assist->create_node( segment_name = l_segment->get_id( )
root_node = wd_context ) ).
wd_assist->bind_node_data( l_segment ).
" Bind value of cells
l_cells = l_segment->get_templates( ).
loop at l_cells into l_cell.
l_cell->bind_value( l_segment->bound_data_source( ) && '.' && 'VALUE' ).
l_cell->bind_image_source( l_segment->bound_data_source( ) && '.' && 'ICON_SOURCE' ).
endloop.
endloop.
endif.
endmethod.
In the example above, the following steps are performed:
- For each DataGridSegment of DataGrid a new node is created (method CREATE_NODE).
- The node is bound to property dataSource of DataGridSegment (method BIND_NODE_DATA).
- A template for DataGridCell is created as a standard cell editor.
- Two attributes of the context node for the dataSource property are bound to the UI element.
The following steps are performed in the assistance class:
- Define the data structure of the context node.
types: begin of TS_DATA_GRID , wd_tkey_x type i, wd_tkey_y type i, value type string, icon_source type string, state type WDUI_C_TABLE_HIERACHICAL_STATE, level type i, end of ts_data_grid . types: tt_data_grid type standard table of ts_data_grid with non-unique default key with non-unique sorted key wd_tkey_2d components wd_tkey_x wd_tkey_y . - Create the runtime service instance.
data l_data type ts_data_grid. data lo_structure_rtti type ref to cl_abap_structdescr. lo_structure_rtti ?= cl_abap_typedescr=>describe_by_data( p_data = l_data ). - Define the default values of the attributes as placeholders for data points.
data lt_attributes type wdr_context_attr_info_map. data ls_attributes like line of lt_attributes. ls_attributes-name = 'VALUE'. ls_attributes-default_value = lv_segment_id. insert ls_attributes into table lt_attributes. ls_attributes-name = 'ICON_SOURCE'. ls_attributes-default_value = `/sap/public/bc/ur/nw5/themes/sap_corbu/common/libs/Icon/Failure.gif`. insert ls_attributes into table lt_attributes. - Create the two-dimensional context node as a child node of the root node. This is defined by parameter IS_2D_NODE of method ADD_NEW_CHILD_NODE.
data l_node_info type ref to if_wd_context_node_info. data l_node type ref to if_wd_context_node. data l_node_2d type ref to if_wd_context_node_2d. l_node_info = root_node->get_node_info( ). l_node_info = l_node_info->add_new_child_node( name = segment_name is_initialize_lead_selection = abap_false static_element_rtti = lo_structure_rtti attributes = lt_attributes is_2d_node = abap_true ). l_node = root_node->get_child_node( segment_name ). l_node_2d ?= l_node.
The data with which the context node is filled must meet the following prerequisites:
- Contains element WD_TKEY_X of type I
- Contains element WD_TKEY_Y of type I
- Contains the key of components WD_TKEY_X and WD_TKEY_Y with the index name WD_TKEY_2D
Two-dimensional context nodes have the following restrictions:
- Lead selection is not supported.
- Nodes cannot be nested.
- Nodes cannot be mapped.
- Nodes can only be created dynamically at runtime and not at designtime.
- Operations for context elements with integer indexes are not possible (in this case an exception of type cx_wd_context is thrown).
- Node attributes cannot be set to final.