首页 > 解决方案 > 动态着色 ALV 网格的单元格 (CL_SALV_TABLE)

问题描述

我需要动态地为 ALV Grid 的单元格着色。我用CL_SALV_TABLE.

例如,在我的情况下,我根据参数从数据表中选择数据。我想在 ALV 网格中显示选定的结果。-> 到目前为止,一切都按我的需要工作。

现在,我想将网格中具有初始值或零值的单元格着色为红色。

这可能吗?如果是,该怎么做?

标签: abapalv

解决方案


将另一列添加type lvc_t_scol到最终输出表中,填充它并告诉 ALV 将其用作颜色列。

这是一篇包含可能颜色的博客文章。

* declaration of output table with color column
TYPES: BEGIN OF lty_output,
         carrid   TYPE  scarr-carrid,
         carrname TYPE  scarr-carrname,
         color    TYPE  lvc_t_scol,
       END OF lty_output.

DATA gt_output TYPE STANDARD TABLE OF lty_output.

* filling color column of output table (e.g. based on condition during a LOOP)
DATA: ls_color TYPE lvc_s_scol,
      lt_color TYPE lvc_t_scol.

gt_output = VALUE #(
    ( carrid = 'AA' carrname = 'American Airlines' color = VALUE #(
        ( fname = 'CARRNAME' color = VALUE #( col = 6 int = 0 inv = 0 ) ) ) )
    ( carrid = 'AF' carrname = 'Air France' color = VALUE #(
        ( fname = 'CARRID'   color = VALUE #( col = 7 int = 0 inv = 0 ) )
        ( fname = 'CARRNAME' color = VALUE #( col = 5 int = 0 inv = 0 ) ) ) )
    ( carrid = 'LH' carrname = 'Lufthansa' color = VALUE #(
        ( fname = ''         color = VALUE #( col = 3 int = 0 inv = 0 ) ) ) ) ).

cl_salv_table=>factory(
    IMPORTING
        r_salv_table = DATA(go_alv)
    CHANGING
        t_table = gt_output ).

* setting color column
go_alv->get_columns( )->set_color_column( 'COLOR' ).

go_alv->display( ).

推荐阅读