ABAP--通过自定义类使用ALV(相应事件)的代码

Class Definition

*----------------------------------------------------------------------*
*       CLASS lcl_alv DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_alv DEFINITION.
  PUBLIC SECTION.

    METHODS select_alv IMPORTING amount TYPE i.

    METHODS display_alv.

    METHODS on_link_click
                FOR EVENT link_click OF cl_salv_events_table
                IMPORTING row column.

  PROTECTED SECTION.

    DATA t_alv TYPE STANDARD TABLE OF alv_tab.

    DATA: o_table   TYPE REF TO cl_salv_table,
          o_columns TYPE REF TO cl_salv_columns_table,
          o_column  TYPE REF TO cl_salv_column_table,
          o_events  TYPE REF TO cl_salv_events_table.

ENDCLASS.                    "lcl_alv DEFINITION

Class Implementation

*----------------------------------------------------------------------*
*       CLASS lcl_alv IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_alv IMPLEMENTATION.

  METHOD select_alv.

    SELECT *
      FROM alv_tab
      INTO TABLE t_alv
          UP TO amount ROWS.

  ENDMETHOD.                    "SELECT_ALV

  METHOD display_alv.

    TRY.
        cl_salv_table=>factory(
          IMPORTING
            r_salv_table = o_table
          CHANGING
            t_table      = t_alv ).
      CATCH cx_salv_msg.                                "#EC NO_HANDLER
    ENDTRY.

    o_columns = o_table->get_columns( ).
    o_columns->set_optimize( abap_true ).

    TRY.
        o_column ?= o_columns->get_column( 'CARRID' ).
        o_column->set_cell_type( if_salv_c_cell_type=>hotspot ).
      CATCH cx_salv_not_found.
    ENDTRY.

    o_events = o_table->get_event( ).

    SET HANDLER me->on_link_click FOR o_events.

    o_table->display( ).

  ENDMETHOD.                    "display_alv

  METHOD on_link_click.

    DATA: l_row_string TYPE string,
          l_col_string TYPE string,
          l_row        TYPE char128.

    MESSAGE i000(0k) WITH 'You Clicked on Line'
                           row
                           'Of Column'
                           column.

  ENDMETHOD.                    "on_single_click
ENDCLASS.                    "lcl_alv IMPLEMENTATION

Defining Objects

DATA o_alv TYPE REF TO lcl_alv.Instance Creation

START-OF-SELECTION.

  CREATE OBJECT o_alv.

Calling Methods

o_alv->select_alv( 30 ).
o_alv->display_alv( ).

posted on 2007-10-25 11:01  毛小娃  阅读(145)  评论(0编辑  收藏  举报

导航