创建一个组件:Y_DYNAMIC_CONTEXT_TABLE
视图:V_MAIN
窗口:V_MAIN
视图中添加控件Group:GRP 设置参数参考图
创建方法
初始化
METHOD wddoinit . create_context_inetab( )."创建动态节点和动态内表 create_table( )."创建动态Table get_data( )."获取数据 ENDMETHOD.
创建动态节点和动态内表
METHOD create_context_inetab . *★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ *创建动态内表 *★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ DATA:lt_fcat TYPE lvc_t_fcat, ls_fcat TYPE lvc_s_fcat. ls_fcat-col_pos = 1. " 顺序 ls_fcat-fieldname = 'CARRID'. " 列名-参考字段 ls_fcat-ref_field = 'CARRID'. " 参考字段 ls_fcat-ref_table = 'SPFLI'. " 参考表 APPEND ls_fcat TO lt_fcat. CLEAR ls_fcat. ls_fcat-col_pos = 1. " 顺序 ls_fcat-fieldname = 'CONNID'. " 列名-参考字段 ls_fcat-ref_field = 'CONNID'. " 参考字段 ls_fcat-ref_table = 'SPFLI'. " 参考表 APPEND ls_fcat TO lt_fcat. CLEAR ls_fcat. create_dynamic_intab( EXPORTING it_fcat = lt_fcat IMPORTING tt_dat = wd_this->lt_tab )."返回內表 *★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ *创建动态节点 *★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ DATA:lt_components TYPE cl_abap_structdescr=>component_table, ls_component TYPE cl_abap_structdescr=>component, lr_type TYPE REF TO cl_abap_datadescr. ls_component-name = 'CARRID'."为结构新增一个成员 lr_type ?= cl_abap_typedescr=>describe_by_name( p_name = 'S_CARR_ID' ). ls_component-type = lr_type."新增成员的类型对象 APPEND ls_component TO lt_components. CLEAR ls_component. ls_component-name = 'CONNID'."为结构新增一个成员 lr_type ?= cl_abap_typedescr=>describe_by_name( p_name = 'S_CONN_ID' ). ls_component-type = lr_type."新增成员的类型对象 APPEND ls_component TO lt_components. CLEAR ls_component. create_dynamic_context( EXPORTING node_name = 'NODE_SPFLI' it_components = lt_components ). ENDMETHOD.
方法:create_dynamic_intab
参数
METHOD create_dynamic_intab . DATA:wa_fcat TYPE lvc_s_fcat. TYPES:BEGIN OF ty_dyndata, fieldname TYPE fieldname, data_type TYPE string, END OF ty_dyndata, ty_t_dyndata TYPE STANDARD TABLE OF ty_dyndata. DATA: lt_dyndata TYPE ty_t_dyndata, lo_structdescr TYPE REF TO cl_abap_structdescr, lo_typedescr TYPE REF TO cl_abap_typedescr, lo_tabledescr TYPE REF TO cl_abap_tabledescr, lr_data TYPE REF TO data, lr_dyntable TYPE REF TO data, lt_component TYPE cl_abap_structdescr=>component_table. FIELD-SYMBOLS: <dyndata> TYPE ty_dyndata, <component> TYPE LINE OF cl_abap_structdescr=>component_table, <dyntable> TYPE table. LOOP AT it_fcat INTO wa_fcat. APPEND INITIAL LINE TO lt_dyndata ASSIGNING <dyndata>. <dyndata>-fieldname = wa_fcat-fieldname. <dyndata>-data_type = wa_fcat-ref_table && '-' && wa_fcat-ref_field. ENDLOOP. LOOP AT lt_dyndata ASSIGNING <dyndata>. TRY. CREATE DATA lr_data TYPE (<dyndata>-data_type). APPEND INITIAL LINE TO lt_component ASSIGNING <component>. <component>-type ?= cl_abap_datadescr=>describe_by_data_ref( lr_data ). <component>-name = <dyndata>-fieldname. CATCH cx_root. ENDTRY. ENDLOOP. lo_structdescr = cl_abap_structdescr=>create( lt_component ). lo_tabledescr = cl_abap_tabledescr=>create( p_line_type = lo_structdescr p_table_kind = cl_abap_tabledescr=>tablekind_std p_unique = abap_false ). * CREATE DATA lr_dyntable TYPE HANDLE lo_tabledescr. CREATE DATA tt_dat TYPE HANDLE lo_tabledescr. ENDMETHOD.
方法create_dynamic_context:
参数
METHOD create_dynamic_context . DATA lr_root_info TYPE REF TO if_wd_context_node_info. DATA lr_structdescr TYPE REF TO cl_abap_structdescr. DATA lv_init_lead_selection TYPE abap_bool. DATA lo_node_info TYPE REF TO if_wd_context_node_info. lr_root_info = wd_context->get_node_info( ). CALL METHOD cl_abap_structdescr=>create EXPORTING p_components = it_components RECEIVING p_result = lr_structdescr. IF iv_init_lead_selection IS NOT INITIAL."是否默认选中第一行 lv_init_lead_selection = abap_true. ELSE. lv_init_lead_selection = abap_false. ENDIF. CALL METHOD lr_root_info->add_new_child_node EXPORTING * static_element_type = lv_table static_element_rtti = lr_structdescr name = node_name "context id is_multiple = abap_true is_singleton = abap_false is_initialize_lead_selection = lv_init_lead_selection is_static = abap_false RECEIVING child_node_info = lo_node_info. ENDMETHOD.
创建动态Table
METHOD create_table . DATA:lo_api_main TYPE REF TO if_wd_view_controller, lo_view TYPE REF TO if_wd_view. lo_api_main = wd_this->wd_get_api( ). lo_view ?= lo_api_main. *★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ *创建Table *★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ DATA:lr_table TYPE REF TO cl_wd_table, lr_column TYPE REF TO cl_wd_table_column, lr_input_field TYPE REF TO cl_wd_input_field, lr_column_header TYPE REF TO cl_wd_caption, lr_group TYPE REF TO cl_wd_group. DATA:lv_table_id TYPE string, lv_node_name TYPE string, lv_data_source TYPE string, lv_text_view_id TYPE string, lv_column_id TYPE string, lv_field_id TYPE string, lv_bind_value TYPE string, lv_caption_text TYPE wdy_md_translatable_text. lv_table_id = 'TBL_SPFLI'. lv_node_name = 'NODE_SPFLI'. lv_data_source = 'NODE_SPFLI'. cl_wd_table=>new_table( EXPORTING id = lv_table_id design = cl_wd_table=>e_design-standard first_visible_row = 0 footer_visible = 'X' read_only = abap_false selection_mode = cl_wd_table=>e_selection_mode-single visible_row_count = 18 enabled = 'X' * column_resize_mode = '01' width = '100%' grid_mode = '00' "both scrollable_col_count = 7 visible = cl_wd_uielement=>e_visible-visible bind_data_source = lv_data_source RECEIVING control = lr_table ). *第一列: "-----添加列(航线代码) lv_column_id = |{ lv_table_id }| && |_CARRID|. lv_field_id = |{ lv_table_id }| && |_CARRID_EDITOR|. lv_bind_value = |{ lv_node_name }| && |.CARRID|. lv_caption_text = |航线代码|. cl_wd_table_column=>new_table_column( EXPORTING id = lv_column_id width = '120px' fixed_position = '02' RECEIVING control = lr_column )."left cl_wd_input_field=>new_input_field( EXPORTING id = lv_field_id bind_value = lv_bind_value no_history = 'X' read_only = 'X' RECEIVING control = lr_input_field ). lr_table->add_column( the_column = lr_column ). lr_column_header ?= cl_wd_caption=>new_caption( text = lv_caption_text ). lr_column->set_table_cell_editor( the_table_cell_editor = lr_input_field ). lr_column->set_header( lr_column_header ). *第二列: "-----添加列(航班连接 Id) lv_column_id = |{ lv_table_id }| && |_CONNID|. lv_field_id = |{ lv_table_id }| && |_CONNID_EDITOR|. lv_bind_value = |{ lv_node_name }| && |.CONNID|. lv_caption_text = |航班连接 Id)|. cl_wd_table_column=>new_table_column( EXPORTING id = lv_column_id width = '120px' fixed_position = '02' RECEIVING control = lr_column )."left cl_wd_input_field=>new_input_field( EXPORTING id = lv_field_id bind_value = lv_bind_value no_history = 'X' read_only = 'X' RECEIVING control = lr_input_field ). lr_table->add_column( the_column = lr_column ). lr_column_header ?= cl_wd_caption=>new_caption( text = lv_caption_text ). lr_column->set_table_cell_editor( the_table_cell_editor = lr_input_field ). lr_column->set_header( lr_column_header ). *★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ *将Table 放入Group中 *★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ cl_wd_grid_data=>new_grid_data( element = lr_table ). lr_group ?= lo_view->get_element( id = 'GRP' ). lr_group->add_child( lr_table ). ENDMETHOD.
获取数据
METHOD get_data . DATA:dy_wa TYPE REF TO data. FIELD-SYMBOLS:<lt_tab> TYPE STANDARD TABLE, <ls_tab> TYPE any. *★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ *用表类型指针<lt_tab>指向数据对象的内容. *★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ASSIGN wd_this->lt_tab->* TO <lt_tab>. CREATE DATA dy_wa LIKE LINE OF <lt_tab>. ASSIGN dy_wa->* TO <ls_tab>. *★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ *获取数据 *★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ SELECT * INTO CORRESPONDING FIELDS OF TABLE <lt_tab> FROM spfli UP TO 10 ROWS. *★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ *绑定节点数据 *★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ DATA:lo_nd_table TYPE REF TO if_wd_context_node. lo_nd_table = wd_context->get_child_node( name = 'NODE_SPFLI' ). lo_nd_table->bind_table( <lt_tab> ). ENDMETHOD.
结果展现