26/02/2013

ABAP Code Sample for Data Browser Using ALV Grid


Summary :

Here is a code sample that performs the operation similar to Data Browser.
This adopts the simplest way to display any table from Data Dictionary and
the data of any table can be edited right from ALV Grid itself.

Some of the concepts that can be learnt from this code samples are
a. Creating ALV grid (for Displaying as well as editing Data).
b. Generating Field Catalog Automatically.
c. Creating Dynamic internal Table.
d. Moving buttons to the ALV tool-bar & Creating Event-Handlers.


*&--------------------------------------------------------------------*
*& Report  ZIALV_EDIT_SIMPLE1                                         *
*&--------------------------------------------------------------------*

*---------------------------------------------------------------------*
* Description                      : ALV to perform Displaying and Editing Tables
* Technical Contact            : tech.sap29@gmail.com
* Technical Spec. Number : ZIALV_EDIT_SIMPLE1
* Created on                      : 24/01/04
*---------------------------------------------------------------------*

report  zialv_edit_simple1            .
include <icon>.

*----------------------------------------------------------------------
*  Definition of Grid event-handler class
*----------------------------------------------------------------------
class lcl_grid_event_receiver definition.

  public section.
    methods:
      toolbar              for event toolbar
                            of cl_gui_alv_grid
                            importing e_object
                                      e_interactive

    ,user_command         for event user_command
                            of cl_gui_alv_grid
                            importing e_ucomm
.  " Period

endclass.

*-------------------------------------------------------------------*
*mplementation of Grid event-handler class
*-------------------------------------------------------------------*
class lcl_grid_event_receiver implementation.

*---------------------------------------------------------------------
* Method for handling all creation/modification calls to the toolbar
*---------------------------------------------------------------------
  method toolbar.

    data : ls_toolbar type stb_button.
*--------------------------------------------------------------------*
* Define Custom Button in the toolbar
*--------------------------------------------------------------------*
    clear ls_toolbar.
    move 0 to ls_toolbar-butn_type.
    move 'EDIT' to ls_toolbar-function.
    move space to ls_toolbar-disabled.
    move 'EDIT' to ls_toolbar-text.
    move icon_change_text to ls_toolbar-icon.
    move 'Click2Edit' to ls_toolbar-quickinfo.

    append ls_toolbar to e_object->mt_toolbar.

    clear ls_toolbar.
    move 0 to ls_toolbar-butn_type.
    move 'UPDA' to ls_toolbar-function.
    move space to ls_toolbar-disabled.
    move 'UPDATE' to ls_toolbar-text.
    move icon_system_save to ls_toolbar-icon.
    move 'Click2Update' to ls_toolbar-quickinfo.

    append ls_toolbar to e_object->mt_toolbar.

    clear ls_toolbar.
    move 0 to ls_toolbar-butn_type.
    move 'EXIT' to ls_toolbar-function.
    move space to ls_toolbar-disabled.
    move 'EXIT' to ls_toolbar-text.
    move icon_system_end to ls_toolbar-icon.
    move 'Click2Exit' to ls_toolbar-quickinfo.

    append ls_toolbar to e_object->mt_toolbar.

  endmethod.

*----------------------------------------------------------------------
*  Method to handle user commands from toolbar
*----------------------------------------------------------------------
  method user_command.

    case e_ucomm .

      when 'EDIT'.
        perform set_input.

      when 'UPDA'.
        perform refresh_disp.
        perform update_table.

      when 'EXIT'.
        leave program.

    endcase.

  endmethod.

endclass.

*---------------------------------------------------------------*
*        Declarations
*---------------------------------------------------------------*

parameters:
  p_intab type dfies-tabname default 'SAPLANE'.


data :
  dref type ref to data,
  it_grid_fcat type lvc_t_fcat,
  struct_grid_lset type lvc_s_layo,
  tab_info like table of dfies.

field-symbols :
  <fs_tab> like line of tab_info,
**** Output Structure****
  <it_disptab> type table.

