ALV顯示OO實現(摘)

weilish發表於2009-12-25
ALV用OO方式實現,似乎沒有function好用,但是可以再一個螢幕上顯示兩個ALV。程式碼例子如下:

全域性資料:

DATA : obj_wcl_container TYPE REF TO cl_gui_custom_container, "控制容器類
       lt_f4       TYPE lvc_t_f4,
       ls_f4       TYPE lvc_s_f4,
       obj_wcl_alv TYPE REF TO cl_gui_alv_grid . "ALV控制類

alv顯示:

FORM. frm_alv_display  USING gt_list LIKE gt_list.
  DATA : ls_variant  TYPE disvariant .
*--- 存放欄位目錄的內表
  DATA lt_fieldcat TYPE lvc_t_fcat .
*--- 佈局結構
  DATA ls_layout TYPE lvc_s_layo .
  DATA: lt_exclude TYPE ui_functions.     "alv不需要的圖示按鈕
  ls_variant-report = sy-repid.

  IF obj_wcl_alv IS INITIAL .
    CREATE OBJECT: obj_wcl_container
     EXPORTING
       container_name = 'OBJ_WCL_CONTAINER'.

“螢幕上要有一個定製的容器,名字必須大寫為OBJ_WCL_CONTAINER

    CREATE OBJECT obj_wcl_alv
      EXPORTING
        i_parent = obj_wcl_container.

*-----分配雙擊事件
      CREATE OBJECT event_receiver.
      SET HANDLER event_receiver->handle_double_click
      FOR obj_wcl_alv.
  ENDIF .
*-----消除按鈕
  PERFORM. exclude_tb_functions CHANGING lt_exclude.
*-----設定欄位
  PERFORM. prepare_field_catalog CHANGING lt_fieldcat .
*-----設定佈局
  PERFORM. prepare_layout CHANGING ls_layout .
  CALL METHOD obj_wcl_alv->register_f4_for_fields
    EXPORTING
      it_f4 = lt_f4.
*-----顯示ALV
  CALL METHOD obj_wcl_alv->set_table_for_first_display
    EXPORTING
      is_variant                    = ls_variant
      is_layout                     = ls_layout
      i_save                        = 'A'
      it_toolbar_excluding          = lt_exclude
    CHANGING
      it_outtab                     = gt_list[]
      it_fieldcatalog               = lt_fieldcat
    EXCEPTIONS
      invalid_parameter_combination = 1
      program_error                 = 2
      too_many_lines                = 3
      OTHERS                        = 4.

ENDFORM.                    " FRM_ALV_DISPLAY

消除按鈕:

FORM. exclude_tb_functions  CHANGING pt_exclude TYPE ui_functions.
  DATA ls_exclude TYPE ui_func.

  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy_row.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_delete_row.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_append_row.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_insert_row.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_move_row.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_cut.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste_new_row.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_loc_undo.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_check.
  APPEND ls_exclude TO pt_exclude.
  ls_exclude = cl_gui_alv_grid=>mc_fc_refresh.
  APPEND ls_exclude TO pt_exclude.
ENDFORM.                    " EXCLUDE_TB_FUNCTIONS

設定欄位:

FORM. prepare_field_catalog CHANGING ct_fieldcat TYPE lvc_t_fcat.
  DATA ls_fcat TYPE lvc_s_fcat .
  REFRESH ct_fieldcat .

  CLEAR ls_fcat .
  ls_fcat-fieldname = 'SELECTED' .
  ls_fcat-coltext     = '選擇'.
  ls_fcat-outputlen     = 2.
  ls_fcat-checkbox = 'X' .
  ls_fcat-edit = 'X' .
  ls_fcat-key = 'X' .
  APPEND ls_fcat TO ct_fieldcat  .

  init ls_fcat 'INFO_TYPE_CODE' ct_fieldcat '金稅發票型別程式碼' 12  'X'.
  init ls_fcat 'INFO_NUMBER' ct_fieldcat '金稅發票號碼' 12   'X'.
  init ls_fcat 'SERIAL_NUMBER' ct_fieldcat '流水號' 12   'X'.

  init ls_fcat 'UPDATE_DATE' ct_fieldcat '發票日期' 8  ''.
  init ls_fcat 'CLIENT_NAME' ct_fieldcat '購方名稱' 30  ''.
  init ls_fcat 'SELLER_NAME' ct_fieldcat '銷方名稱' 30   ''.

