- ALV要求字段的值为图标的需求并不多见,一般都用于红黄绿灯,来表示单据的执行状态,添加图标的方式也可以实现红黄绿灯的功能,也可以参考SALV实现红黄绿灯这篇文章:http://t.csdnimg.cn/Dzx7x
- 效果图
- SAVL列设置为图标图标显示实现步骤:
- 输出内表增加一个char30的字段类型,然后赋值。图标的值:SE38执行showicon程序
TYPES:BEGIN OF TY_dATA,icon TYPE char30,TYPES:END OF ty_data.DATA mt_data TYPE TABLE OF ty_data.INCLUDE: <icon>.LOOP AT mt_data ASSIGNING FIELD-SYMBOL(<fs_data>).<fs_data>-icon = icon_checked.ENDLOOP.
- 将该字段设置为图标显示列
DATA: lo_functional_settings TYPE REF TO cl_salv_functional_settings.DATA: lo_tooltips TYPE REF TO cl_salv_tooltips,lv_value TYPE lvc_value.DATA: lo_columns TYPE REF TO cl_salv_columns.DATA: lo_column TYPE REF TO cl_salv_column_table.INCLUDE: <icon>.lo_columns = co_alv->get_columns( ).TRY.lo_column ?= lo_columns->get_column( 'ICON' )." 将列设置为显示ICONlo_column->set_icon( if_salv_c_bool_sap=>true )." 设置列标签 labello_column->set_long_text( '图标' )." 图标对齐方式lo_column->set_alignment( if_salv_c_alignment=>centered )." 字段长度lo_column->set_output_length( 6 ).CATCH cx_salv_not_found.ENDTRY."这里做鼠标悬停时的提示信息lo_functional_settings = co_alv->get_functional_settings( ).lo_tooltips = lo_functional_settings->get_tooltips( ).TRY."当图标代码值为icon_checked,提示 Everything is Processedlv_value = icon_checked.lo_tooltips->add_tooltip(type = cl_salv_tooltip=>c_type_iconvalue = lv_valuetooltip = 'Everything is Processed' ).CATCH cx_salv_existing.ENDTRY.ENDMETHOD.
- 完整代码
CLASS lcl_model DEFINITION.PUBLIC SECTION.TYPES:BEGIN OF TY_dATA,icon TYPE char30,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_icon CHANGING co_alv TYPE REF TO cl_salv_table.METHODS:set_status CHANGING co_alv TYPE REF TO cl_salv_table.ENDCLASS.CLASS lcl_model IMPLEMENTATION.METHOD get_data.SELECT vbeln erdat erzet ernam auartINTO CORRESPONDING FIELDS OF TABLE mt_dataFROM vbakUP TO 20 ROWS.INCLUDE: <icon>.LOOP AT mt_data ASSIGNING FIELD-SYMBOL(<fs_data>).<fs_data>-icon = icon_checked.ENDLOOP.ENDMETHOD.METHOD alv_show.DATA: lx_msg TYPE REF TO cx_salv_msg.TRY.cl_salv_table=>factory(IMPORTINGr_salv_table = mo_salvCHANGINGt_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_functional_settings TYPE REF TO cl_salv_functional_settings.DATA: lo_tooltips TYPE REF TO cl_salv_tooltips,lv_value TYPE lvc_value.DATA: lo_columns TYPE REF TO cl_salv_columns.DATA: lo_column TYPE REF TO cl_salv_column_table.INCLUDE: <icon>.lo_columns = co_alv->get_columns( ).TRY.lo_column ?= lo_columns->get_column( 'ICON' )." 将列设置为显示ICONlo_column->set_icon( if_salv_c_bool_sap=>true )." 设置列标签 labello_column->set_long_text( '图标' )." 图标对齐方式lo_column->set_alignment( if_salv_c_alignment=>centered )." 字段长度lo_column->set_output_length( 6 ).CATCH cx_salv_not_found.ENDTRY."这里做鼠标悬停时的提示信息lo_functional_settings = co_alv->get_functional_settings( ).lo_tooltips = lo_functional_settings->get_tooltips( ).TRY."当图标代码值为icon_checked,提示 Everything is Processedlv_value = icon_checked.lo_tooltips->add_tooltip(type = cl_salv_tooltip=>c_type_iconvalue = lv_valuetooltip = 'Everything is Processed' ).CATCH cx_salv_existing.ENDTRY.ENDMETHOD.ENDCLASS.START-OF-SELECTION.DATA(lo_model) = NEW lcl_model( ).lo_model->get_data( ).lo_model->alv_show( ).