Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

how to edit alv using cl_salv_table

1190_5939_439
Active Participant
5,772

Hi Experts

Now I meet a requirement that I need edit alv fields . Can you help me ? Thanks

I will edit red field.

The code is following

*&---------------------------------------------------------------------*
*& Report  ZJGLMM012
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT zjglmm012.
TABLES: marc,eina,eine,sscrfields,agr_users, ztesoppap.
DATA:zbz.
DATA  ALV TYPE REF TO cl_salv_table.


TYPES: BEGIN OF ty_item,
         matnr     LIKE marc-matnr,
         maktx     LIKE makt-maktx,
         werks     LIKE marc-werks,
         lifnr     LIKE eina-lifnr,
         name1     LIKE lfa1-name1,
         major     LIKE ztesoppap-major,
         esostu    LIKE ztesoppap-esostu,
         esono     LIKE ztesoppap-esono,
         esodate   LIKE ztesoppap-esodate,
         ppapstu LIKE ztesoppap-ppapstu,
         ppapno    LIKE ztesoppap-ppapno,
         ppapdate  LIKE ztesoppap-ppapdate,
         dled      LIKE ztesoppap-dled,
       END OF ty_item.




DATA: item TYPE STANDARD TABLE OF ty_item ,
       wa_item type ty_item .
*二、声明查询屏幕
SELECTION-SCREEN FUNCTION KEY 1 .

SELECTION-SCREEN BEGIN OF BLOCK blc WITH FRAME TITLE text-001.
PARAMETERS:
pwerks LIKE marc-werks DEFAULT '2100' OBLIGATORY.


SELECT-OPTIONS : smatnr  FOR marc-matnr,
                 slifnr FOR eina-lifnr NO INTERVALS NO-EXTENSION.
SELECTION-SCREEN END OF BLOCK blc.
PARAMETERS: r1 RADIOBUTTON GROUP rad1 USER-COMMAND sele DEFAULT 'X' ,
            r2 RADIOBUTTON GROUP rad1 ,
            r3 RADIOBUTTON GROUP rad1 .

INITIALIZATION.
  sscrfields-functxt_01 = '更新数据'.

AT SELECTION-SCREEN .

  CASE sscrfields-ucomm . "
    WHEN 'FC01'.
      PERFORM frm_update_data.
  ENDCASE.

START-OF-SELECTION.
  PERFORM check_authority.
  CHECK zbz IS INITIAL.
  PERFORM frm_get_data.                 "获取数据
  IF item[] IS NOT INITIAL.
    PERFORM frm_show_data.
  ELSE.
    MESSAGE s001(00) WITH '没有查询到数据'.
  ENDIF.
*&---------------------------------------------------------------------*
*&      Form  FRM_GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_get_data .
  SELECT
    *
    INTO CORRESPONDING FIELDS OF TABLE item
    FROM ztesoppap
    WHERE werks = pwerks
    AND matnr IN smatnr
    AND lifnr IN slifnr .


  LOOP AT item into wa_item .
    SELECT SINGLE maktx INTO wa_item-maktx FROM MAKT WHERE matnr = wa_item-matnr.
    SELECT SINGLE name1 INTO wa_item-name1 FROM lfa1 WHERE lifnr = wa_item-lifnr.
    MODIFY item from wa_item.
  ENDLOOP.


   DATA message TYPE REF TO cx_salv_msg.
      TRY.
       cl_salv_table=>factory(
        IMPORTING
          r_salv_table =  alv
        CHANGING
          t_table      = item ).
       CATCH cx_salv_msg INTO message.
         ENDTRY.
ENDFORM.                    " FRM_GET_DATA
*&---------------------------------------------------------------------*
*&      Form  FRM_SHOW_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_show_data .


       alv->display( ).

ENDFORM.                    " FRM_SHOW_DATA
FORM set_status USING rt_extab TYPE slis_t_extab.
  DATA:it_status TYPE TABLE OF sy-ucomm.
  IF r1 = 'X'.
    APPEND '&ZZPT' TO it_status.
    SET PF-STATUS 'ZCMD1' EXCLUDING it_status.
  ELSE.
    SET PF-STATUS 'ZCMD1'.
  ENDIF.

ENDFORM.

