alv动态显示列


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: <t_output> 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: <w_output> type any,
                 <fields_name>.

  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 <t_output>.
  create data w_table like line of <t_output>.
  assign w_table->* to <w_output>.

  loop at t_sflight.
    move-corresponding t_sflight to <w_output>.
    read table t_connid with key connid = t_sflight-connid.
* 把结构<w_output>的字段名为“t_sflight-connid的值”的数据参考变量赋值给字段符号(Field sysmbol,指针)
* 通过访问指针<fields_name>来访问数据参考变量的值,同理,也可以改变指针的值来修改数据参考变量的值。
* 注:ASSIGN COMPONENT + fieldname 中,fieldname字段类型必须为字符型。
    p_connid = t_sflight-connid.

    assign component p_connid of structure <w_output> to <fields_name>.
    <fields_name> = t_sflight-price.
    at end of carrid.
      append <w_output> to <t_output>.
      clear: <w_output>.
    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                = <t_output>                    "必输项
    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

你可能感兴趣的:(command,layout,table,callback,output,structure)