SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

ALV Grid: Display table and change column's context menu



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ALV Grid / ALV Tree / ALV List
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Sat Sep 15, 2007 8:53 pm    Post subject: ALV Grid: Display table and change column's context menu Reply with quote

Display table and changed context menu for the column #2 - added my own function code and disabled all standard functional codes. Context menus for other columns remain unchanged. The function is the same as for double click or additional button.
Events:
  • print events (see above)
  • double_click
  • toolbar
  • user_command
  • context_menu_request
Used object:
  • cl_gui_alv_grid
  • cl_gui_splitter_container
  • cl_gui_container
  • cl_gui_dialogbox_container
  • cl_alv_event_toolbar_set (in event handler)
  • cl_ctmenu (in event handler)


Code:
program z.
* The context menu for the 2nd column does the same
* as "Plants List" button
* Context menus for other columns remain unchanged.

*---------------------------------------------------------------------*
*       Constants                                                     *
*---------------------------------------------------------------------*
*---------------------------------------------------------------------*
*       Types                                                         *
*---------------------------------------------------------------------*
class lcl_event_receiver definition deferred.
class cl_gui_cfw definition load.
*---------------------------------------------------------------------*
*       Data                                                          *
*---------------------------------------------------------------------*
data: it_t001  type table of t001,
      it_t001k type table of t001k.
*---------------------------------------------------------------------*
*       Classes                                                       *
*---------------------------------------------------------------------*
*---------------------------------------------------------------------*
*       Definitions                                                   *
*---------------------------------------------------------------------*
*---------------------------------------------------------------------*
class screen_init definition create private.
  public section.
    class-methods  init_screen returning value(this)
                               type ref to screen_init.
    methods:       constructor,
                   show_details importing value(p_bukrs)
                                type t001-bukrs,
                   get_selected changing p_rows type lvc_t_row,
                   get_current_cell exporting p_col type i.
  private section.
    data:          "---- main list
                   splitter  type ref to cl_gui_splitter_container,
                   grid      type ref to cl_gui_alv_grid,
                   gs_print  type lvc_s_prnt,
                   gs_layout type lvc_s_layo,
                   "---- detail list in an amodal window
                   grid2     type ref to cl_gui_alv_grid,
                   gs_print2 type lvc_s_prnt,
                   gs_layout2 type lvc_s_layo,
                   dialogbox type ref to cl_gui_dialogbox_container,
                   e_handler type ref to lcl_event_receiver.
    methods        fill_grid.
endclass.
*---------------------------------------------------------------------*
class lcl_event_receiver definition.
  public section.
* methods for double click.
    methods: handle_double_click for event double_click
                                 of cl_gui_alv_grid
                                 importing e_row e_column,
             handle_close for event close
                                 of cl_gui_dialogbox_container
                                 importing sender,
* methods for additional button on toolbar.
*         add own button to toolbar.
             handle_toolbar      for event toolbar of cl_gui_alv_grid
                                 importing e_object e_interactive,
*         handle user command when user press additional button.
             handle_user_command for event user_command
                                 of cl_gui_alv_grid
                                 importing e_ucomm,
* method for context menu.
             handle_context_menu for event context_menu_request
                                 of cl_gui_alv_grid
                                 importing e_object,
* methods for print events.
             handle_top_of_page for event print_top_of_page
                                of cl_gui_alv_grid,
             handle_end_of_page for event print_end_of_page
                                of cl_gui_alv_grid,
             handle_top_of_list for event print_top_of_list
                                of cl_gui_alv_grid,
             handle_end_of_list for event print_end_of_list
                                of cl_gui_alv_grid.
  private section.
    data:    pagenum type i.
endclass.
*---------------------------------------------------------------------*

*---------------------------------------------------------------------*
*       Data                                                          *
*---------------------------------------------------------------------*
data this_screen type ref to screen_init.

*---------------------------------------------------------------------*
*       Implementations                                               *
*---------------------------------------------------------------------*
*---------------------------------------------------------------------*
class screen_init implementation.
*-------------------------------*
  method init_screen.
    data screen type ref to screen_init.
    create object screen.
    this = screen.
  endmethod.
*-------------------------------*
  method constructor.
    data container type ref to cl_gui_container.
    create object splitter
           exporting parent = cl_gui_container=>screen0
                     rows = 1
                     columns = 1.
    call method splitter->set_border
         exporting border = cl_gui_cfw=>false.
    call method splitter->set_column_mode
         exporting mode = splitter->mode_absolute.
    container = splitter->get_container( row = 1 column = 1 ).

    create object grid exporting i_parent = container.
    gs_layout-grid_title = 'Companies table T001.'.
