ALV显示红绿灯(FM ALV 和 OO ALV两种实现方式)

直接上代码吧,copy过去就可以用:

*&---------------------------------------------------------------------*
*& Report  ZLM_ALV_07
*& http://blog.sina.com.cn/sapliumeng
*&---------------------------------------------------------------------*
*1)在alv引用类型池定义时加入icon,如type-pools: slis, icon.
*
*2)给显示内表itab定义一字段statu存储状态灯;
*
*3)循环内表itab给字段statu值赋:
*绿灯:itab-statu = icon_led_green.
*红灯:itab-statu = icon_led_red.
*4)alv的fieldcat属性i_fieldcat-icon = 'X'.
*&---------------------------------------------------------------------*

REPORT  zlm_alv_07.

*&---------------------------------------------------------------------*
*& 声明
*&---------------------------------------------------------------------*
TYPE-POOLS: slis, icon.

*&---------------------------------------------------------------------*
*&基本数据定义
*&---------------------------------------------------------------------*
DATA:
      BEGIN OF str_data,
        id     TYPE int4,
        age    TYPE int4,
        statu  TYPE char10,
      END OF str_data.

DATA:
      g_it_data LIKE TABLE OF str_data,
      g_wa_data LIKE str_data.

*&---------------------------------------------------------------------*
*& ALV相关定义
*&---------------------------------------------------------------------*
DATA: fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE.

*&---------------------------------------------------------------------*
*& 宏定义
*&---------------------------------------------------------------------*
DEFINE fill_fieldcat.
  fieldcat-fieldname = &1.
  fieldcat-seltext_l = &2.
  fieldcat-icon    = 'X'.      "图标功能
  append fieldcat.
END-OF-DEFINITION.

*&---------------------------------------------------------------------*
*& 主逻辑
*&---------------------------------------------------------------------*

START-OF-SELECTION.
  PERFORM zform_fill_data.
  PERFORM zform_show.

*&---------------------------------------------------------------------*
*& form区
*&---------------------------------------------------------------------*

FORM zform_fill_data.
  fill_fieldcat  'ID'   '编号'.
  fill_fieldcat  'AGE'  '年龄'.
  fill_fieldcat  'STATU' '灯饰'.

  g_wa_data-id    = 1.
  g_wa_data-age   = 12.
  g_wa_data-statu = icon_led_red.

  APPEND g_wa_data TO g_it_data.

  g_wa_data-id    = 2.
  g_wa_data-age   = 13.
  g_wa_data-statu = icon_led_green.

  APPEND g_wa_data TO g_it_data.
ENDFORM.                    "zform_fill_data

*&---------------------------------------------------------------------*
*&      Form  zform_show
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM zform_show.
  CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
    EXPORTING
      it_fieldcat = fieldcat[]
    TABLES
      t_outtab    = g_it_data[].
ENDFORM.                    "zform_show



OO 方式ALV 红绿灯

效果:

ALV显示红绿灯(FM ALV 和 OO ALV两种实现方式)

主要代码:

*&---------------------------------------------------------------------*

*&  全局变量

*&---------------------------------------------------------------------*

DATA:

      BEGIN OF str_spfli,

        checkbox TYPE char1,

        status   TYPE icon_d,                 "检查状态图标

        carrid   TYPE  sflight-carrid       , "航线承运人ID

        connid   TYPE  sflight-connid       , "航班连接 Id

        fldate   TYPE  sflight-fldate       , "航班日期

        price    TYPE  sflight-price        , "航空运费

        cellstyle TYPE lvc_t_styl,          "可编辑字段SORT表

      END OF  str_spfli.

DATA:

      g_it_show LIKE TABLE OF str_spfli WITH KEY checkbox carrid connid,

      g_wa_show LIKE str_spfli.

START-OF-SELECTION.

  "获取数据

  SELECT carrid

         connid

         fldate

         price

    FROM sflight INTO CORRESPONDING FIELDS OF  TABLE g_it_show.

  FIELD-SYMBOLS 《l_show》 LIKE LINE OF  g_it_show.  "英文单引号会被html误解析,特使用《》

  DATA:l_dex TYPE int4.

  LOOP AT g_it_show ASSIGNING 《l_show》.

    l_dex = sy-tabix MOD 3.

    CASE l_dex.

      WHEN 1 .

        《l_show》-status = icon_led_green.

      WHEN 2.

       《l_show》-status = icon_led_red.

      WHEN OTHERS.

        《l_show》-status = icon_led_yellow.

    ENDCASE.

  ENDLOOP.

  "调用屏幕

  CALL SCREEN 9001.

