Register Login

Delete Record Through ALV grid Using Checkbox

Updated May 18, 2018

Create the table with following field insert Data

  • MANDT 3
  • PERNR   8
  • REFNO  8
  • NAME    40
  • SALARY 15

Write Following code and create Screen with Screen Number 600 with Custom Control CCONT and two buttons with Function Code  DEL AND EXIT.

*&---------------------------------------------------------------------*

*& Report  ZMAKE_EDIT

*&

*&---------------------------------------------------------------------*

*&

*&

*&---------------------------------------------------------------------*



REPORT  ZDELETE_DATA_ALV.

Tables:PA0001,ZTEST_EDIT.



TYPE-POOLS : slis.

DATA: graphic_size TYPE i.

TYPES pict_line(256) TYPE c.



* data declarations......................



DATA :init,



      container TYPE REF TO cl_gui_custom_container,



      editor    TYPE REF TO cl_gui_textedit,



      picture   TYPE REF TO cl_gui_picture,



      pict_tab TYPE TABLE OF pict_line,



      url(255) TYPE c.





SELECTION-SCREEN:BEGIN OF BLOCK b1 WITH FRAME TITLE text-010.

Select-options: S_PERNR for PA0001-PERNR.





*SELECTION-SCREEN PUSHBUTTON 70(4) but1 USER-COMMAND SAVE.





SELECTION-SCREEN END OF BLOCK b1.



DATA: gstring TYPE c.



Data: stable TYPE lvc_s_stbl.



*Data declarations for ALV

DATA: c_ccont TYPE REF TO cl_gui_custom_container,         "Custom container object

      c_alvgd         TYPE REF TO cl_gui_alv_grid,         "ALV grid object

      it_fcat            TYPE lvc_t_fcat,                  "Field catalogue

      it_layout          TYPE lvc_s_layo.

Types:Begin of ty_edit,

*Include structure ZTEST_EDIT.

*CHECK type C,

PERNR type ZTEST_EDIT-PERNR,

 NAME  type ZTEST_EDIT-NAME,

 SALARY  type ZTEST_EDIT-SALARY,

 end of ty_edit.





 types:Begin of ty_final,

*Include structure ZTEST_EDIT.

CHECK type C,

PERNR type ZTEST_EDIT-PERNR,

 NAME  type ZTEST_EDIT-NAME,

 SALARY  type ZTEST_EDIT-SALARY,

 end of ty_final.



 Data:IT_FINAL type table of ty_final ,

     WA_FINAL type ty_final.





Data:IT_EDIT type table of ty_edit,

     WA_EDIT type ty_edit.



     types:Begin of ty_edit1.

     Include structure ZTEST_EDIT.



     types: end of ty_edit1.







    Data: IT_EDIT1 type table of ty_edit1,

     WA_EDIT1 type ty_edit1.

START-OF-SELECTION.

  select  PERNR NAME SALARY into table it_edit from ZTEST_EDIT where PERNR in S_PERNR.

loop at it_edit into wa_edit.

wa_final-PERNR = wa_edit-PERNR.

wa_final-NAME  = wa_edit-NAME.

wa_final-SALARY  = wa_edit-SALARY.

wa_final-CHECK = ''.



append wa_final to it_final.







endloop.



* Calling the ALV screen with custom container with name CCONT.



 CALL SCREEN 0600.

*&---------------------------------------------------------------------*

*&      Module  STATUS_0600  OUTPUT

*&---------------------------------------------------------------------*

*       text

*----------------------------------------------------------------------*

MODULE STATUS_0600 OUTPUT.

*  SET PF-STATUS 'xxxxxxxx'.

*  SET TITLEBAR 'xxx'.



"Creating objects of the container

  CREATE OBJECT c_ccont

       EXPORTING

          container_name = 'CCONT'.



*  create object for alv grid

  create object c_alvgd

  exporting

  i_parent = c_ccont.



*  SET field for ALV

  PERFORM alv_build_fieldcat.



* Set ALV attributes FOR LAYOUT

  PERFORM alv_report_layout.



  CHECK NOT c_alvgd IS INITIAL.



* Call ALV GRID



  CALL METHOD c_alvgd->set_table_for_first_display

    EXPORTING

      is_layout                     = it_layout

      i_save                        = 'A'

    CHANGING

      it_outtab                     = IT_FINAL

      it_fieldcatalog               = it_fcat

    EXCEPTIONS

      invalid_parameter_combination = 1

      program_error                 = 2

      too_many_lines                = 3

      OTHERS                        = 4.

  IF sy-subrc <> 0.

    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

  ENDIF.



ENDMODULE.                 " STATUS_0600  OUTPUT





FORM alv_build_fieldcat.



  DATA lv_fldcat TYPE lvc_s_fcat.

  CLEAR lv_fldcat.

  lv_fldcat-row_pos   = '1'.

  lv_fldcat-col_pos   = '2'.



  lv_fldcat-fieldname = 'CHECK'.

lv_fldcat-tabname = 'IT_FINAL'.

lv_fldcat-checkbox = 'X'.

lv_fldcat-edit         = 'X'.

lv_fldcat-scrtext_m = 'Status'.



