Is it possible to add a user defined function to an instance of CL_SALV_TREE
without copying GUI status to the report where this instance is used?
What I am trying to do is to add a custom function with add_function
method.
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( ).
However the button does not appear.
I remember that I tried to do it some time ago with CL_SALV_TABLE
and I got an explicit exception when using add_function
.
What I tried back then was.
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( ).
It ends with a short dumped caused by unhandled exception CX_SALV_METHOD_NOT_SUPPORTED
. What I see in the SAP standard code is
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.
From this piece of code one could suppose that it is possible to add a new function with add_function
method for the object IF_SALV_C_TABLE_OBJECTS=>TREE
.
So far I used the following workaround.
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( ).
Unfortunately the buttons appear in such case in a different location, directly before the tree and not in the status bar.
Short answer - no, you can't, you need a GUI Status (CUAD
). You can hide functions at runtime, but you can't add new functions at runtime.