ABAP--如何在REUSE_ALV_GRID_DISPLAY标识不同行用不同的颜色

ABAP教程:使用REUSE_ALV_GRID_DISPLAY为不同行设置颜色
本文介绍了如何在ABAP中利用REUSE_ALV_GRID_DISPLAY函数,根据条件为表格的不同行设置不同的颜色。首先在内表添加颜色字段,然后根据条件设置颜色值。接着,更新颜色控制内表slis_layout_alv以包含颜色字段。最后,调用REUSE_ALV_GRID_DISPLAY函数显示带颜色标记的数据。同时,文章还展示了如何自定义表头,展示关键统计信息。

1 在内表中加入颜色字段
.............................
 color(4) type c,
................................

2 在内表添加记录是通过条件设置颜色字段值
wa_customerinfo-color = 'C110'.

3 设置颜色控制内表 slis_layout_alv 的颜色字段为内表增加的颜色字段
    data  g_slis_layo type slis_layout_alv.   "定义Grid输出颜色控制内表
    g_slis_layo-info_fieldname = 'COLOR'.

4 输出记录
    call function 'REUSE_ALV_GRID_DISPLAY'
      exporting
        i_callback_program = g_repid
        is_layout          = g_slis_layo
        it_fieldcat        = g_field[]
        it_sort            = g_sort[]
        it_events          = event_top_of_page[]
      tables
        t_outtab           = i_customerinfo.

以下为显示表头的代码
data  event_top_of_page   type slis_alv_event  occurs 0 with header line. 
perform event_top_of_page tables event_top_of_page.

form event_top_of_page tables rt_event type slis_t_event.
  refresh rt_event.
  clear rt_event.
  rt_event-name = slis_ev_top_of_page.
  rt_event-form = 'WRITE_TOP_OF_PAGE'.
  append rt_event.
endform.                    "event_top_of_page

form write_top_of_page.
  data: hline type slis_listheader,
       text(60) type c,
       text10(10) type c,
       text5(5) type c,
       percent type p decimals 2.

  refresh head.
  clear head.
  clear: hline, text.
  hline-typ  = 'S'.
  write: '时间:', g_curdate  to text.
  hline-info = text.
  append hline to head.
  write: '客户状态:绿色-老呆死客户,红色-新呆死客户,兰色-超信用额度客户'
    to text.
  hline-info = text.
  write: g_countkunnr  to text5.
  append hline to head.
  concatenate '往来单位:' text5 '个.' into text.
  hline-info = text.
  append hline to head.
  write: g_oldbad_count  to text5.
  concatenate '老呆死帐单位:' text5 '个,占' into text.
  percent = g_oldbad_count * 100 / g_countkunnr.
  write: percent  to text5.
  concatenate text5 '%' into text+23.
  write: g_newbad_count  to text5.
  concatenate '新呆死单位:' text5 '个,占' into text+30.
  percent = g_newbad_count * 100 / g_countkunnr.
  write: percent  to text5.
  concatenate text5 '%' into text+53.
*  CONDENSE text.
  hline-info = text.
  append hline to head.
  clear text.
  percent = g_ccdebit_count / 10000.
  write: percent  to text10.
  concatenate '应收帐款:' text10 '万元' into text.
  percent = g_webtr_count / 10000.
  write: percent  to text10.
  concatenate '认可呆死帐:' text10  '万元.' into  text+30.
  hline-info = text.
  append hline to head.

  percent = g_oldbad_money / 10000.
  write: percent  to text10.
  concatenate '老呆死帐款:' text10 '万元,' into text.
  percent = g_oldbad_money * 100 / g_ccdebit_count.
  write: percent  to text5.
  concatenate '占' text5 '%' into text+30.
  hline-info = text.
  append hline to head.

  percent = g_newbad_money / 10000.
  write: percent  to text10.
  concatenate '新呆死帐款:' text10 '万元,' into text.
  percent = g_newbad_money  * 100 / g_ccdebit_count.
  write: percent  to text5.
  concatenate '占' text5 '%' into text+30.
  hline-info = text.
  append hline to head.

  write: g_over_count  to text5.
  concatenate '超安全额度单位:' text5 '个,' into text.
  percent = g_over_count * 100 / g_countkunnr.
  write: percent  to text5.
  concatenate '占' text5 '%' into text+24.
  percent = g_over_money / 10000.
  write: percent  to text10.
  concatenate  '超安全额度:' text10 '万元.' into text+30.
  hline-info = text.
  append hline to head.
  call function 'REUSE_ALV_COMMENTARY_WRITE'
    exporting
      it_list_commentary = head.
