cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamically create a button with an action handler

daniel_humberg
Contributor
0 Kudos

I have created a button in my coding, and successfully added it to the view.


    DATA lo_button           TYPE REF TO cl_wd_button.
    DATA lo_flow_layout_data TYPE REF TO cl_wd_flow_data.

    lo_button = cl_wd_button=>new_button( view = mo_view ).
    lo_button->set_text( me->mv_refresh_button_text ).
    lo_button->set_on_action( value = 'do_a_dummy_refresh' ).

*   set layout (matrix, flow, etc.)
*   ....

    lo_container->add_child( lo_button ).

When the user clicks the button, I would just like to refresh the view (i.e. go to WDDOMODIFYVIEW etc.).

Currently, if the button is clicked, I get the dump "Access via 'NULL' object reference not possible" because for the action there is not action handler.

Is there a way to avoid the dump, or to dynamically create an action handler?

Regardsd,D.

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

hi daniel...

just create an empty metho say... sample.

and in the action handler give this method name.

---regards,

alex b justin

daniel_humberg
Contributor
0 Kudos

Does this method have to be in the same view than the button?

The problem is that the buttons are created dynamically, and the coding where the buttons are created is not inside the view where the buttons are displayed.

Former Member
0 Kudos

Hi Daniel,

Try the below code.

data: LR_CONTAINER         type ref to CL_WD_UIELEMENT_CONTAINER,
      LR_BUTTON            type ref to CL_WD_BUTTON.

LR_BUTTON    =  CL_WD_BUTTON=>NEW_BUTTON(  id = 'BUTT_1' text = 'NEW'  on_action = 'CLICK' ).
 LR_CONTAINER ?= view->GET_ELEMENT( 'ROOTUIELEMENTCONTAINER' ).

LR_CONTAINER->ADD_CHILD( LR_BUTTON ).

Here Click is the your action name. So using the following code you can map the button and the corresponding event.

DATA:

    lt_parameters LIKE if_wd_event=>parameters,

    parameter     LIKE LINE OF lt_parameters,

    lr_link1      TYPE REF TO cl_wd_button.

lr_link1 ?= view->get_element( 'BUTT_1' ).

 parameter-name  = 'CLICK'.

  parameter-value = 1.

  parameter-type  = 'g'.

  INSERT parameter INTO TABLE lt_parameters.

     lr_link1->map_on_action( lt_parameters ).

Now create one attribute 'clicked_at' and set the value as 'click' ( event name for your button ).

wd_context->set_attribute( name = 'CLICKED_AT' value = CLICK ).

So when ever the button is clicked, then the value of your attribute should be 1(parameter-value = 1).

So based on that attribute value you can write your code for refresh the view.

Actually this code for a method with in a view. But you can make it as other views also.

Thanks.

daniel_humberg
Contributor
0 Kudos

Hi Viji,

I tried your coding, but it still gives me a dump called "Access via 'NULL' object reference not possible" (in CL_WDR_WINDOW_PHASE_MODEL=>GET_ACTION_FROM_EVENT ) when clicking the button.

 
    lo_button->set_text( me->mv_refresh_button_text ).
    lo_button->set_on_action( value = 'DO_DUMMY_REFRESH' ).

    DATA lt_parameters LIKE if_wd_event=>parameters.
    DATA ls_parameter  LIKE LINE OF lt_parameters.

    ls_parameter-name  = 'DO_DUMMY_REFRESH'.
    ls_parameter-value = 1.
    ls_parameter-type  = 'g'.
    INSERT ls_parameter INTO TABLE lt_parameters.
    lo_button->map_on_action( lt_parameters ).

*   set layout (matrix, flow, etc.)
     /psc/cl_zxa_wd_tools=>set_layout_data_for_ui_element(
                                                      io_ui_element       = lo_button
                                                      iv_layout_data_type = me->mv_layout_data_type ).

Any idea?

Former Member
0 Kudos

Hi Daniel,

Using debugging you can try to find exactly where the dump is raised.

Instaed of using following code,

lo_button->set_text( me->mv_refresh_button_text ).
lo_button->set_on_action( value = 'DO_DUMMY_REFRESH' ).

Give the text and action in New button itself.

Please refer my code.

LR_BUTTON    =  CL_WD_BUTTON=>NEW_BUTTON(  id = 'BUTT_1' text = 'NEW'  on_action = 'CLICK' ).

Thanks.

daniel_humberg
Contributor
0 Kudos

I created another button with the action "Do_DUMMY_REFRESH", and created an event handler method called ONACTION_DO_DUMMY_REFREH.

After this, the coding above works. However, I would like to create that method manually. Is there any other way?

Former Member
0 Kudos

Hi Daniel,

This is the only way to map your button and the Event.

Thanks.

daniel_humberg
Contributor
0 Kudos

By the way, after creating the event handler method manually, it also works without the event parameters, so i did not need the following coding.

What exactly is it used for?

 
    DATA lt_parameters LIKE if_wd_event=>parameters.
    DATA ls_parameter  LIKE LINE OF lt_parameters.
 
    ls_parameter-name  = 'DO_DUMMY_REFRESH'.
    ls_parameter-value = 1.
    ls_parameter-type  = 'g'.
    INSERT ls_parameter INTO TABLE lt_parameters.
    lo_button->map_on_action( lt_parameters ).

Former Member
0 Kudos

Hi Daniel,

Sorry. for Single button there is no need for that code. But if you have two buttons with same event name means you need it.

Anyway now you can remove that code. Sorry for the confusing..

Thanks.