分享

ALV?OO事件获取

 昵称7678957 2011-09-26

首先大概讲一下ALV GRID双击事件的实现原理:ALV GRID继承自CL_GUI_ALV_GRID这个Class interface,而在CL_GUI_ALV_GRID这个class interface中已经定义了很多的Methods以及Events。例如,在ALV GRID中我们常用到的 SET_TABLE_FOR_FIRST_DISPLAY这个method就可以在CL_GUI_ALV_GRID的methods中找到。因此如果我们需要查看在ALV GRID中我们能够使用哪些method或者那些Event,只需要在Repository Browser中选择Class/Interfase然后输入CL_GUI_ALV_GRID,就可以查看到所以已经定义好的menthod以及events等等。言归正传,在这次试验中我们需要实现一个ALV Grid的Dobule Click事件,我们在CL_GUI_ALV_GRID的Events中可以找到一个"DOUBLE_CLICK"这样的事件。而这个DOUBLE_CLICK都接收那些参数呢?双击DOUBLE_CLICK,然后在右边的栏位中选中DOUBLE_CLICK时间,在左上方有一个Paremeters的按钮,点击进去以后我们看到有E_ROW、E_COLUMN,ES_ROW_NO这三个参数。OK,现在事件已经找到了我们接下来需要做的工作就是定义一个类去实现这个事件,并将该事件注册到我们的ALV GRID中。下面将结合实际的例子来进行讲述:

      一,在Screen中绘制一个Custom Control,并命名为ALV_GRID;在Process before output中定义 MODULE STATUS_0100,在Process After Input中定义 MODULE USER_COMMAND_0100  

      二,在程序中定义一个名为 grid_event_receiver的CLASS。具体代码如下:

            CLASS grid_event_receiver DEFINITION.
              PUBLIC SECTION.
                 METHODS:
                      handler_Double_click
                           FOR EVENT DOUBLE_CLICK of cl_gui_alv_grid
                           IMPORTING e_row e_column,
          ENDCLASS.                    "grid_event_rece

      三,在程序中对grid_event_receiver这个CLASS进行具体实现。具体代码如下:

              CLASS grid_event_receiver IMPLEMENTATION.
                   METHOD handler_Double_click.
                      DATA: ls_sflight like line of gt_sflight.
                      READ TABLE gt_sflight INDEX e_row-index INTO ls_sflight.
                       SELECT * from sbook INTO TABLE gt_sbook
                                                     WHERE carrid   = ls_sflight-carrid
                                                      and     connid   = ls_sflight-connid
                                                      and     fldate   = ls_sflight-fldate.
                       PERFORM DETAIL_LIST.
                       ENDMETHOD.                 "handler_Double_click
             ENDCLASS.                    "grid_event_receiver IMPLEMENTATION                                                

通过以上的代码,我们可以看到在handler_double_click事件中我们Dobule_Click事件中的Paremeter e_row(表示双击事件发生的该行)的index从gt_sflight 这个内表中读取数据,然后再从sbook这个table中读取数据,最后再执行DETAIL_LIST这个form。

      四,在程序中定义一些变量,代码如下:

                 DATA: custom_container type ref to cl_gui_custom_container,              "custom_container:the container of grid1
                           grid1  type ref to cl_gui_alv_grid,                                             "The main alv grid to show the mail data
                            grid2  type ref to cl_gui_alv_grid,                                               "The Detail alv grid to show the detail data
                           dialogbox_container type ref to cl_gui_dialogbox_container,         "dialogbox_container:The container of grid2
                           event_receiver type ref to grid_event_receiver,                            "event_receiver:Point to grid_event_recerive

      五,在PBO的moudule中读取数据并显示在grid1并注册Double_Click事件到grid1上。在 STATUS_0100这个module中首先要第四部中定义的变量进行实例话,然后调用ALV Grid的 SET_TABLE_FOR_FIRST_DISPLAY方法显示数据,最后需要将前面定义的double click事件注册到该grid上。具体代码为:

IF custom_container is INITIAL.
    PERFORM Get_DATA TABLES GT_OUTPUT[].      "Get data from table and fill the data into internal table GT_OUTPUT
    CREATE OBJECT custom_container            "Create the instance of custom_container
        EXPORTING
           CONTAINER_NAME = 'ALV_GRID'         "Set the Container_name to 'ALV_GRID'(Define in Screen)
        exceptions
           cntl_error = 1
           cntl_system_error = 2
           create_error = 3
           lifetime_error = 4
           lifetime_dynpro_dynpro_link = 5.
    CREATE OBJECT grid1 EXPORTING i_parent = custom_container.  "Create the instance of grid1 and set custom_container as grid1's Container
     gs_layout-grid_title = 'Flight'(100).
     gs_layout-excp_fname = g_lights_name.
     gs_layout-sel_mode = 'A'.
    CALL METHOD grid1->SET_TABLE_FOR_FIRST_DISPLAY       "Call the method to show data
      EXPORTING
         I_STRUCTURE_NAME               = 'SFLIGHT'
      CHANGING
         IT_OUTTAB                      = GT_OUTPUT[]
    EXCEPTIONS
      INVALID_PARAMETER_COMBINATION = 1
      PROGRAM_ERROR                  = 2
      TOO_MANY_LINES                 = 3
      others                         = 4.
    IF SY-SUBRC <> 0.
   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    CREATE OBJECT event_receiver.                       "Create the instance of event_receiver
    set HANDLER event_receiver->handler_Double_click FOR grid1.   "register the double_click event to grid1
  ENDIF.

     六,在PAI的module中处理其他一些代码。

     通过以上一些步骤我们就可以实现ALV GRID的双击事件了。需要注意的是:在以上的代码中我们没有对Grid2以及dialogbox_container进行实例话。而对这两者的实例化以及如何在grid2种显示详细数据放置在Dobule Click事件的实现过程中(对应前面代码中的 PERFORM DETAIL_LIST)。Detail_LIST中的具体代码如下:

FORM DETAIL_LIST .
  CREATE OBJECT dialogbox_container         "Create the instance of dialogbox_container
     EXPORTING
        TOP       = 100
       LEFT      = 150
        WIDTH     = 800
        HEIGHT    = 200
        LIFETIME = cntl_lifetime_dynpro
        .
  CREATE OBJECT grid2                      "Create the instance of grid2 and set dialogbox_container as its container
       EXPORTING i_parent = dialogbox_container.
  CALL METHOD grid2->SET_TABLE_FOR_FIRST_DISPLAY   "call the method to show the data
    EXPORTING
       I_STRUCTURE_NAME = 'SBOOK'
    CHANGING
       IT_OUTTAB         = gt_sbook.
ENDFORM.                    " DETAIL_LIST

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多