1、获得光标所在alv的方法
*&---------------------------------------------------------------------*
*& 获得光标所在ALV的方法
*&---------------------------------------------------------------------*
**TOP**
DATA:
P_CONTROL TYPE REF TO CL_GUI_CONTROL,
P_CONTROL1 TYPE REF TO CL_GUI_CONTROL.
**I01**
* 获得光标所在ALV
IF P_CONTROL IS INITIAL.
CREATE OBJECT P_CONTROL.
ENDIF.
CALL METHOD P_CONTROL->GET_FOCUS
IMPORTING
CONTROL = P_CONTROL1
EXCEPTIONS
CNTL_ERROR = 1
CNTL_SYSTEM_ERROR = 2.
CASE P_CONTROL1.
* 上ALV的场合
WHEN G_GRID1.
……
* 下ALV的场合
WHEN G_GRID2.
……
WHEN OTHERS.
ENDCASE.
2、 获得、设置选择行方法
*&---------------------------------------------------------------------*
*& 获得,设置选择行方法
*&---------------------------------------------------------------------*
**TOP**
DATA:
G_ROWS TYPE LVC_T_ROW,
G_ID TYPE LVC_T_ROID.
**I01**
* 获得选择行
CALL METHOD G_GRID1->GET_SELECTED_ROWS
IMPORTING
ET_INDEX_ROWS = G_ROWS2
ET_ROW_NO = G_ID2.
**O01**
IF NOT G_ROWS IS INITIAL AND
NOT G_ID IS INITIAL.
* 设置选择列位置
CALL METHOD G_GRID1->SET_SELECTED_ROWS
EXPORTING
IT_INDEX_ROWS = G_ROWS
IT_ROW_NO = G_ID.
ENDIF.
3、 获得、设置当前alv光标位置
*&---------------------------------------------------------------------*
*& 获得,设置当前alv光标位置
*&---------------------------------------------------------------------*
**TOP**
DATA: WA_COLUM TYPE LVC_S_COL,
WA_ROW TYPE LVC_S_ROID.
**I01**
* 获得当前alv光标位置
CALL METHOD G_GRID1->GET_CURRENT_CELL
IMPORTING
ES_COL_ID = WA_COLUM
ES_ROW_NO = WA_ROW.
**O01**
* 设置当前alv光标位置
CALL METHOD G_GRID1->SET_CURRENT_CELL_VIA_ID
EXPORTING
IS_COLUMN_ID = WA_COLUM
IS_ROW_NO = WA_ROW.
4、 获得、设置左上角的行和列(固定alv行列滚动条)
*&---------------------------------------------------------------------*
*& 获得,设置左上角的行和列(固定ALV行列滚动条)
*&---------------------------------------------------------------------*
**TOP**
DATA: WA_INFO TYPE LVC_S_COL,
WA_ROW_ID TYPE LVC_S_ROID.
**I01**
* 获得左上角的行和列(固定ALV行列滚动条)
CALL METHOD G_GRID1->GET_SCROLL_INFO_VIA_ID
IMPORTING
ES_COL_INFO = WA_INFO
ES_ROW_NO = WA_ROW_ID.
**O01**
* 设置左上角的行和列(固定ALV行列滚动条)
CALL METHOD G_GRID1->SET_SCROLL_INFO_VIA_ID
EXPORTING
IS_COL_INFO = WA_INFO
IS_ROW_NO = WA_ROW_ID.
5、 屏幕值保存方法
*&---------------------------------------------------------------------*
*& 画面值保存方法
*&---------------------------------------------------------------------*
**I01**
DATA:
L_VALID(1) TYPE C VALUE 'X'.
* 保存画面值
CALL METHOD G_GRID1->CHECK_CHANGED_DATA
IMPORTING
E_VALID = L_VALID.
6、 单元格输入可否的控制方法
*&---------------------------------------------------------------------*
*& 单元格入力可否的制御方法
*&---------------------------------------------------------------------*
**O01**
* 单元格控制可入力的场合,INPUT设置‘1’.
CALL METHOD G_GRID1->SET_READY_FOR_INPUT
EXPORTING
I_READY_FOR_INPUT = 1.
* 单元格控制不可入力的场合,INPUT设置‘0’.
CALL METHOD G_GRID1->SET_READY_FOR_INPUT
EXPORTING
I_READY_FOR_INPUT = 0.
7、 双击事件触发的方法
*&---------------------------------------------------------------------*
*& 双击事件触发方法
*&---------------------------------------------------------------------*
*---TOP-----*
CLASS LCL_EVENT_HANDLER DEFINITION.
PUBLIC SECTION.
METHODS HANDLE_DOUBLE_CLICK
FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID
IMPORTING E_ROW "行 ID
E_COLUMN "列名
ES_ROW_NO. "数值行 ID
ENDCLASS. "LCL_EVENT_RECEIVER DEFINITION
DATA:
G_EVENT_HANDLER TYPE REF TO LCL_EVENT_HANDLER,
*---FO1-----*
CLASS LCL_EVENT_HANDLER IMPLEMENTATION.
* METHOD: DOUBLE CLICK
METHOD:
HANDLE_DOUBLE_CLICK.
CLEAR: FLG_CHANGED.
* 双击事件的处理
READ TABLE IT_DATA INDEX E_ROW INTO WA_DATA.
ENDMETHOD. "HANDLE_DOUBLE_CLICK
ENDCLASS. "LCL_EVENT_HANDLER IMPLEMENTATION
*---OO1-----*
* 添加ALV事件
CREATE OBJECT G_EVENT_HANDLER.
* 添加ALV句柄
SET HANDLER G_EVENT_HANDLER->HANDLE_DOUBLE_CLICK FOR G_GRID1.
CALL METHOD G_GRID1->REGISTER_EDIT_EVENT "注册更新事件
EXPORTING
I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_MODIFIED.
8、 alv单元格值改变事件触发的方法
*&---------------------------------------------------------------------*
*& ALV值改变触发检查
*&---------------------------------------------------------------------*
*---TOP-----*
CLASS LCL_ALV_RECEIVER DEFINITION.
PUBLIC SECTION.
METHODS HANDLE_DATA_CHANGED
FOR EVENT DATA_CHANGED OF CL_GUI_ALV_GRID
IMPORTING ER_DATA_CHANGED
E_ONF4
E_ONF4_BEFORE
E_ONF4_AFTER.
ENDCLASS. "LCL_ALV_RECEIVER DEFINITION
DATA:
G_EVENT_HANDLER TYPE REF TO LCL_ALV_RECEIVER,
*---FO1-----*
CLASS LCL_ALV_RECEIVER IMPLEMENTATION.
* METHOD: HANDLE_DATA_CHANGED
METHOD HANDLE_DATA_CHANGED.
DATA: IT_MOD_DATA TYPE LVC_T_MODI,
WA_MOD_DATA TYPE LVC_S_MODI.
IT_MOD_DATA = ER_DATA_CHANGED->MT_MOD_CELLS.
READ TABLE IT_MOD_DATA INDEX 1 INTO WA_MOD_DATA.
*-----------例如 STAND---------------------*
* IF WA_MOD_DATA-FIELDNAME = 'ZWGT' AND
* WA_MOD_DATA-VALUE < '0'.
* MESSAGE ID '00' TYPE 'I' NUMBER 001
* WITH WA_MOD_DATA-VALUE '重量不能为负值!'.
* LEAVE SCREEN.
* ENDIF.
*-----------例如 END-----------------------*
ENDMETHOD. "HANDLE_DOUBLE_CLICK
ENDCLASS. "LCL_ALV_RECEIVER IMPLEMENTATION
*---OO1-----*
* 添加ALV事件
CREATE OBJECT G_EVENT_HANDLER.
* 添加ALV句柄
SET HANDLER G_EVENT_HANDLER->HANDLE_DATA_CHANGED FOR G_GRID1.
CALL METHOD G_GRID1->REGISTER_EDIT_EVENT "注册更新事件
EXPORTING
I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_MODIFIED.
9、ALV里下拉列表的实现
*&---------------------------------------------------------------------*
*& ALV里下拉列表的实现
*&---------------------------------------------------------------------*
*---TOP-----*
DATA:
IT_DROPDOWN TYPE LVC_T_DROP,
WA_DROPDOWN TYPE LVC_S_DROP,
*---FO1-----*
* ALV 的下拉列表的编辑
PERFORM FRM_DROPDOWN_EDIT.
*&---------------------------------------------------------------------*
*& Form FRM_DROPDOWN_EDIT
*&---------------------------------------------------------------------*
* ALV 的下拉列表的编辑
*----------------------------------------------------------------------*
FORM FRM_DROPDOWN_EDIT.
CLEAR: WA_DROPDOWN.
REFRESH: IT_DROPDOWN.
*可以将写死的赋值,改成 loop 循环数据的赋值
* 订货地区下拉列表内容编辑(例如)
WA_DROPDOWN-HANDLE = '1'. "对应LVC_S_FCAT里设'1'的列
WA_DROPDOWN-VALUE = 'D'. "显示的值
APPEND WA_DROPDOWN TO IT_DROPDOWN.
CLEAR:WA_DROPDOWN.
WA_DROPDOWN-HANDLE = '1'.
WA_DROPDOWN-VALUE = 'A'.
APPEND WA_DROPDOWN TO IT_DROPDOWN.
CLEAR:WA_DROPDOWN.
WA_DROPDOWN-HANDLE = '1'.
WA_DROPDOWN-VALUE = 'U'.
APPEND WA_DROPDOWN TO IT_DROPDOWN.
CLEAR:WA_DROPDOWN.
ENDFORM. " FRM_DROPDOWN_EDIT
*---OO1-----*
CALL METHOD G_GRID1->SET_DROP_DOWN_TABLE "FIRST_DISPLAY调用前
EXPORTING
IT_DROP_DOWN = IT_DROPDOWN.
* ALV项目表示用表的设置
MOVE 'C' TO WA_CAT-JUST.
MOVE 'X' TO WA_CAT-EDIT.
MOVE '1' TO WA_CAT-DRDN_HNDL. "ALV 控制: 自然数,设定下拉列表时付'1','2'......
MOVE 'ZPO_AREA' TO WA_CAT-FIELDNAME.
MOVE '订货地区' TO WA_CAT-COLTEXT.
APPEND WA_CAT TO IT_CAT.
CLEAR WA_CAT.
10 OO ALV 设置保存布局