Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

ALV output save

Hi everyone,

I m very new to ABAP objects and i have never worked on this.

I have one requirment, In my ALV(Normal not Oops) output there r some records. If i select one record and click on save it shud be saved.The user shud select only one at a time.

This requirment i want to do in oops. So can anybody tell me r give me sample code(in a simple way) . so that i can understand it and do it.

regards

Sameer

replied
*&---------------------------------------------------------------------*
*& Report  ZGRID_SAMPLE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zgrid_sample.

TABLES: vbak.

TYPE-POOLS: abap.

TYPES: BEGIN OF types_vbak,
       check TYPE boole_d,
       vbeln TYPE vbeln,
       vkorg TYPE vkorg,
       netwr TYPE netwr,
       END OF types_vbak,

       types_t_vbak TYPE STANDARD TABLE OF types_vbak.


DATA: gc_grid TYPE REF TO cl_gui_alv_grid,
      gt_vbak TYPE types_t_vbak,
      gt_fcat TYPE lvc_t_fcat,
      gw_count(2) TYPE n.


*----------------------------------------------------------------------*
*       CLASS lc_levent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS: lc_levent DEFINITION.

  PUBLIC SECTION.

    CLASS-METHODS: lc_click FOR EVENT hotspot_click
                                OF cl_gui_alv_grid
                                IMPORTING e_row_id
                                          e_column_id
                                          es_row_no.

ENDCLASS.                    "lc_levent DEFINITION


*----------------------------------------------------------------------*
*       CLASS lc_event  IMPLMENATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS: lc_levent IMPLEMENTATION.

  METHOD: lc_click.
    PERFORM sub_catch_event USING e_row_id
                                  e_column_id
                                  es_row_no.
  ENDMETHOD.                    "lc_click

ENDCLASS.                    "lc_event  IMPLMENATION

SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE text-s01.
SELECT-OPTIONS: s_vbeln FOR vbak-vbeln.
SELECTION-SCREEN: END OF BLOCK b1.


START-OF-SELECTION.

  SELECT vbeln
         vkorg
         netwr
         FROM vbak INTO CORRESPONDING FIELDS
                             OF TABLE gt_vbak
                       WHERE vbeln IN s_vbeln.

  IF sy-subrc NE 0.
    MESSAGE 'No Data found' TYPE 'E'.
  ELSE.
**  set  the initial row.
    PERFORM set_initial_row.
** Call the Screen
    CALL SCREEN '9001'.
  ENDIF.
*&---------------------------------------------------------------------*
*&      Module  STATUS_9001  OUTPUT
*&---------------------------------------------------------------------*
*       Show Grid Output
*----------------------------------------------------------------------*
MODULE status_9001 OUTPUT.

  SET PF-STATUS 'MAIN'.
  SET TITLEBAR 'MAIN'.

ENDMODULE.                 " STATUS_9001  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_9001  INPUT
*&---------------------------------------------------------------------*
*       Handle Command
*----------------------------------------------------------------------*
MODULE user_command_9001 INPUT.

  CASE sy-ucomm.

    WHEN 'BACK' OR
         '%EX'  OR
         'CANC'.
      LEAVE TO SCREEN 0.
    WHEN 'INSR'.
      PERFORM sub_insert_row.
  ENDCASE.

ENDMODULE.                 " USER_COMMAND_9001  INPUT
*&---------------------------------------------------------------------*
*&      Module  SET_GRID  OUTPUT
*&---------------------------------------------------------------------*
*       Set Grid Display
*----------------------------------------------------------------------*
MODULE set_grid OUTPUT.

  DATA: ls_layo TYPE lvc_s_layo.


  CREATE OBJECT gc_grid
    EXPORTING
      i_parent          = cl_gui_container=>screen0
    EXCEPTIONS
      error_cntl_create = 1
      error_cntl_init   = 2
      error_cntl_link   = 3
      error_dp_create   = 4
      OTHERS            = 5.
  IF sy-subrc = 0.
** create the FCAT
    PERFORM sub_create_fcat CHANGING gt_fcat.
