alv動態顯示列

weilish發表於2011-06-17
report  z_zxp_test05.
tables: sflight.

data: t_sflight like table of sflight with header line.

data: begin of t_connid occurs 0,
        connid like sflight-connid,
      end of t_connid.

data: i_fieldcat like table of lvc_s_fcat,
      w_fieldcat like line of i_fieldcat.

field-symbols: type standard table.

parameters: p_carrid like sflight-carrid.

start-of-selection.

  perform. sub_calc_column.
  perform. sub_generate_itab.
  perform. sub_display.
*&---------------------------------------------------------------------*
*&      Form  sub_calc_column
*&---------------------------------------------------------------------*
*       1、取數並計算動態列
*----------------------------------------------------------------------*
form. sub_calc_column .

  select *
    from sflight
    into corresponding fields of table t_sflight
   where carrid = p_carrid.

  loop at t_sflight.
    t_connid-connid = t_sflight-connid.
    collect t_connid.
  endloop.

endform.                    " sub_calc_column
*&---------------------------------------------------------------------*
*&      Form  sub_generate_itab
*&---------------------------------------------------------------------*
*       2、生成動態列表,並填充資料
*----------------------------------------------------------------------*
form. sub_generate_itab .

  data: ep_table type ref to data,
        w_table type ref to data,
        p_connid(4).

  field-symbols: type any,
                 .

  define append_fieldcat.
    w_fieldcat-fieldname = &1.
    w_fieldcat-datatype  = &2.
    w_fieldcat-intlen    = &3.
    w_fieldcat-decimals  = &4.
    w_fieldcat-just      = &5.
    append w_fieldcat to i_fieldcat.
    clear: w_fieldcat.
  end-of-definition.

  append_fieldcat 'CARRID' 'CHAR' '3' '' 'L'.
  loop at t_connid.
    append_fieldcat t_connid-connid 'CURR' '13' '2' 'R'.
  endloop.

* 注:這裡的EXPORTING、IMPORTING相對於程式來說,it_fieldcatalog是程式的輸出引數,ep_table是程式得到的引數;
* 對於類方法來說it_fieldcatalog是輸入引數,ep_table是輸出引數(進到方法裡可以看到it_fieldcatalog是EXPORTING
* 引數,ep_table是IMPORTING引數)
  call method cl_alv_table_create=>create_dynamic_table
    exporting
      it_fieldcatalog = i_fieldcat
    importing
      ep_table        = ep_table.

  assign ep_table->* to .
  create data w_table like line of .
  assign w_table->* to .

  loop at t_sflight.
    move-corresponding t_sflight to .
    read table t_connid with key connid = t_sflight-connid.
* 把結構的欄位名為“t_sflight-connid的值”的資料參考變數賦值給欄位符號(Field sysmbol,指標)
* 通過訪問指標來訪問資料參考變數的值,同理,也可以改變指標的值來修改資料參考變數的值。
* 注:ASSIGN COMPONENT + fieldname 中,fieldname欄位型別必須為字元型。
    p_connid = t_sflight-connid.

    assign component p_connid of structure to .
    = t_sflight-price.
    at end of carrid.
      append to .
      clear: .
    endat.
  endloop.

endform.                    " sub_generate_itab
*&---------------------------------------------------------------------*
*&      Form  sub_display
*&---------------------------------------------------------------------*
*       3、顯示動態列表的資料
*----------------------------------------------------------------------*
form. sub_display .

  type-pools: slis.

  data: g_variant like disvariant.                             "(不知道用途,有待研究)
  data: g_layout type slis_layout_alv.                         "優化設定
  data: i_fieldcat type slis_t_fieldcat_alv with header line.  "輸出欄位情況(必輸項)
  data: git_event_exit type slis_t_event_exit,
        gw_event_exit like line of git_event_exit.             "Events for Callback(需瞭解的功能)

  data: p_mdvname like m_crama-ktext,
        p_connid(4).

  define add_it_alv.
    i_fieldcat-fieldname    = &1.
    i_fieldcat-seltext_l    = &2.
    i_fieldcat-outputlen    = &3.      "控制輸出的寬度(在設定g_layout-colwidth_optimize = 'X'後,失效。)
    i_fieldcat-fix_column   = &4.
    i_fieldcat-key          = &5.
    i_fieldcat-just         = &6.
    i_fieldcat-decimals_out = &7.      "控制小數位
    i_fieldcat-round        = &8.
    i_fieldcat-no_zero      = &9.      "控制是否輸出“0”
    append i_fieldcat.
    clear i_fieldcat.
  end-of-definition.

  refresh i_fieldcat.
  add_it_alv 'CARRID' '公司程式碼' '10' 'X' 'X' 'L' '' '' 'X'.
  loop at t_connid.
    concatenate '航班' t_connid-connid '價格' into p_mdvname.
    p_connid = t_connid-connid.
    add_it_alv p_connid p_mdvname '20' 'X' 'X' 'R' '2' '0' 'X'.
  endloop.

  g_layout-colwidth_optimize = 'X'.

  gw_event_exit-ucomm  = 'SUNTEST'.
  gw_event_exit-before = 'X'.
  gw_event_exit-ucomm  = '&IC1'.             "標準功能CODE:DOUBLE CLICK
  gw_event_exit-before = 'X'.                "標準功能前執行USER COMMAND
  append gw_event_exit to git_event_exit.

  call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
      i_callback_program      = sy-repid
      it_fieldcat             = i_fieldcat[]                  "必輸項
      is_variant              = g_variant
      i_save                  = 'X'
      is_layout               = g_layout
      i_grid_title            = 'Sun Test Assign的動態用法'
      it_event_exit           = git_event_exit
      i_callback_user_command = 'FRM_UCOMM'                   "注:FRM_UCOMM是子程式的名稱
    tables
      t_outtab                =                     "必輸項
    exceptions
      program_error           = 1
      others                  = 2.

endform.                    " sub_display
*&---------------------------------------------------------------------*
*&      Form  FRM_UCOMM
*&---------------------------------------------------------------------*
*       4、自定義ALV的雙擊事件(i_callback_user_command = 'FRM_UCOMM')
*----------------------------------------------------------------------*
form. frm_ucomm using r_ucomm like sy-ucomm
                     rs_selfield type slis_selfield.

  case r_ucomm.
    when 'SUNTEST'. "雙擊

    when '&IC1'.
      if rs_selfield-fieldname eq 'MAKTX'.

      else.
        r_ucomm = '&ETA'.                    "強行將雙擊事件關聯到F2,及介面上的檢視按鈕
      endif.
  endcase.

endform.                    "FRM_UCOMM

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

相關文章