二维码

[教程] CODING FOR EDIT, CANCEL AND SAVE BUTTON EVENT HANDLERS

Twilight发表于 2016-01-13 01:31Twilight 最后回复于 2016-01-13 01:31 [复制链接] 3732 0

We have added three buttons EDIT, CANCEL and SAVE buttons to the over view page in the previous tutorial.

Let us code in the event handlers of those buttons.

Go to the component and overview page and create event handlers with name ‘EDIT’, ‘CANCEL’ and ‘SAVE’.  (Event handler names are case sensitive;all these name of event handlers should match with on click properties of buttons that we have created in the previous chapter).
EDIT event handler.

Write the following code in the event handler.
  1. METHOD eh_onedit.

  2.   DATA:lr_entity TYPE REF TO cl_crm_bol_entity.

  3.   lr_entity ?= me->typed_context->header->collection_wrapper->get_current( ).
  4.   CHECK lr_entity IS BOUND.
  5.   CHECK lr_entity->is_change_allowed( ) = abap_true.
  6.   lr_entity->lock( ).
  7.   IF lr_entity->is_locked( ) = abap_false.
  8.     me->view_group_context->reset( ).
  9.   ELSE.
  10.     me->view_group_context->set_all_editable( ).
  11.   ENDIF.

  12. ENDMETHOD.
复制代码

First we are reading current record from the context node HEADER into LR_ENTITY.
Next, we are checking, it is bound or not.

Next we are using method IS_CHANGE_ALLOWED or not, if change is allowed, then we are applying LOCK on that record.

Next if lock is successful, using VIEW_GROUP_CONTEXT, we are setting all views editable else we are just resetting the view group context, which put all views into display mode.

CANCEL event handler:
  1. METHOD eh_oncancel.

  2.   DATA:lr_entity TYPE REF TO cl_crm_bol_entity,
  3.        lr_tx TYPE REF TO if_bol_transaction_context.

  4.   lr_entity ?= me->typed_context->header->collection_wrapper->get_current( ).
  5.   CHECK lr_entity IS BOUND.
  6.   lr_tx ?= lr_entity->get_transaction( ).
  7.   lr_tx->revert( iv_suppress_buffer_sync = abap_true ).
  8.   view_group_context->reset( ).

  9. ENDMETHOD.
复制代码

We are reading the current record and getting the instance of transaction from it. calling the revert method will cancel all the changes that we have done.

Calling reset method will put all views into display mode.

SAVE event handler:
  1. METHOD eh_onsave.

  2.   DATA:lr_entity TYPE REF TO cl_crm_bol_entity,
  3.        lr_tx TYPE REF TO if_bol_transaction_context,
  4.        lr_core TYPE REF TO cl_crm_bol_core.

  5.   lr_entity ?= me->typed_context->header->collection_wrapper->get_current( ).

  6.   CHECK lr_entity IS BOUND.
  7.   lr_tx ?= lr_entity->get_transaction( ).
  8.   IF lr_tx IS NOT BOUND.
  9.     lr_core = cl_crm_bol_core=>get_instance( ).
  10.     lr_tx = lr_core->begin_transaction( ).
  11.   ENDIF.
  12.   IF lr_tx IS BOUND.
  13.     IF lr_tx->check_save_possible( ) = abap_true.
  14.       CHECK lr_tx->save( ) EQ abap_true.
  15.       lr_tx->commit( ).
  16.     ENDIF.
  17.   ENDIF.

  18.   view_group_context->reset( ).

  19. ENDMETHOD.
复制代码

We are reading the current record from the context node.  If node is bound, then we are taking the transaction context of it.

If it is initial, then we are asking core class to provide the transaction context.
Then we are checking whether save operation is possible or not using CHECK_SAVE_POSSIBLE method. If we can save the transaction, then we are calling SAVE method and commit the work with one more method COMMIT.

One save is done, then put all views into display mode by resetting the view group context.

Activate all methods and test the application.
Clicking on EDIT button will make all views shift into edit mode.
edit.jpg

Press cancel button, all views will go into display mode.

Next , we will learn one important topic called UI OBJECT TYPE.
回复

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表