endform.                    "write_top_of_page

提供的引用内容未提及使用ABAPREUSE_ALV_GRID_DISPLAY_LVC实现ALV双层标题的方法。通常在ABAP里,要使用REUSE_ALV_GRID_DISPLAY_LVC实现ALV双层标题,可按以下步骤操作: ### 定义字段目录 要定义字段目录,并且为每个字段设定抬头信息。对于双层标题,需要使用 `LVC_S_FCAT` 结构里的 `GROUP1` 和 `GROUP2` 字段来定义不同层级的标题。 ### 设定布局 要对ALV的布局进行设定,确保双层标题能够正确显示。 ### 调用REUSE_ALV_GRID_DISPLAY_LVC 把定义好的字段目录和布局当作参数传递给 `REUSE_ALV_GRID_DISPLAY_LVC` 函数。 以下是示例代码: ```abap REPORT z_alv_double_header. TABLES: mara. DATA: gt_fieldcat TYPE lvc_t_fcat, gs_layout TYPE lvc_s_layo, gt_data TYPE STANDARD TABLE OF mara. START - OF - SELECTION. " 填充数据 SELECT * FROM mara INTO CORRESPONDING FIELDS OF TABLE gt_data. " 定义字段目录 PERFORM fill_fieldcat. " 设置布局 gs_layout-zebra = 'X'. " 斑马线效果 " 调用REUSE_ALV_GRID_DISPLAY_LVC CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC' EXPORTING i_callback_program = sy - repid is_layout_lvc = gs_layout it_fieldcat_lvc = gt_fieldcat TABLES t_outtab = gt_data EXCEPTIONS program_error = 1 OTHERS = 2. IF sy - subrc <> 0. MESSAGE ID sy - msgid TYPE sy - msgty NUMBER sy - msgno WITH sy - msgv1 sy - msgv2 sy - msgv3 sy - msgv4. ENDIF. FORM fill_fieldcat. DATA: wa_fieldcat TYPE lvc_s_fcat. " 第一层标题 wa_fieldcat - fieldname = 'MATNR'. wa_fieldcat - coltext = '物料编号'. wa_fieldcat - group1 = '基本信息'. APPEND wa_fieldcat TO gt_fieldcat. CLEAR wa_fieldcat. wa_fieldcat - fieldname = 'MAKTX'. wa_fieldcat - coltext = '物料描述'. wa_fieldcat - group1 = '基本信息'. APPEND wa_fieldcat TO gt_fieldcat. CLEAR wa_fieldcat. " 第二层标题 wa_fieldcat - fieldname = 'BRGEW'. wa_fieldcat - coltext = '毛重'. wa_fieldcat - group1 = '重量信息'. wa_fieldcat - group2 = '详细重量'. APPEND wa_fieldcat TO gt_fieldcat. CLEAR wa_fieldcat. wa_fieldcat - fieldname = 'NTGEW'. wa_fieldcat - coltext = '净重'. wa_fieldcat - group1 = '重量信息'. wa_fieldcat - group2 = '详细重量'. APPEND wa_fieldcat TO gt_fieldcat. CLEAR wa_fieldcat. ENDFORM. ``` 在这个示例中,借助 `fill_fieldcat` 子例程定义了字段目录,利用 `GROUP1` 和 `GROUP2` 字段实现了双层标题。之后把定义好的字段目录和布局传递给 `REUSE_ALV_GRID_DISPLAY_LVC` 函数。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值