cancel
Showing results for 
Search instead for 
Did you mean: 

How to dynamicaly get state property of element

glauco
Active Contributor
0 Kudos

Hi friends.

I have the label element on a the view.

I need to get the element property "state" of linked element of this label. (It can be input field, text edit) I can use "get label for" and get linked element ID.

            lr_elem = me->Zget_elem_from_node(
                 p_i_node_name    = lc_node_name
                 p_i_r_wd_context = p_i_r_wd_context
             ).


But I can't get this property yet.

How to get state property of "lr_elem"  it in this way ?

Thanks.

Glauco

Accepted Solutions (1)

Accepted Solutions (1)

former_member184578
Active Contributor
0 Kudos

Hi,

Try the below code:


DATA: lr_label     TYPE REF TO cl_wd_label,

         lr_ui_elem   TYPE REF TO cl_wd_input_field,

         lv_label_for TYPE string,

         lv_state     TYPE wdui_state.

   lr_label ?= view->get_element( id = 'LABEL' ).

*  Get Label For

   lv_label_for = lr_label->get_label_for( ).

*  Get Ui element for Label

   lr_ui_elem ?= view->get_element( id  = lv_label_for ).

*  Get State of UI element

   lv_state = lr_ui_elem->get_state( ).

lv_state will have 01 if it is Required/Mandatory Field else 00.

hope this helps,

Regards,

Kiran

glauco
Active Contributor
0 Kudos

Hi Kiran.

Thank you very much for your quick answer and help.

One thing to complement your answer is, at first, I can have 3 type of mandatory fields here: Input Field, Text Edit and Drop Down By Key.

So, For each field on the view, I had to ask its get_state( ) method, because the generic element object did not have this (lr_uielement TYPE REF TO cl_wd_uielement). So I did like below.


DATA: lr_uielement TYPE REF TO cl_wd_uielement.


          lr_uielement ?= lr_v_main_ctrl->get_element( id  = lc_label_for ).

           DATA: lr_imput_field TYPE REF TO  cl_wd_input_field,
                     lr_ddk          TYPE REF TO   cl_wd_dropdown_by_key ,
                     lr_text_edit   TYPE REF TO   cl_wd_text_edit,
                     lc_state       TYPE wdui_state,
                     lc_value       TYPE string.

           CASE lr_uielement->_definition_name.

             WHEN 'INPUT_FIELD'.
               lr_imput_field ?= lr_uielement.
               lc_state = lr_imput_field->get_state( ).
               lc_value = lr_imput_field->get_value( ).
             WHEN 'DROP_DOWN_BY_KEY'.
               lr_ddk ?= lr_uielement.
               lc_state = lr_ddk->get_state( ).
               lc_value = lr_ddk->get_selected_key( ).
             WHEN 'TEXT_EDIT'.
               lr_text_edit ?= lr_uielement.
               lc_state = lr_text_edit->get_state( ).
               lc_value = lr_text_edit->get_value( ).
           ENDCASE.

Now it works great.

But, problem here is I'm obligated to daclere each correct "type ref" to represent each field type. In future, I could need include new object types like dropDownByIndex or whatever type for obligatpry fields.

Question to improve this code to be more generic : Does anyone has the sugestion / examples on how to use any generic object to ask, above in CASE statement, this method get_state( ) ? Or another way like using field symbols and declaring dynamically, so I can ask dynamically for get_state( ) method without needing to declare each field type ?

thank you.

Glauco

former_member184578
Active Contributor
0 Kudos

Hi,

You can call the get_state( ) method dynamically. And you can use the generic ui instead of declaring for each ui element type using type ref.

This code:


DATA: lr_label     TYPE REF TO cl_wd_label,

         lr_ui_elem   TYPE REF TO cl_wd_uielement,

         lv_label_for TYPE string,

         lv_state     TYPE wdui_state.

   lr_label ?= view->get_element( id = 'LABEL' ).

*  Get Label For

   lv_label_for = lr_label->get_label_for( ).

*  Get Ui element for Label

   lr_ui_elem ?= view->get_element( id  = lv_label_for ).

*  Get State of UI element

    CALL METHOD lr_ui_elem->('GET_STATE')

     RECEIVING

       value = lv_state.

hope this helps,

Regards,

Kiran

glauco
Active Contributor
0 Kudos

Greate tip Kiran.

But I added a try statment inside this tt_ui_elements loop, because there are ui elements without get_state() method, e.g. text_edit TEXT_VIEW ui element, it would give dump during executing time. So I put it a try..catch...endtry.

Now it works greate as dynamic way, like below, and get state only for correct ui_elements and ignore wrong ui_elements into catch statement.


TRY.
          CALL METHOD lr_uielement->('GET_STATE')
               RECEIVING
                 value = lc_state.


   CATCH cx_root.

ENDTRY.

OBS.: Before your answer I was trying to use something like this below but give me dump.

* get reference of lr_uielement into dref1.
*
* CREATE DATA dref1 TYPE (lr_uielement->_definition_name).
* ASSIGN dref1->* TO <fs>.
** lc_value = <fs>->get_value( ). error here in fs->...

tahnk you.

Glauco

former_member184578
Active Contributor
0 Kudos

Welcome! Yes, you must catch the exceptions using TRY..CATCH block when calling methods dynamically.

Regards,

Kiran

Answers (0)