ENDFORM.                    " PREPARE_FIELD_CATALOG

設定佈局:

FORM. prepare_layout  CHANGING cs_layout TYPE lvc_s_layo.
  cs_layout-zebra = 'X' .
  cs_layout-grid_title = '列印正負銷貨清單' .
  cs_layout-smalltitle = 'X' .
  cs_layout-edit_mode = 'X' .
  cs_layout-edit = '' .
  cs_layout-cwidth_opt = 'X' .

ENDFORM.                    " PREPARE_LAYOUT

在PAI事件中如果有全選可取消全選:

form. FRM_SELECT_ALL .
  data: ls_list like line of gt_list .
  loop at gt_list into ls_list.
    MOVE 'X' TO ls_list-selected.
    MODIFY gt_list FROM ls_list INDEX sy-tabix TRANSPORTING selected.
  endloop.
*  call method obj_wcl_alv->refresh_table_display.

endform.                    " FRM_SELECT_ALL

如果有雙擊事件需要註冊雙擊事件,和自己寫實現程式碼。

  CLASS lcl_event_receiver DEFINITION.
    PUBLIC SECTION.
      METHODS:
        handle_double_click
        FOR EVENT double_click OF cl_gui_alv_grid
          IMPORTING e_row.
  ENDCLASS.                    "lcl_event_receiver DEFINITION
*&---------------------------------------------------------------------*
*&      雙擊事件的實現
*&---------------------------------------------------------------------*
  CLASS lcl_event_receiver IMPLEMENTATION.

    METHOD handle_double_click.
      DATA wa_bkpf LIKE LINE OF gt_list.
      READ TABLE gt_list INTO wa_bkpf INDEX e_row-index.   "讀取當前行

      "進入發票
      SET PARAMETER ID 'VF' FIELD wa_bkpf-vbeln.
      CALL TRANSACTION 'VF03' AND SKIP FIRST SCREEN.

    ENDMETHOD.                    "catch_doubleclick

  ENDCLASS.                    "lcl_event_receiver IMPLEMENTATION

常見錯誤:

1.alv在螢幕上不顯示!

一定要在螢幕裡畫上一個custom且名字是容器的名字,要大寫!OBJ_WCL_CONTAINER

2.核取方塊不起作用

核取方塊選擇了全選之後沒有全選,因為在pai之後接著會pbo,我把取數寫進了pbo中這樣核取方塊欄位就被覆蓋更新了。還有一種情況就是欄位名寫錯了,但是系統並沒有報錯。注意還要用下面的方法更新:

form. FRM_RENEW_DATA_1001 .
  DATA: l_valid TYPE c .
***更新內表中的選擇標記位
  CALL METHOD obj_wcl_alv->check_changed_data
    IMPORTING
      e_valid = l_valid.
endform.

DEFINE init .
*&1 structure
*&2 fieldname
*&3 tabname
*&4 seletext_m
*&5 outputlen
  clear &1 .
  &1-fieldname = &2 .
*  &1-tabname       = &3.
  &1-coltext     = &4.
  &1-outputlen     = &5.
  &1-no_zero       = 'X' .
  &1-key           = &6 .
  append &1 to &3 .
END-OF-DEFINITION .

退出命令,注意要設定為E。

module exit at exit-command.

IF g_ok_code = 'BACK' .
  SET SCREEN 0.
  LEAVE SCREEN.
endif . 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16794144/viewspace-623625/,如需轉載,請註明出處,否則將追究法律責任。

相關文章