APPEND lv_fldcat TO it_fcat.

  CLEAR lv_fldcat.



  lv_fldcat-row_pos   = '1'.

  lv_fldcat-col_pos   = '2'.

  lv_fldcat-fieldname = 'PERNR'.

  lv_fldcat-tabname   = 'IT_FINAL'.

  lv_fldcat-outputlen = 8.

  lv_fldcat-scrtext_m = 'Employee Code'.

  lv_fldcat-icon = 'X'.

  APPEND lv_fldcat TO it_fcat.

  CLEAR lv_fldcat.



  lv_fldcat-row_pos   = '1'.

  lv_fldcat-col_pos   = '3'.

  lv_fldcat-fieldname = 'NAME'.

  lv_fldcat-tabname   = 'IT_FINAL'.

  lv_fldcat-outputlen = 15.

  lv_fldcat-scrtext_m = 'Name'.

  lv_fldcat-icon = ''.

  APPEND lv_fldcat TO it_fcat.

  CLEAR lv_fldcat.

  lv_fldcat-row_pos   = '1'.

  lv_fldcat-col_pos   = '4'.

  lv_fldcat-fieldname = 'SALARY'.

  lv_fldcat-tabname   = 'IT_FINAL'.

  lv_fldcat-outputlen = 15.

  lv_fldcat-scrtext_m = 'SALARY'.

  lv_fldcat-edit = 'X'. "Make column Editable.

   APPEND lv_fldcat TO it_fcat.

  CLEAR lv_fldcat.

  "ENDLOOP.



ENDFORM.                    "



FORM alv_report_layout.

  it_layout-cwidth_opt = 'X'.

  it_layout-col_opt = 'X'.

  it_layout-zebra = 'X'.



ENDFORM.



*&---------------------------------------------------------------------*

*&      Module  USER_COMMAND_0600  INPUT

*&---------------------------------------------------------------------*

*       text

*----------------------------------------------------------------------*

MODULE USER_COMMAND_0600 INPUT.

c_alvgd->check_changed_data( ).



*Based on the user input

*When user clicks 'DELETE;

  CASE SY-UCOMM.



    WHEN 'DEL'.



*A pop up is called to confirm the saving of changed data

      CALL FUNCTION 'POPUP_TO_CONFIRM'

        EXPORTING

          titlebar       = 'SAVING DATA'

          text_question  = 'Do you want Delete the data?'

          icon_button_1  = 'icon_booking_ok'

        IMPORTING

          answer         = gstring

        EXCEPTIONS

          text_not_found = 1

          OTHERS         = 2.

      IF sy-subrc NE 0.

*       MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

*               WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

      ENDIF.



*When the User clicks 'YES'

      IF ( gstring = '1' ).

        MESSAGE ' Data is Deleted' TYPE 'S'.

**Now the changed data is stored in the it_pbo internal table

*        "it_pbo = it_output.

*        "clear it_table1.



loop at IT_FINAL into WA_FINAL where CHECK = 'X'.

  wa_edit1-PERNR  = WA_FINAL-PERNR.

  wa_edit1-NAME  = WA_FINAL-NAME.



wa_edit1-SALARY = WA_FINAL-SALARY.

append wa_edit1 to it_edit1.

*break-point.



clear wa_edit1.

endloop.



loop at IT_edit1 into WA_EDIT1.



*ZTEST_EDIT-PERNR  =  wa_edit1-PERNR.

*  ZTEST_EDIT-NAME  = wa_edit1-NAME.

*

ZTEST_EDIT-SALARY = wa_edit1-SALARY.



*update ZTEST_EDIT SET SALARY = wa_edit1-SALARY where PERNR =  wa_edit1-PERNR.

delete from ZTEST_EDIT where PERNR =  wa_edit1-PERNR.

*Delete from tarnsparent table

delete IT_FINAL where PERNR =  wa_edit1-PERNR. "Delete From Internal Table



commit work.

endloop.



*ZTEST_EDIT-pernr =

* UPDATE ZTEST_EDIT FROM TABLE it_edit1.

* commit work.

 "break-point.

        PERFORM redisplay.

      ELSE.

*When user clicks NO or Cancel

        MESSAGE 'Not Saved'  TYPE 'S'.

      ENDIF.

**When the user clicks the 'EXIT; he is out

    WHEN 'EXIT'.

      LEAVE PROGRAM.

  ENDCASE.



  CLEAR: sy-ucomm..



ENDMODULE.                 " PAI  INPUT





FORM redisplay .



*Cells of the alv are made non editable after entering OK to save



  CALL METHOD c_alvgd->set_ready_for_input

    EXPORTING

      i_ready_for_input = 0.



*Row and column of the alv are refreshed after changing values



  stable-row = 'X'.

  stable-col = 'X'.



*REfreshed ALV display with the changed values

*This ALV is non editable and contains new values

  CALL METHOD c_alvgd->refresh_table_display

    EXPORTING

      is_stable = stable

    EXCEPTIONS

      finished  = 1

      OTHERS    = 2.

  IF sy-subrc <> 0.

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

  ENDIF.



ENDFORM.                    " RED



*ENDMODULE.                 " USER_COMMAND_0600  INPU

*&---------------------------------------------------------------------*

*&      Module  PICT  OUTPUT

*&---------------------------------------------------------------------*

*       text

*----------------------------------------------------------------------*

 

 Similarly we can make Update also.

Regards

Sunil Kumar Gautam

JK LAKSHMI CEMENT NEW DELHI

 

Thanks for My respected Sir CS Reddy and my colleagues Dheeraj Sharma,Tusar Sharma, Sandeep Bisth, Deepak Tiwari and my  guru Anurag Shukla  for encourage to write the article.


Comments

  • 22 May 2012 7:57 am Sunil Kumar Gautam
    This is useful delete the data after dispaly
  • 07 Sep 2012 8:11 am Mashimaroda Tipiwas
    Thanks.

×