data :

    ok_code like sy-ucomm
   ,save_ok like sy-ucomm

*----------------------------------------------------------------------
* Container Object [grid_container]
*----------------------------------------------------------------------
  ,grid_container type ref to cl_gui_custom_container
*----------------------------------------------------------------------
* Control Object [grid]
*----------------------------------------------------------------------
  ,grid type ref to cl_gui_alv_grid

*----------------------------------------------------------------------
* Event-Handler Object [grid_handler]
*----------------------------------------------------------------------
 ,grid_handler type ref to lcl_grid_event_receiver

  . " period


*----------------------------------------------------------------------
* Begin of process logic
*----------------------------------------------------------------------
start-of-selection.

  call screen '0100'.


*&--------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&--------------------------------------------------------------------*
*      PBO module ...Displays the Grid
*---------------------------------------------------------------------*

module status_0100 output.

*--------------------------------------------------------------------*
*  A L V    G R I D
*--------------------------------------------------------------------*

  if grid_container is initial.

    create object grid_container
        exporting
          container_name = 'CCONTAINER1'.

    create object grid
        exporting
           i_appl_events = 'X'
           i_parent = grid_container.

    create object grid_handler.
    set handler:
       grid_handler->user_command for grid,
       grid_handler->toolbar for grid .

*Assigning the input value to <fs_tab>-tabname
    assign p_intab to <fs_tab>-tabname .

    call function 'LVC_FIELDCATALOG_MERGE'
         exporting
              i_structure_name = <fs_tab>-tabname
         changing
              ct_fieldcat      = it_grid_fcat.

*Creating internal table
    call method cl_alv_table_create=>create_dynamic_table
       exporting
           it_fieldcatalog           = it_grid_fcat
       importing
           ep_table                  = dref.

    assign  dref->* to <it_disptab>.

    perform populate_grid_data .

    call method grid->set_ready_for_input
        exporting
          i_ready_for_input = 0.


*Enabling the grid to edit mode,
*setting table name as title of the grid
    struct_grid_lset-edit = 'X'. "To enable editing in ALV
    struct_grid_lset-grid_title  = p_intab.

    call method grid->set_table_for_first_display
      exporting
        is_layout           = struct_grid_lset
      changing
        it_outtab             =  <it_disptab>
        it_fieldcatalog       =  it_grid_fcat
    .      " Period


  endif.

endmodule.

*&--------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&--------------------------------------------------------------------*
module user_command_0100 input.

****User Commands are handled as events in method user_command.

endmodule.                 " USER_COMMAND_0100  INPUT


*&--------------------------------------------------------------------*
*&      Form  EXIT_PROGRAM
*&--------------------------------------------------------------------*
form exit_program.

  call method grid_container->free.

  call method cl_gui_cfw=>flush.

  leave program.

endform.                  " EXIT_PROGRAM

*&--------------------------------------------------------------------*
*&      Form  populate_grid_data
*&--------------------------------------------------------------------*
form populate_grid_data.

 select * from (p_intab) into corresponding fields of table <it_disptab>.

endform.                     " populate_grid_data


*---------------------------------------------------------------------*
*       FORM refresh_disp                                             *
*---------------------------------------------------------------------*
form refresh_disp.

  call method grid->refresh_table_display.

endform.

*---------------------------------------------------------------------*
*       FORM update_table                                             *
*---------------------------------------------------------------------*
form update_table.

  modify (p_intab) from table <it_disptab>.

  call method grid->set_ready_for_input
      exporting
          i_ready_for_input = 0.

endform.


*---------------------------------------------------------------------*
*       FORM set_input                                                *
*---------------------------------------------------------------------*
form set_input.

  call method grid->set_ready_for_input
     exporting
       i_ready_for_input = 1.

endform.



Screen Logic :


process before output.

  module status_0100.
process after input.

  module user_command_0100.



Screen No : 100
The screen consists of just a custom control of name ‘CCONTAINER1’





No comments:

Post a Comment