子曰花开2015

导航

ALV编辑单元格

 

示例演示:

  1. SE11表:

 

  1. 源代码:

 

*&---------------------------------------------------------------------*
*& Report ZXHN_EDIT_ALV
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZXHN_EDIT_ALV.

TYPE-POOLS: slis.

TABLES: ZXHN_EDIT_ALV .

TYPESBEGIN OF ty_out,
         zid     TYPE ZXHN_EDIT_ALV-zid,
         zname   TYPE ZXHN_EDIT_ALV-zname,
         zage    TYPE ZXHN_EDIT_ALV-zage,
         selec   TYPE char1,
         celltab TYPE lvc_t_styl,
       END OF ty_out .

DATA: gt_out TYPE TABLE OF ty_out,
      gs_out TYPE ty_out.

DATA:t_fieldcat TYPE lvc_t_fcat,
     w_fieldcat TYPE LINE OF lvc_t_fcat,
     t_layout   TYPE lvc_s_layo.

DATA:rt_extab TYPE slis_t_extab.

DATA g_grid TYPE REF TO cl_gui_alv_grid.
DATA stbl TYPE lvc_s_stbl.

DATA ls_celltab TYPE LVC_S_STYL.
DATA lt_celltab TYPE lvc_t_styl.

SELECTION-SCREEN:BEGIN OF BLOCK a1 WITH FRAME TITLE text-001.
SELECT-OPTIONS:s_zid   FOR ZXHN_EDIT_ALV-zid,
               s_zname FOR ZXHN_EDIT_ALV-zname,
               s_zage  FOR ZXHN_EDIT_ALV-zage.
SELECTION-SCREEN:END OF BLOCK a1.

START-OF-SELECTION.
*-----------------------获取数据
  PERFORM frm_get_data.
  PERFORM frm_handle_data.

  PERFORM frm_alv_show.

END-OF-SELECTION.
*&---------------------------------------------------------------------*
*&      Form  FRM_GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_GET_DATA .

  SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_out
           FROM ZXHN_EDIT_ALV
           WHERE zid   IN s_zid
             AND zname IN s_zname
             AND zage  IN s_zage .

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

  PERFORM frm_init_layout.
  PERFORM frm_set_fieldset.
  PERFORM frm_output_alv.
  PERFORM f_status USING rt_extab.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_INIT_LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_INIT_LAYOUT .
  t_layout-cwidth_opt 'X'.
  t_layout-zebra 'X'.
  t_layout-box_fname 'SELEC'.
  t_layout-stylefname 'CELLTAB' .

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

  PERFORM frm_init_fieldcat USING 'ZID' '学号' ''  10.
  PERFORM frm_init_fieldcat USING 'ZNAME' '名字' ''  30.
  PERFORM frm_init_fieldcat USING 'ZAGE' '年龄' 'X'  10.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_INIT_FIELDCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_0153   text
*      -->P_0154   text
*      -->P_10     text
*----------------------------------------------------------------------*
FORM FRM_INIT_FIELDCAT  USING    fieldcatname LIKE w_fieldcat-fieldname
                                 seltext LIKE w_fieldcat-scrtext_l
                                 edit LIKE w_fieldcat-edit
                                 len LIKE w_fieldcat-outputlen.

  CLEAR w_fieldcat.
  w_fieldcat-fieldname = fieldcatname .
  w_fieldcat-scrtext_l = seltext.
  w_fieldcat-edit edit.
  w_fieldcat-outputlen = len.
  APPEND w_fieldcat TO t_fieldcat.

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

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      I_CALLBACK_PROGRAM       = sy-repid
      I_CALLBACK_PF_STATUS_SET 'F_STATUS'
      I_CALLBACK_USER_COMMAND  'ALV_USER_COMMAND'
      IS_LAYOUT_LVC            = t_layout
      IT_FIELDCAT_LVC          = t_fieldcat
    TABLES
      T_OUTTAB                 = gt_out
*   EXCEPTIONS
*     PROGRAM_ERROR            = 1
*     OTHERS                   = 2
    .
  IF SY-SUBRC <> 0.
* Implement suitable error handling here
  ENDIF.

ENDFORM.

FORM f_status USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS 'ZXHN_EDIT_ALV01' EXCLUDING rt_extab.
  "frm_platform_o
ENDFORM.

FORM alv_user_command USING r_ucomm LIKE sy-ucomm
                       rs_selfield TYPE slis_selfield.

  CASE r_ucomm.
    WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
      LEAVE TO SCREEN 0.
    WHEN 'EDIT'.
      PERFORM frm_edit_data .
    WHEN 'INSERT'.
    WHEN 'SAVE'.
      PERFORM frm_save_data .

  ENDCASE.

  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      E_GRID = g_grid.
  CALL METHOD g_grid->check_changed_data .
  CALL METHOD g_grid->refresh_table_display .
  rs_selfield-REFRESH 'X'.

ENDFORM.

FORM FRM_EDIT_DATA .
  LOOP AT gt_out INTO gs_out WHERE SELEC 'X'.

    READ TABLE gs_out-celltab INTO ls_celltab WITH KEY fieldname 'ZAGE'.

    IF ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled .
      ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.
    ELSE.
      ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
    ENDIF.

    MODIFY gs_out-celltab FROM ls_celltab INDEX sy-tabix.
    MODIFY gt_out FROM gs_out.
  ENDLOOP .

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

  LOOP AT gt_out INTO gs_out .

    ls_celltab-fieldname 'ZAGE'.         "field
    ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.    "to enable the required fields

    INSERT ls_celltab INTO TABLE lt_celltab.
    INSERT LINES OF lt_celltab INTO TABLE gs_out-celltab.
    MODIFY gt_out FROM gs_out.
    CLEAR: ls_celltab, gs_out .
    REFRESH lt_celltab.

  ENDLOOP .

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

  FIELD-SYMBOLS: <fs_alv> TYPE ty_out.
  DATA: gs_alv_grid TYPE REF TO cl_gui_alv_grid.

  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = gs_alv_grid.
  CALL METHOD gs_alv_grid->check_changed_data.

  LOOP AT gt_out ASSIGNING <fs_alv> WHERE selec EQ 'X'.

    UPDATE ZXHN_EDIT_ALV SET zage = <fs_alv>-zage WHERE zid EQ <fs_alv>-zid .

  ENDLOOP .

ENDFORM.

posted on 2019-06-13 10:44  子曰花开2015  阅读(73)  评论(0编辑  收藏  举报