sap 報表 ZALV_XZH(新增報表空白行且雙擊欄位生成一行)demo

小张同学¥1發表於2024-10-06

*&---------------------------------------------------------------------*
*& Report ZALV_XZH
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZALV_XZH.

TYPES:BEGIN OF ty_ekko,
ebeln TYPE ekko-ebeln,
verkf TYPE ekko-verkf,
sel TYPE c, "用來標識行選擇的欄位
F_STYLE TYPE LVC_T_STYL,
END OF ty_ekko.
DATA:it_ekko TYPE STANDARD TABLE OF ty_ekko,
wa_ekko TYPE ty_ekko.

DATA:lt_fieldcat TYPE lvc_t_fcat,
wa_fieldcat LIKE LINE OF lt_fieldcat,
wa_layout TYPE lvc_s_layo.
CONSTANTS: gco_callback_user_command TYPE slis_formname VALUE 'FRM_USER_COMMAND',
gco_callback_status TYPE slis_formname VALUE 'FRM_USER_STATUS'.

START-OF-SELECTION.

SELECT ebeln
verkf
INTO CORRESPONDING FIELDS OF TABLE it_ekko
FROM ekko
UP TO 10 ROWS.

APPEND INITIAL LINE TO it_ekko.

PERFORM frm_set_catalog.

PERFORM frm_loop_itdata.
* wa_layout-zebra = 'X'.
* wa_layout-box_fname = 'SEL'. "指定行選擇欄位
wa_layout-cwidth_opt = 'X'.
wa_layout-stylefname = 'F_STYLE'.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
is_layout_lvc = wa_layout
it_fieldcat_lvc = lt_fieldcat
i_callback_pf_status_set = gco_callback_status
i_callback_user_command = gco_callback_user_command
TABLES
t_outtab = it_ekko[]
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc = 0.

ENDIF.

*&---------------------------------------------------------------------*
*& Form FRM_SET_PF_STATUS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_user_status USING i_it_extab TYPE slis_t_extab.
* SET PF-STATUS '*******' .
ENDFORM. " FRM_SET_PF_STATUS

FORM frm_user_command USING i_ucomm TYPE sy-ucomm
i_wa_selfield TYPE slis_selfield.
CASE i_ucomm.
WHEN '&IC1'. "Double click
* if click on PO field, jump to me23n
* IF i_wa_selfield-fieldname = 'EBELN'.
* SET PARAMETER ID 'BES' FIELD i_wa_selfield-value.
* CALL TRANSACTION 'ME23N' AND SKIP FIRST SCREEN.
* ENDIF.
PERFORM frm_loop_itdata .
APPEND INITIAL LINE TO it_ekko.
i_wa_selfield-refresh = 'X'.
WHEN OTHERS.

ENDCASE.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FRM_SET_CATALOG
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM frm_set_catalog .

DEFINE mac_fieldcat.
clear wa_fieldcat.
wa_fieldcat-fieldname = &1.
wa_fieldcat-reptext = &2.
wa_fieldcat-tabname = 'IT_EKKO'.
wa_fieldcat-outputlen = &3. "設定輸出列寬
wa_fieldcat-edit = &4.
APPEND wa_fieldcat TO lt_fieldcat.
END-OF-DEFINITION.

clear wa_fieldcat.
wa_fieldcat-fieldname = 'SEL'.
wa_fieldcat-reptext = '選擇框'.
wa_fieldcat-tabname = 'IT_EKKO'.
wa_fieldcat-edit = 'X'.
wa_fieldcat-checkbox = 'X'.
wa_fieldcat-outputlen = 5. "設定輸出列寬
APPEND wa_fieldcat TO lt_fieldcat.

mac_fieldcat 'EBELN' '採購單號' 10 'X'.
mac_fieldcat 'VERKF' '銷售員' 10 'X'.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_loop_itdata
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM frm_loop_itdata .
DATA stylelin TYPE lvc_s_styl.

LOOP AT it_ekko INTO wa_ekko.
IF wa_ekko-ebeln IS NOT INITIAL.
CLEAR :wa_ekko-f_style[].
CLEAR stylelin.
stylelin-fieldname = 'EBELN'. " 需要編輯的列名
stylelin-style = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED. " 設定為不可編輯狀態
APPEND stylelin TO wa_ekko-f_style.

CLEAR STYLELIN.
stylelin-fieldname = 'VERKF'.
stylelin-style = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.
APPEND stylelin TO wa_ekko-f_style.

MODIFY it_ekko FROM wa_ekko.
ENDIF.
ENDLOOP.
ENDFORM.

相關文章