是否可以在不复制 GUI 状态的情况下将用户定义的函数添加到 CL_SALV_TREE?

Is it possible to add a user defined function to CL_SALV_TREE without copying GUI status?

是否可以将用户定义的函数添加到 CL_SALV_TREE 的实例而不将 GUI 状态复制到使用该实例的报告中?

我想做的是使用 add_function 方法添加自定义函数。

REPORT zzy.

CLASS lcl_main DEFINITION FINAL CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS:
      main.
ENDCLASS.

CLASS lcl_main IMPLEMENTATION.
  METHOD main.
    DATA: lt_table TYPE STANDARD TABLE OF t000.
    TRY .
      cl_salv_tree=>factory(
        IMPORTING
          r_salv_tree = DATA(lo_salv_tree)
        CHANGING
          t_table = lt_table
      ).
      DATA(lo_salv_functions) = lo_salv_tree->get_functions( ).
      lo_salv_functions->add_function(
        name = 'EXPORT_TO_EXCEL'
        icon = '@J2@'
        tooltip = 'Export as Excel'
        position = if_salv_c_function_position=>right_of_salv_functions
      ).
      lo_salv_functions->set_all( abap_true ).

      lo_salv_tree->display( ).
    CATCH cx_salv_error.
      ASSERT 0 = 1.
    ENDTRY.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  lcl_main=>main( ).

但是按钮没有出现。

我记得前段时间我曾尝试使用 CL_SALV_TABLE 进行此操作,但在使用 add_function.

时出现了明确的异常

我当时尝试的是

REPORT zzy.

CLASS lcl_main DEFINITION FINAL CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS:
      main.
ENDCLASS.

CLASS lcl_main IMPLEMENTATION.
  METHOD main.
    DATA: lt_table TYPE STANDARD TABLE OF t000.
    TRY .
      cl_salv_table=>factory(
        IMPORTING
          r_salv_table = DATA(lo_salv_table)
        CHANGING
          t_table = lt_table
      ).
      DATA(lo_salv_functions) = lo_salv_table->get_functions( ).
      lo_salv_functions->add_function(
        name = 'EXPORT_TO_EXCEL'
        icon = '@J2@'
        tooltip = 'Export as Excel file'
        position = if_salv_c_function_position=>right_of_salv_functions
      ).
      lo_salv_functions->set_all( abap_true ).

      lo_salv_table->display( ).
    CATCH cx_salv_error.
      ASSERT 0 = 1.
    ENDTRY.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  lcl_main=>main( ).

它以由未处理的异常引起的短转储结束 CX_SALV_METHOD_NOT_SUPPORTED。我在SAP标准代码中看到的是

if lr_controller->r_model->get_display_object( )
                        ne IF_SALV_C_TABLE_OBJECTS=>GRID
  and lr_controller->r_model->get_display_object( )
                        ne IF_SALV_C_TABLE_OBJECTS=>TREE.
  text   = text-001.
  l_name = name.
  raise exception type CX_SALV_METHOD_NOT_SUPPORTED
      exporting class  = 'CL_SALV_FUNCTIONS'
                method = 'ENABLE_FUNCTION'
                object = l_name
                key    = text.
endif.

从这段代码可以假设可以使用 add_function 方法为对象 IF_SALV_C_TABLE_OBJECTS=>TREE.

添加一个新函数

到目前为止,我使用了以下解决方法。

REPORT zzy.

CLASS lcl_controller DEFINITION FINAL.
  PUBLIC SECTION.
    INTERFACES:
      if_salv_csqt_content_manager.
    METHODS:
      constructor.
    DATA:
      mo_salv_tree TYPE REF TO cl_salv_tree,
      mt_table TYPE STANDARD TABLE OF t000.
ENDCLASS.

CLASS lcl_controller IMPLEMENTATION.
  METHOD constructor.
    CALL FUNCTION 'SALV_CSQT_CREATE_CONTAINER'
      EXPORTING
        r_content_manager       = me
        title                   = 'Workaround'.
  ENDMETHOD.

  METHOD if_salv_csqt_content_manager~fill_container_content.
    TRY .
      cl_salv_tree=>factory(
        EXPORTING
          r_container = r_container
        IMPORTING
          r_salv_tree = DATA(mo_salv_tree)
        CHANGING
          t_table = mt_table
      ).
      DATA(lo_tree_settings) = mo_salv_tree->get_tree_settings( ).
      lo_tree_settings->set_hierarchy_header( `Hierarchy` ).
      lo_tree_settings->set_hierarchy_size( 30 ).
      lo_tree_settings->set_header( |{ sy-title }| ).

      DATA(lo_salv_functions) = mo_salv_tree->get_functions( ).
      lo_salv_functions->add_function(
        name = 'EXPORT_TO_EXCEL'
        icon = '@J2@'
        tooltip = 'Export as Excel file'
        position = if_salv_c_function_position=>right_of_salv_functions
      ).
      lo_salv_functions->set_all( abap_true ).

      DATA(lo_salv_nodes) = mo_salv_tree->get_nodes( ).
      DATA(lo_root) = lo_salv_nodes->add_node(
        related_node = space
        relationship = if_salv_c_node_relation=>last_child
        data_row = VALUE t000( mandt = '100' )
        text = `Test`
      ).

      lo_salv_nodes->add_node(
        related_node = lo_root->get_key( )
        relationship = cl_gui_column_tree=>relat_last_child
        data_row = VALUE t000( mandt = '200' )
        text = `Test2`
      ).

      mo_salv_tree->display( ).
    CATCH cx_salv_error.
      ASSERT 0 = 1.
    ENDTRY.
  ENDMETHOD.
ENDCLASS.

CLASS lcl_main DEFINITION FINAL CREATE PRIVATE.
  PUBLIC SECTION.
    CLASS-METHODS:
      main.
ENDCLASS.

CLASS lcl_main IMPLEMENTATION.
  METHOD main.
    DATA(lo_controller) = NEW lcl_controller( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  lcl_main=>main( ).

不幸的是,在这种情况下,按钮出现在不同的位置,直接在树之前,而不是在状态栏中。

简短回答 - 不,你不能,你需要一个 GUI 状态 (CUAD)。您可以在运行时隐藏函数,但不能在运行时添加新函数。