- SALV只有设置列颜色的处理的方法
- 但是SALV的行颜色和列颜色可以通过输出内表控制,具体实现可以参考:http://t.csdnimg.cn/OiBOX
- SALV设置列颜色:获取全体列对象的引用调用方法set_color()就能实现
METHODS:set_colors CHANGING co_alv TYPE REF TO cl_salv_table.METHOD set_colors.DATA: lo_cols TYPE REF TO cl_salv_columns_table,lo_col TYPE REF TO cl_salv_column_table.DATA: ls_color TYPE lvc_s_colo.INCLUDE <color>."获取所有的列对象lo_cols = co_alv->get_columns( ).TRY."从所有的类对象中获取ERZET列lo_col ?= lo_cols->get_column( 'ERZET ').ls_color-col = col_total. "色号"调用set_color(),该方法的传入参数类型为lvc_s_cololo_col->set_color( ls_color ).CATCH cx_salv_not_found.ENDTRY.ENDMETHOD.
显示效果如下图所示:
- 完整代码
CLASS lcl_model DEFINITION.PUBLIC SECTION.TYPES:BEGIN OF TY_dATA,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_colors CHANGING co_alv TYPE REF TO cl_salv_table.ENDCLASS.CLASS lcl_model IMPLEMENTATION.METHOD get_data.SELECT vbeln erdat erzet ernam auartINTO TABLE mt_dataFROM vbakUP TO 20 ROWS.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_colors( 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_colors.DATA: lo_cols TYPE REF TO cl_salv_columns_table,lo_col TYPE REF TO cl_salv_column_table.DATA: ls_color TYPE lvc_s_colo.INCLUDE <color>."获取所有的列对象lo_cols = co_alv->get_columns( ).TRY.lo_col ?= lo_cols->get_column( 'ERZET ').ls_color-col = col_total. "色号lo_col->set_color( ls_color ).CATCH cx_salv_not_found.ENDTRY.ENDMETHOD.ENDCLASS.START-OF-SELECTION.DATA(lo_model) = NEW lcl_model( ).lo_model->get_data( ).lo_model->alv_show( ).