* reserve two lines for the print_end_of_page event
    gs_print-reservelns = 2.

    call method  me->fill_grid.

    create object e_handler.
    set handler e_handler->handle_toolbar for grid.
    set handler e_handler->handle_user_command for grid.
    set handler e_handler->handle_context_menu for grid.
    set handler e_handler->handle_double_click for grid.
    set handler e_handler->handle_top_of_list for grid.
    set handler e_handler->handle_top_of_page for grid.
    set handler e_handler->handle_end_of_list for grid.
    set handler e_handler->handle_end_of_page for grid.
* call method 'set_toolbar_interactive' to raise event toolbar.
    call method grid->set_toolbar_interactive.
  endmethod.
*-------------------------------*
  method show_details.
    data grid_title type lvc_title.
    concatenate 'Plants for' p_bukrs into grid_title
                  separated by space.
* create dialogbox to show detail list (if not already existent)
    if dialogbox is initial.
      gs_layout2-grid_title = grid_title.
* reserve two lines for the print_end_of_page event
      gs_print-reservelns = 2.
* create dialogbox container as dynpro-instance
* when the user switches to another screen, it is
* destroyed by lifetime mangagement of cfw
      create object dialogbox
          exporting
            top = 50
            left = 150
            lifetime = cntl_lifetime_dynpro
            caption = gs_layout2-grid_title
            width = 800
            height = 200.
      create object grid2
          exporting i_parent = dialogbox.
* register abap oo event 'close'. it is not necessary to register this
* event at the frontend (this is done during creation).
      set handler e_handler->handle_close for dialogbox.
      call method grid2->set_table_for_first_display
           exporting i_structure_name = 'T001K'
                     is_print         = gs_print2
                     is_layout        = gs_layout2
           changing  it_outtab        = it_t001k.
      call method cl_gui_control=>set_focus exporting control = grid2.
    else.
      call method:
        dialogbox->set_caption exporting caption = grid_title,
        dialogbox->set_visible exporting visible = 'x',
        grid2->set_gridtitle exporting i_gridtitle = grid_title,
        grid2->refresh_table_display.
    endif.
  endmethod.
*-------------------------------*
  method fill_grid.
    select * from t001 into table it_t001.
    call method grid->set_table_for_first_display
         exporting i_structure_name = 'T001'
                   is_print         = gs_print
                   is_layout        = gs_layout
         changing  it_outtab        = it_t001.
  endmethod.
*-------------------------------*
  method get_selected.
    call method grid->get_selected_rows
                importing et_index_rows = p_rows.
  endmethod.
*-------------------------------*
  method get_current_cell.
    call method grid->get_current_cell importing e_col = p_col.
  endmethod.
endclass.
*---------------------------------------------------------------------*
class lcl_event_receiver implementation.
* event handler methods for toolbar event.
*-------------------------------*
  method handle_toolbar.
    data: ls_toolbar  type stb_button.
*....................................................................
* e_object of event toolbar is of type ref to cl_alv_event_toolbar_set.
* this class has got one attribute, namely mt_toolbar, which
* is a table of type ttb_button. one line of this table is
* defined by the structure stb_button (see data declaration above).
* a remark to the flag e_interactive:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*         'e_interactive' is set, if this event is raised due to
*         the call of 'set_toolbar_interactive' by the user.
*         you can distinguish this way if the event was raised
*         by yourself or by alv
*         (e.g. in method 'refresh_table_display').
*         an application of this feature is still unknown... :-)

* append a separator to normal toolbar
    clear ls_toolbar.
    move 3 to ls_toolbar-butn_type.
    append ls_toolbar to e_object->mt_toolbar.
* append an icon to show booking table
    clear ls_toolbar.
    move 'PLANTS' to ls_toolbar-function.
    move '@A8@' to ls_toolbar-icon. "ICON_PLANT
    move 'show plants list' to ls_toolbar-quickinfo.
    move 'Plants for the Company' to ls_toolbar-text.
    move ' ' to ls_toolbar-disabled.
    append ls_toolbar to e_object->mt_toolbar.
  endmethod.