FORM zform_alv_show .

  DATA:   l_style      TYPE i,

          ls_layout    TYPE lvc_s_layo,

          is_stable    TYPE lvc_s_stbl,

          l_disvariant TYPE disvariant.

* 屏幕初始化

  IF alv_ctner IS INITIAL.

    CREATE OBJECT alv_ctner

      EXPORTING

        repid                       = sy-repid

        dynnr                       = '9001'

*       side                        = cl_gui_docking_container=>dock_at_right     ”ALV贴屏幕右边,从屏幕右边开始算宽度

        side                        = cl_gui_docking_container=>dock_at_left      "ALV贴屏幕左边,从左边算屏幕宽度,

        extension                   = 900                                         "屏幕宽度

*        style                       = cl_gui_control=>ws_child                    "可选参数,设置ALV是否可用手动拖动大小

      EXCEPTIONS

        cntl_error                  = 1

        cntl_system_error           = 2

        create_error                = 3

        lifetime_error              = 4

        lifetime_dynpro_dynpro_link = 5

        OTHERS                      = 6.

    IF sy-subrc <> 0.

      MESSAGE s001(00) WITH '屏幕初始化失败'.

      LEAVE LIST-PROCESSING.

    ENDIF.

*添加自定义工具条

    PERFORM frm_set_alv_toolbar.

*

    CREATE OBJECT g_alv_grid

      EXPORTING

        i_parent = alv_ctner.

*构建显示字段样式

    PERFORM frm_build_fieldcat.

    "Set/Reset Ready for Input Status

    CALL METHOD g_alv_grid->set_ready_for_input

      EXPORTING

        i_ready_for_input = 1.

* layout 设定样式

    CLEAR ls_layout.

    ls_layout-cwidth_opt = 'X'.     "优化列宽

    CLEAR l_disvariant.

    l_disvariant-report = sy-repid. "当前程序

* event  绑定事件

    CREATE OBJECT g_event.

    SET HANDLER g_event->toolbar             FOR g_alv_grid.

    SET HANDLER g_event->handle_data_changed FOR g_alv_grid.

*    SET HANDLER g_event->handle_f4           FOR g_alv_grid.

    SET HANDLER g_event->handle_cell_click   FOR g_alv_grid.

    SET HANDLER g_event->handle_user_command FOR g_alv_grid.

    SET HANDLER g_event->handle_double_click FOR g_alv_grid.

    SET HANDLER g_event->handle_onf1         FOR g_alv_grid.

   "以下两个方法达到的效果是:光标离开正在编辑的单元格时,就触发了handle_data_changed

   "如果不注释掉以下两个方法,则只有执行下一操作时才会触发handle_data_changed,比如点保存点删除

    CALL METHOD g_alv_grid->register_edit_event

      EXPORTING

        i_event_id = cl_gui_alv_grid=>mc_evt_modified.

    CALL METHOD g_alv_grid->register_edit_event

      EXPORTING

        i_event_id = cl_gui_alv_grid=>mc_evt_enter.

*   "显示内表数据

    CALL METHOD g_alv_grid->set_table_for_first_display

      EXPORTING

        is_layout            = ls_layout

        is_variant           = l_disvariant

        it_toolbar_excluding = gt_tools

      CHANGING

        it_fieldcatalog      = it_fieldcatalog[]

        it_outtab            = g_it_show[].

  ELSE.

    "冲销设置tcode 为 RELOAD,防止因为原来的tcode影响程序执行

    CALL METHOD cl_gui_cfw=>set_new_ok_code

      EXPORTING

        new_code = 'RELOAD'.

    "ALV 控制:刷新稳定性 按行刷新,可选参数

    CLEAR is_stable.

    is_stable-col = 'X'.

    "刷新

    CALL METHOD g_alv_grid->refresh_table_display

      EXPORTING

        is_stable      = is_stable

        i_soft_refresh = 'X'.

    "提交

    CALL METHOD cl_gui_cfw=>flush.

  ENDIF.

ENDFORM.                    " ZFORM_ALV_SHOW


上一篇:通过三条直线求出围成三角形的面积


下一篇:FM无线数据传输协议细节