** Display GRID
    CALL METHOD gc_grid->set_table_for_first_display
      EXPORTING
        i_default                     = 'X'
        is_layout                     = ls_layo
      CHANGING
        it_outtab                     = gt_vbak
        it_fieldcatalog               = gt_fcat
*        it_sort                       =
*        it_filter                     =
      EXCEPTIONS
        invalid_parameter_combination = 1
        program_error                 = 2
        too_many_lines                = 3
        OTHERS                        = 4
            .
    IF sy-subrc <> 0.
      MESSAGE 'No Grid Displayed' TYPE 'E'.
    ELSE.
      SET HANDLER lc_levent=>lc_click FOR gc_grid.
    ENDIF.
  ENDIF.



ENDMODULE.                 " SET_GRID  OUTPUT
*&---------------------------------------------------------------------*
*&      Form  SUB_CREATE_FCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM sub_create_fcat CHANGING ct_fcat TYPE lvc_t_fcat.

  DATA: ls_fcat TYPE lvc_s_fcat.

  CLEAR: ls_fcat.
  ls_fcat-col_pos = 1.
  ls_fcat-fieldname = 'CHECK'.
  ls_fcat-tabname = 'GT_VBAK'.
  ls_fcat-checkbox = abap_true.
  ls_fcat-hotspot = abap_true.
  APPEND ls_fcat TO ct_fcat.

  CLEAR: ls_fcat.
  ls_fcat-col_pos = 2.
  ls_fcat-fieldname = 'VBELN'.
  ls_fcat-tabname = 'GT_VBAK'.
  ls_fcat-rollname = 'VBELN'.
  APPEND ls_fcat TO ct_fcat.

  CLEAR: ls_fcat.
  ls_fcat-col_pos = 3.
  ls_fcat-fieldname = 'VKORG'.
  ls_fcat-tabname = 'GT_VBAK'.
  ls_fcat-rollname = 'VKORG'.
  APPEND ls_fcat TO ct_fcat.

  CLEAR: ls_fcat.
  ls_fcat-col_pos = 4.
  ls_fcat-fieldname = 'NETWR'.
  ls_fcat-tabname = 'GT_VBAK'.
  ls_fcat-rollname = 'NETWR'.
  APPEND ls_fcat TO ct_fcat.


ENDFORM.                    " SUB_CREATE_FCAT
*&---------------------------------------------------------------------*
*&      Form  SUB_INSERT_ROW
*&---------------------------------------------------------------------*
*       Insert New Row
*----------------------------------------------------------------------*
FORM sub_insert_row .



ENDFORM.                    " SUB_INSERT_ROW
*&---------------------------------------------------------------------*
*&      Form  SET_INITIAL_ROW
*&---------------------------------------------------------------------*
*       Set Initial Row
*----------------------------------------------------------------------*
FORM set_initial_row .




ENDFORM.                    " SET_INITIAL_ROW
*&---------------------------------------------------------------------*
*&      Form  SUB_CATCH_EVENT
*&---------------------------------------------------------------------*
*       Catch Click event of a Hotspot
*----------------------------------------------------------------------*
FORM sub_catch_event  USING uw_row_id     TYPE lvc_s_row
                            uw_column_id  TYPE lvc_s_col
                            us_row_no     TYPE lvc_s_roid.
  FIELD-SYMBOLS: <l_vbak> TYPE types_vbak.

  IF uw_column_id = 'CHECK'.
    gw_count = gw_count + 1.
    IF gw_count > 1.
      MESSAGE e003(z1) WITH 'Please Select Only one row'.
    ELSE.
      READ TABLE gt_vbak ASSIGNING <l_vbak>
                         INDEX uw_row_id.
      IF sy-subrc EQ 0.
        IF <l_vbak>-check = abap_true.
          <l_vbak>-check = abap_false.
        ELSE.
          <l_vbak>-check = abap_true.
        ENDIF.
        gc_grid->refresh_table_display( ).
      ENDIF.
    ENDIF.
  ENDIF.
ENDFORM.                    " SUB_CATCH_EVENT

The above code is a simple example for handling check-boxes in OO-ALV, use this to populate the final table value for Checkboxes and then hadle the sy-ucomm event of the Screen to process them

Hope That Helps

Anirban M.

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question