* event handler methods for user_command event.
*-------------------------------*
  method handle_user_command.
    data: wa_t001  like line of it_t001,
          wa_t001k like line of it_t001k,
          lt_rows          type lvc_t_row,
          ls_selected_line type lvc_s_row.
    case e_ucomm.
      when 'PLANTS'         "from additional button
        or 'MPLANTS'.       "from context menu
        call method this_screen->get_selected changing p_rows = lt_rows.
        call method cl_gui_cfw=>flush.
        loop at lt_rows into ls_selected_line.
          read table it_t001 index ls_selected_line-index into wa_t001.
          select * from t001k into table it_t001k
                 where bukrs = wa_t001-bukrs.
          call method this_screen->show_details
                      exporting p_bukrs = wa_t001-bukrs.
        endloop.
    endcase.
  endmethod.
* event handler methods for context_menu_request event.
*-------------------------------*
  method handle_context_menu.
    data: lt_std_fcodes type ui_functions,
          lt_std_fc_get type ui_funcattr,
          std_fc        type uiattentry,
          lt_own_fcodes type ui_functions,
          li_sel_col type i.           "selected column
*....................................................................
* e_object of event context_menu_request is of type ref to cl_ctmenu.

* query which column was selected.
    call method this_screen->get_current_cell
                importing p_col = li_sel_col.
    call method cl_gui_cfw=>flush.
    check sy-subrc = 0.

* change menu only for 2nd column
    check li_sel_col = 2.

* add new function code
    append 'MPLANTS' to lt_own_fcodes.
    call method e_object->add_function exporting
                          fcode = 'MPLANTS'
                          text  = 'Display Plants'.

* build table for standard functions
    call method e_object->get_functions
                importing fcodes = lt_std_fc_get.
    loop at lt_std_fc_get into std_fc.
      append std_fc-fcode to lt_std_fcodes.
    endloop.

* disable all standard functions and show new function
    call method: e_object->disable_functions
                           exporting fcodes = lt_std_fcodes,
                 e_object->enable_functions
                           exporting fcodes = lt_own_fcodes,
                 cl_gui_cfw=>flush.
  endmethod.
* event handler methods for dbl-click event.
*-------------------------------*
  method handle_double_click.
    data: wa_t001  like line of it_t001,
          wa_t001k like line of it_t001k.
    read table it_t001 index e_row-index into wa_t001.
    select * from t001k into table it_t001k
           where bukrs = wa_t001-bukrs.
    call method this_screen->show_details
                exporting p_bukrs = wa_t001-bukrs.
  endmethod.
*-------------------------------*
  method handle_close.
* set dialogbox invisible
* (the dialogbox is destroyed outomatically when the user
* switches to another dynpro).
    call method sender->set_visible exporting visible = space.
* in this example closing the dialogbox leads
* to make it invisible. it is also conceivable to destroy it
* and recreate it if the user doubleclicks a line again.
* displaying a great amount of data has a greater impact on performance.
  endmethod.

*------------------------------------------------------------
* event handler methods for print events. use write to provide output.
*-------------------------------*
  method handle_top_of_page.
    add 1 to pagenum.
    write: / 'event: print_top_of_page #', pagenum,
             ' program:', sy-cprog.
    call method cl_gui_cfw=>flush.
  endmethod.
*-------------------------------*
  method handle_end_of_page.
    write: / 'event: print_end_of_page #', pagenum,
             ' program:', sy-cprog.
    call method cl_gui_cfw=>flush.
  endmethod.
*-------------------------------*
  method handle_top_of_list.
    write: / 'event: print_top_of_list, program:', sy-cprog.
    call method cl_gui_cfw=>flush.
  endmethod.
*-------------------------------*
  method handle_end_of_list.
    write: / 'event: print_end_of_list, program:', sy-cprog.
    call method cl_gui_cfw=>flush.
  endmethod.
endclass.
*---------------------------------------------------------------------*

*---------------------------------------------------------------------*
*       Program execution                                             *
*---------------------------------------------------------------------*
load-of-program.
  call screen 100.

*---------------------------------------------------------------------*
*       Dialog Modules PBO                                            *
*---------------------------------------------------------------------*
module status_0100 output.
  set pf-status 'SCREEN_100'.
  set titlebar 'TIT_100'.
  this_screen = screen_init=>init_screen( ).
endmodule.
*---------------------------------------------------------------------*
*       Dialog Modules PAI                                            *
*---------------------------------------------------------------------*
module cancel input.
  leave program.
endmodule.
*----------------------------------------------------------------------*


Source: http://www.geocities.com/victorav15/sapr3/utilities/zvvooa_alv5.txt
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ALV Grid / ALV Tree / ALV List All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.