FORM user_command USING rf_ucomm  LIKE sy-ucomm
                        rs        TYPE slis_selfield .

ENDFORM.


*&---------------------------------------------------------------------*
*&      Form  FRM_UPDATE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_update_data .


  SELECT
    marc~matnr
    marc~werks
    eina~lifnr
       INTO CORRESPONDING FIELDS OF TABLE item
     FROM marc
     INNER JOIN eina ON marc~matnr = eina~matnr
     WHERE marc~lvorm <>  'X'
     AND eina~loekz <> 'X'    .


  DATA: lt_zjsfs LIKE TABLE OF ztesoppap WITH HEADER LINE.
  DATA: in_zjsfs LIKE TABLE OF ztesoppap WITH HEADER LINE.
  DATA: up_zjsfs LIKE TABLE OF ztesoppap WITH HEADER LINE.
  SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_zjsfs FROM ztesoppap .
  SORT item BY werks  matnr lifnr.
  DELETE ADJACENT DUPLICATES FROM item COMPARING ALL FIELDS.
  DATA: num(1) TYPE c.
  LOOP AT item into wa_item.
   SELECT SINGLE  *  FROM ztesoppap WHERE matnr = wa_item-matnr
      AND  werks = wa_item-werks
      AND  lifnr = wa_item-lifnr.
    IF sy-subrc = 0."存在相同数据
       clear ztesoppap.                                                "  Because it is defintion in the tables.
      IF ztesoppap-dled IS NOT INITIAL."存在相同数据,但是已经被标记删除
        up_zjsfs-werks =  wa_item-werks.
        up_zjsfs-matnr = wa_item-matnr.
        up_zjsfs-lifnr = wa_item-lifnr.
        up_zjsfs-major = ztesoppap-major.
        up_zjsfs-esostu = ztesoppap-esostu.
         up_zjsfs-esodate = ztesoppap-esodate.
        up_zjsfs-major = ztesoppap-major.
         up_zjsfs-ppapno = ztesoppap-ppapno.
        up_zjsfs-ppapdate = ztesoppap-ppapdate.
        up_zjsfs-dled = ''.
        APPEND up_zjsfs.
      ENDIF.
    ELSEIF sy-subrc <> 0."不存在该数据,做插入动作
      in_zjsfs-werks = wa_item-werks.
      in_zjsfs-matnr = wa_item-matnr.
      in_zjsfs-lifnr = wa_item-lifnr.
      in_zjsfs-dled = ''.
      APPEND in_zjsfs.
    ENDIF.
  ENDLOOP.
  "更新删除标识
  IF up_zjsfs[] IS NOT INITIAL.
    UPDATE ztesoppap FROM TABLE up_zjsfs.
  ENDIF.





  "插入新数据
  IF in_zjsfs[] IS NOT INITIAL.
    INSERT ztesoppap FROM TABLE in_zjsfs.
  ENDIF.




  MESSAGE s001(00) WITH '更新成功'.
ENDFORM.                    " FRM_UPDATE_DATA
*&---------------------------------------------------------------------*
*&      Form  FRM_SAVEDATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_savedata .

ENDFORM.                    " FRM_SAVEDATA
*&---------------------------------------------------------------------*
*&      Form  CHECK_AUTHORITY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM check_authority .
  CLEAR:zbz.
  IF r2 = 'X'.
    SELECT SINGLE *  FROM agr_users WHERE uname = sy-uname AND agr_name = 'Z_AU_ZRPP015T'.
    IF sy-subrc <> 0.
      MESSAGE s001(00) WITH '没有数据维护权限'.
      zbz = 'X'.
    ENDIF.
  ENDIF.
ENDFORM.                    " CHECK_AUTHORITY


3 REPLIES 3

deagle_zhang
Explorer

0 Kudos
3,213

Holy cow! I missed this year's? Thanks for the link, mate.

Sandra_Rossi
Active Contributor
3,213

Already asked in the forum many times. The feature is not provided by SAP, it only exists in CL_GUI_ALV_GRID (but even not supported officially). The grid part of SALV being a wrapper of CL_GUI_ALV_GRID, people have found a few ways to get the instance of the grid control, and shared in the forum/blogs how to get it.