ABAP - SALV教程11 红黄绿灯

  • SALV通过某列设置成异常列,SALV就会根据某列的值自动映射成红黄绿灯
  • 注意事项
  1. 该列的类型为CHAR1,即是结构的字段类型为CHAR1
  2. 该字段的值赋值为 (space,1,2,3) space:灰灯、1:红灯、2:黄灯、3:绿灯 
  •  案例代码
    CLASS lcl_model DEFINITION.
      PUBLIC SECTION.
        TYPES:BEGIN OF TY_dATA,
                status TYPE char1,
                vbeln  TYPE vbak-vbeln,
                erdat  TYPE erdat,
                erzet  TYPE erzet,
                ernam  TYPE ernam,
                auart  TYPE auart.
        TYPES:END  OF ty_data.
    
        DATA mt_data TYPE TABLE OF ty_data.
    
        DATA mo_salv TYPE REF TO cl_salv_table.
    
        "获取数据的方法
        METHODS get_data.
        "展示SALV的方法
        METHODS alv_show.
    
      PRIVATE SECTION.
        METHODS:
          set_status CHANGING co_alv TYPE REF TO cl_salv_table.
        METHODS:
          set_icon CHANGING co_alv TYPE REF TO cl_salv_table.
    
    ENDCLASS.
    
    CLASS lcl_model IMPLEMENTATION.
      METHOD get_data.
        SELECT vbeln erdat erzet ernam auart
          INTO CORRESPONDING FIELDS OF TABLE mt_data
          FROM vbak
          UP TO 20 ROWS.
    
        LOOP AT mt_data ASSIGNING FIELD-SYMBOL(<fs_data>).
          IF sy-tabix MOD 3 = 1.
            <fs_data>-status = '1'.
          ELSEIF sy-tabix MOD 3 = 2.
            <fs_data>-status = '2'.
          ELSEIF sy-tabix MOD 3 = 0.
            <fs_data>-status = '3'.
          ENDIF.
    
          AT LAST.
            <fs_data>-status = ''.
          ENDAT.
        ENDLOOP.
      ENDMETHOD.
    
      METHOD alv_show.
        DATA: lx_msg TYPE REF TO cx_salv_msg.
        TRY.
            cl_salv_table=>factory(
              IMPORTING
                r_salv_table = mo_salv
              CHANGING
                t_table      = mt_data ).
          CATCH cx_salv_msg INTO lx_msg.
        ENDTRY.
        me->set_status( CHANGING co_alv = mo_salv ).
        me->set_icon( CHANGING co_alv = mo_salv ).
        mo_salv->display( ).
      ENDMETHOD.
    
      METHOD set_status.
        DATA(lo_functions) = co_alv->get_functions( ).
        "cl_salv_functions_list类型的实例对象的添加状态方法
        lo_functions->set_all( abap_true ).
        "lo_functions->set_default( abap_true ).
      ENDMETHOD.
    
      METHOD set_icon.
        " 声明接收全体列对象的变量
        DATA: lo_columns TYPE REF TO cl_salv_columns.
        DATA: lo_columns_table TYPE REF TO cl_salv_columns_table.
        " 声明接收单个列对象的变量
        DATA: lo_column TYPE REF TO cl_salv_column.
        DATA: lo_column_table  TYPE REF TO cl_salv_column_table.
    
        " 声明型号灯映射变量
        DATA: ls_ddic TYPE salv_s_ddic_reference.
    
        " 获取全部列的对象
        lo_columns = co_alv->get_columns( ).
    
        " 设置自动优化列宽度
        lo_columns->set_optimize( 'X' ).
    
        "红黄绿灯的固定传值
        ls_ddic-field = 'FIELD'.
        ls_ddic-table = 'SALV_S_DDIC_REFERENCE'.
    
        TRY.
            " 获取全部列的对象
            lo_columns_table = co_alv->get_columns( ).
    
            " 修改该列为异常列
            lo_columns_table->set_exception_column( 'STATUS' ).
    
            lo_column_table ?= lo_columns_table->get_column('STATUS').
    
            lo_column_table->set_ddic_reference( ls_ddic ).
    
            "修改该列描述
            lo_column_table->set_short_text( '状态' ).
            lo_column_table->set_medium_text( '状态' ).
            lo_column_table->set_long_text( '状态' ).
    
          CATCH cx_salv_not_found.
        ENDTRY.
    
        DATA: lo_functional_settings TYPE REF TO cl_salv_functional_settings.
        DATA: lo_tooltips TYPE REF TO cl_salv_tooltips.
        lo_functional_settings = co_alv->get_functional_settings( ).
        lo_tooltips = lo_functional_settings->get_tooltips( ).
    
        "单元格显示类型:异常灯、图标、符号。即这里针对的是类型为 异常列 所有的单元格
        "这里做鼠标悬停在字段是的简要说明
        lo_tooltips->add_tooltip(
          type = cl_salv_tooltip=>c_type_exception
          value = space
          tooltip = 'Undefined').
    
        lo_tooltips->add_tooltip(
          type = cl_salv_tooltip=>c_type_exception
          value = '1'
          tooltip = '未拣配').
    
        lo_tooltips->add_tooltip(
          type = cl_salv_tooltip=>c_type_exception
          value = '2'
          tooltip = '未完全拣配').
    
        lo_tooltips->add_tooltip(
          type = cl_salv_tooltip=>c_type_exception
          value = '3'
          tooltip = '已完全拣配').
      ENDMETHOD.
    
    ENDCLASS.
    
    START-OF-SELECTION.
      DATA(lo_model) = NEW lcl_model( ).
      lo_model->get_data( ).
      lo_model->alv_show( ).

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值