OO实现ALV-SALV-实战攻略5-SALV的栏位属性及事件点击
ALV的栏位属性是SAP提供的ALV功能的比较重要的一个,详细的功能包括异常状态图标、图标、符号、复选框、按钮、热点、超链接、单元格类型、单元格颜色、某一栏位颜色、某一行颜色,栏位隐藏等等。
ALV的栏位功能主要是通过CL_SALV_COLUMN_TABLE和CL_SALV_COLUMNS_TABLE这两个类的实现的。
关于颜色等样式属性,可以单独设置,也可以通过单元格(cell)style去设置。详细可以接口IF_SALV_C_CELL_TYPE看到,如下:
具体实现方法如下:
在SALV最终输入内表中定义一个保存style的字段,字段类型为salv_t_int4_column,是个表类型,表类型中的结构(structure)定义如下,由一个列名和对应值组成。当不指定列名,只对value赋值,意味着整行的单元格都应用同一个style,如果指定列名,则对指定的与列名相同的应用style。设置好后要保存到内表中。
关于事件的实现,其中要能读到内表,所以要将内表放到能调取到的地方。具体各种事件参见类,代码以热点及双击事件作为代表。
REPORT ZSALV02. TABLES:VBAK. TYPE-POOLS: ICON, SLIS,SYM. CLASS SALV_DISPLAY DEFINITION. PUBLIC SECTION. METHODS:DATA_DIAPLAY. PRIVATE SECTION. TYPES:BEGIN OF TY_DATA, CHECKBOX TYPE CHAR1, T_COLOR TYPE LVC_T_SCOL, T_CELLTYPE TYPE SALV_T_INT4_COLUMN, VBELN TYPE VBAK-VBELN, ERDAT TYPE VBAK-ERDAT, ERZET TYPE VBAK-ERZET, ERNAM TYPE VBAK-ERNAM, AUART TYPE VBAK-AUART, NETWR TYPE VBAK-NETWR, END OF TY_DATA. DATA:LT_DATA TYPE STANDARD TABLE OF TY_DATA, WA_DATA TYPE TY_DATA. DATA:S_ALV TYPE REF TO CL_SALV_TABLE. DATA:LR_FUNCTIONS TYPE REF TO CL_SALV_FUNCTIONS, LR_EVENTS TYPE REF TO CL_SALV_EVENTS_TABLE, LR_COLUMNS TYPE REF TO CL_SALV_COLUMNS_TABLE, LR_COLUMN TYPE REF TO CL_SALV_COLUMN_TABLE, LR_FUNCTIONAL_SETTINGS TYPE REF TO CL_SALV_FUNCTIONAL_SETTINGS. DATA: G_COLOR TYPE LVC_S_COLO, "列颜色 LS_COLOR TYPE LVC_S_SCOL, LT_COLOR TYPE LVC_T_SCOL. "行颜色及单元格颜色 DATA: LT_CELLTYPE TYPE SALV_T_INT4_COLUMN. DATA: LS_CELLTYPE LIKE LINE OF LT_CELLTYPE. METHODS: COLUMN_SET CHANGING P_ALV TYPE REF TO CL_SALV_TABLE, ON_LINK_CLICK FOR EVENT LINK_CLICK OF CL_SALV_EVENTS_TABLE IMPORTING ROW"事件触发所在的行号 COLUMN,"事件触发所在的列名 HOTPOT_CLICK FOR EVENT LINK_CLICK OF CL_SALV_EVENTS_TABLE IMPORTING ROW "事件触发所在的行号 COLUMN,"事件触发所在的列名 DOUBLE_CLICK FOR EVENT DOUBLE_CLICK OF CL_SALV_EVENTS_TABLE IMPORTING ROW "事件触发所在的行号 COLUMN."事件触发所在的列名 ENDCLASS. CLASS SALV_DISPLAY IMPLEMENTATION. METHOD DATA_DIAPLAY. DEFINE ADD_COLOR. CLEAR LS_COLOR. LS_COLOR-FNAME = &1. LS_COLOR-COLOR-COL = &2. LS_COLOR-COLOR-INT = 1. LS_COLOR-COLOR-INV = 0. LS_COLOR-NOKEYCOL = 'X'. APPEND LS_COLOR TO LT_COLOR. END-OF-DEFINITION. SELECT * FROM VBAK INTO CORRESPONDING FIELDS OF TABLE LT_DATA UP TO 20 ROWS. WA_DATA-NETWR = 2222222222. APPEND WA_DATA TO LT_DATA. CLEAR:WA_DATA. LOOP AT LT_DATA INTO WA_DATA. IF WA_DATA-NETWR > 1000."单元格颜色条件 LS_COLOR-FNAME = 'NETWR'. LS_COLOR-COLOR-COL = '5'. LS_COLOR-COLOR-INT = '0'."加深 LS_COLOR-COLOR-INV = '0'."前景背景颜色反转 APPEND LS_COLOR TO LT_COLOR. WA_DATA-T_COLOR = LT_COLOR. MODIFY LT_DATA FROM WA_DATA. CLEAR:LS_COLOR,LT_COLOR."注意每次刷新内表及工作区 ENDIF. IF WA_DATA-AUART = 'ZCQ'. * ADD_COLOR 'VBELN' 4."行颜色可以使用宏对每个单元格定义 * ADD_COLOR 'ERDAT' 4. * ADD_COLOR 'ERZET' 4. * ADD_COLOR 'ERNAM' 4. * ADD_COLOR 'AUART' 4. * ADD_COLOR 'NETWR' 4. LS_COLOR-COLOR-COL = COL_NEGATIVE."行颜色也可以不指定列名就是整行 LS_COLOR-COLOR-INT = '0'."加深 LS_COLOR-COLOR-INV = '0'."前景背景颜色反转 APPEND LS_COLOR TO LT_COLOR. WA_DATA-T_COLOR = LT_COLOR. MODIFY LT_DATA FROM WA_DATA. CLEAR:LS_COLOR,LT_COLOR."注意每次刷新内表及工作区 ENDIF. * ZZK的订单类型单元格设定成按钮 IF WA_DATA-AUART = 'ZZK'. LS_CELLTYPE-COLUMNNAME = 'AUART'. LS_CELLTYPE-VALUE = IF_SALV_C_CELL_TYPE=>BUTTON. APPEND LS_CELLTYPE TO LT_CELLTYPE. WA_DATA-T_CELLTYPE = LT_CELLTYPE. MODIFY LT_DATA FROM WA_DATA. CLEAR:LS_COLOR,LT_COLOR,LS_CELLTYPE,LT_CELLTYPE."注意每次刷新内表及工作区 ENDIF. * 只有用户名为80181007 ERNAM列设定hotspot IF WA_DATA-ERNAM = '80181007'. LS_CELLTYPE-COLUMNNAME = 'ERNAM'. LS_CELLTYPE-VALUE = IF_SALV_C_CELL_TYPE=>HOTSPOT. APPEND LS_CELLTYPE TO LT_CELLTYPE. WA_DATA-T_CELLTYPE = LT_CELLTYPE. MODIFY LT_DATA FROM WA_DATA. CLEAR:WA_DATA,LS_COLOR,LT_COLOR,LS_CELLTYPE,LT_CELLTYPE."注意每次刷新内表及工作区 ENDIF. ENDLOOP. TRY . CL_SALV_TABLE=>FACTORY( IMPORTING R_SALV_TABLE = S_ALV CHANGING T_TABLE = LT_DATA ). CATCH CX_SALV_MSG. ENDTRY. "显示ALV的标准功能 LR_FUNCTIONS = S_ALV->GET_FUNCTIONS( ). LR_FUNCTIONS->SET_ALL( 'X' ). ME->COLUMN_SET( CHANGING P_ALV = S_ALV ). "=====事件注册 * DATA:HANDLER TYPE REF TO LCL_HANDLE_EVENTS. LR_EVENTS = S_ALV->GET_EVENT( )."注册事件至类 * CREATE OBJECT HANDLER. SET HANDLER ME->HOTPOT_CLICK FOR LR_EVENTS. SET HANDLER ME->DOUBLE_CLICK FOR LR_EVENTS. SET HANDLER ME->ON_LINK_CLICK FOR LR_EVENTS. S_ALV->DISPLAY( ). ENDMETHOD. METHOD COLUMN_SET. LR_COLUMNS = P_ALV->GET_COLUMNS( ). * LR_COLUMNS->SET_OPTIMIZE( 'X' )."设置全局列宽优化 LR_COLUMNS->SET_HEADERS_VISIBLE( 'X' ). "设置标题栏是否可见 LR_FUNCTIONAL_SETTINGS = S_ALV->GET_FUNCTIONAL_SETTINGS( ). TRY . LR_COLUMNS->SET_CELL_TYPE_COLUMN( 'T_CELLTYPE' ). LR_COLUMN ?= LR_COLUMNS->GET_COLUMN( 'VBELN' ). LR_COLUMN->SET_MEDIUM_TEXT( '销售订单号2')."修改列名 LR_COLUMN->SET_VISIBLE( 'X' )."可以设置列不可见 LR_COLUMN->SET_OPTIMIZED( 'X' )."单独设置列宽自适应 LR_COLUMN->SET_CELL_TYPE( IF_SALV_C_CELL_TYPE=>HOTSPOT ). LR_COLUMN ?= LR_COLUMNS->GET_COLUMN( 'ERDAT' ). G_COLOR-COL = '6'. G_COLOR-INT = '1'. G_COLOR-INV = '0'. LR_COLUMN->SET_COLOR( G_COLOR )."设置列颜色 LR_COLUMNS->SET_COLOR_COLUMN( 'T_COLOR' )."设置行颜色及单元格颜色 "设置复选框栏位 LR_COLUMN ?= LR_COLUMNS->GET_COLUMN( 'CHECKBOX' ). LR_COLUMN->SET_CELL_TYPE( IF_SALV_C_CELL_TYPE=>CHECKBOX_HOTSPOT ). LR_COLUMN->SET_LONG_TEXT( '选择' ). "设置热点 LR_COLUMN ?= LR_COLUMNS->GET_COLUMN( 'VBELN' ). LR_COLUMN->SET_CELL_TYPE( IF_SALV_C_CELL_TYPE=>HOTSPOT ). CATCH CX_SALV_NOT_FOUND. CATCH CX_SALV_DATA_ERROR. CATCH CX_SALV_EXISTING. ENDTRY. ENDMETHOD. METHOD HOTPOT_CLICK. * MESSAGE '你单击了我' TYPE 'I'. READ TABLE ME->LT_DATA INTO WA_DATA INDEX ROW."读当前行 CASE COLUMN."取列名 WHEN 'VBELN'. SET PARAMETER ID 'AUN' FIELD WA_DATA-VBELN. CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN. ENDCASE. ENDMETHOD. METHOD DOUBLE_CLICK. * MESSAGE '你双击了我' TYPE 'I'. READ TABLE ME->LT_DATA INTO WA_DATA INDEX ROW."读当前行 CASE COLUMN."取列名 WHEN 'ERNAM'. SET PARAMETER ID 'XUS' FIELD WA_DATA-ERNAM. CALL TRANSACTION 'ZSD053' AND SKIP FIRST SCREEN. ENDCASE. ENDMETHOD. METHOD ON_LINK_CLICK."复选框选中操作 READ TABLE ME->LT_DATA ASSIGNING FIELD-SYMBOL(<FS_DATA>) INDEX ROW."读当前行 IF <FS_DATA>-CHECKBOX EQ ''. <FS_DATA>-CHECKBOX = 'X'. ELSE. <FS_DATA>-CHECKBOX = ''. ENDIF. ME->S_ALV->REFRESH( ). ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA:LR_DISPLAY TYPE REF TO SALV_DISPLAY. CREATE OBJECT LR_DISPLAY. LR_DISPLAY->DATA_DIAPLAY( ).