Материал: grid

Внимание! Если размещение файла нарушает Ваши авторские права, то обязательно сообщите нам
background image

An Easy Reference for ALV Grid Control

FORM handle_context_menu_request USING i_object TYPE REF TO cl_ctmenu . 

*--Here you can add some logic to retrive what was clicked and  

*--decide what to add to the context menu 

  CALL METHOD i_object->add_function 

    EXPORTING 

      fcode       = 'CREA' 

      text        = 'Create Booking'(204) . 

ENDFORM . 

Code Part 31 – 

Adding an entry for the context menu 

 
 

You can add a separator by “

add_separator

”, a menu by “

add_menu

”, and a submenu by 

“

add_submenu

” methods. 

  You can also use “

disable_functions

” to disable some of the functions on the menu, 

“

enable_functions

” to enable, “

hide_functions

” to hide them and “

show_functions

” to make 

them displayed again. You pass the list of the function codes to those methods via the parameter 
“

fcodes

”. These are all about the class “

CL_CTMENU

” since the context menu instantiates that class. For 

further functionalities refer to that class. 

DATA lt_fcodes TYPE ui_functions . 

 
CLEAR lt_fcodes. 
APPEND cl_gui_alv_grid=>mc_fc_col_optimize TO lt_fcodes. 
APPEND 'CREA' TO lt_fcodes . 
CALL METHOD e_object->disable_functions 
      EXPORTING fcodes = lt_fcodes . 

Code Part 32 – 

An example filling the table to be passed to above functions 

About Printing 

 
 

There are some events that you can utilize about printing. You simply write what is to be output in 

methods handling these events. Here is the list of events in this context. 
 

print_end_of_list 

Define output text to be printed at the end of the entire list

print_top_of_list 

Define output text to be printed at the beginning of the entire list

print_end_of_page 

Define output text to be printed at the end of each page

print_top_of_page 

Define output text to be printed at the beginning of each page

Table 15 – 

Events for printing

© 2005 SAP AG 

46 

background image

An Easy Reference for ALV Grid Control

 
 

To utilize one of the events above, let’s proceed with the standard procedure since we did not add 

this event in our general scheme at section 

D.1

.  

First add a handler method in your handler class definition as

: 

 
e.g.

METHOD handle_print_top_of_list 

              FOR EVENT print_top_of_list OF cl_gui_alv_grid . 

 
 

Then implement the method in the implementation part of your local class. 

e.g. 

METHOD handle_print_top_of_list . 

        

WRITE:/ 'Flights Made on ', sy-datum . 

    ENDMETHOD . 
 

And register this method as the handler. 

e.g.  

          

SET HANDLER gr_event_handler->handle_print_top_of_list FOR gr_alvgrid . 

 
 

The output of these events can be examined at the print preview or in the printing. 

 
 

Making ALV Grid Editable 

 
 

This may be one of the mostly-used functionalities of the ALV Grid since as a developer we prefer to 

use an ALV Grid instead of a table control for some reasons that are known by all of you (at least for the 
sake of appearance). In fact, making the ALV Grid editable has nothing to do with events. However, since 
controlling data input which is explained in the next section is related, it is better that we deal with this 
topic here. 
 
 

To make a column editable, it will be sufficient to set the field “

EDIT

” in the field catalog. The ALV 

Grid perceives if there are some editable fields and adds buttons for editing purposes. If you do not need 
these new buttons, you know how to exclude them. 
 
 

To make individual cells editable, we will utilize the table we used for making a cell a pushbutton. As 

you remember, it was of type “

LVC_T_STYL

”. If you have not added this inner table, add it now. For this 

procedure; add the name of the field to the field “

FIELDNAME

”, and pass 

“

cl_gui_alv_grid=>mc_style_enabled

” to make a field editable and 

“

cl_gui_alv_grid=>mc_style_disabled

” to make a field non-editable, to the field “

STYLE

”. You 

can use the one with “disable” when you make an entire column editable and want just a few of cells 
along it non-editable. As you remember from the pushbutton section we must tell the layout about this 
styling field. 
 e.g. 

ps_layout-stylefname = ‘CELLSTYLES’ . 

 
 

Now, let’s solidify the procedure by code parts below. We want our column “

SEATSMAX

” entirely 

editable except the case “

CARRID

” is ‘XY’ which is a rare case and we want our cells along the column 

‘PLANETYPE’ editable if their respective ‘

CONNID

’ fields contain the value ‘02’. 

 
 

Assume we have added our style table (“

CELLSTYLES

”) to our list data table and tell the layout 

structure about it and we adjust the field catalog so that the column “

SEATSMAX

” has the property “

EDIT

” 

set to ‘X’. 
 
 
 
 
 
 

© 2005 SAP AG 

47 

background image

An Easy Reference for ALV Grid Control

FORM adjust_editables USING pt_list LIKE gt_list[] . 

  DATA ls_listrow LIKE LINE OF pt_list . 

  DATA ls_stylerow TYPE lvc_s_styl . 

  DATA lt_styletab TYPE lvc_t_styl . 

  LOOP AT pt_list INTO ls_listrow . 

    IF ls_listrow-carrid = 'XY' . 

      ls_stylerow-fieldname = 'SEATSMAX' . 

      ls_stylerow-style = cl_gui_alv_grid=>mc_style_disabled . 

      APPEND ls_stylerow TO lt_styletab . 

    ENDIF . 

    IF ls_listrow-connid = '02' . 

      ls_stylerow-fieldname = 'PLANETYPE' . 

      ls_stylerow-style = cl_gui_alv_grid=>mc_style_enabled . 

      APPEND ls_stylerow TO lt_styletab . 

    ENDIF . 

    INSERT LINES OF lt_styletab INTO ls_listrow-cellstyles . 

    MODIFY pt_list FROM ls_listrow . 

  ENDLOOP . 

ENDFORM  

Code Part 33 – 

Conditionally setting fields to be editable or non-editable

 
 

As usual, cell based settings override entire column settings. You can dynamically switch between 

cases in any proper part of your execution. Just fill your inner table as required and refresh table display; 
for entire column settings, set or unset the property “

EDIT

” of the field catalog for the column and reset 

the field catalog using “set_frontend_fieldcatalog”. 
 

As the last condition to be met for editability, you must call the method “

set_ready_for_input

” 

passing ‘1’ to the parameter “

i_ready_for_input

”. 

Using this method you can switch between editable and non-editable mode. As you guess, passing 

‘0’ to the parameter while calling the method, switches to non-editable mode. 
 
 
 
 
 
 
 

© 2005 SAP AG 

48 

background image

An Easy Reference for ALV Grid Control

 
 

Controlling Data Changes 

 
 

As we can now make our ALV Grid editable we may require controlling input data. The ALV Grid has 

events “

data_changed

” and “

data_changed_finished

”. The former method is triggered just after the 

change at an editable field is perceived. Here you can make checks for the input. And the second event is 
triggered after the change is committed. 
 
  You can select the way how the control perceives data changes by using the method 
“

register_edit_event

”. You have two choices: 

i. 

After return key is pressed: To select this way, to the parameter “

i_event_id

” pass 

“

cl_gui_alv_grid=>mc_evt_enter

”. 

ii. 

After the field is modified and the cursor is moved to another field: 

For 

this, 

pass 

“

cl_gui_alv_grid=>mc_evt_modifies

” to the same parameter. 

 
 
 

To make events controlling data changes be triggered, you must select either way by calling this 

method. Otherwise, these events will not be triggered. 
 
  To control field data changes, ALV Grid uses an instance of the class 
“

CL_ALV_CHANGED_DATA_PROTOCOL

” and passes this via the event “

data_changed

”. Using methods 

of this class, you can get and modify cell values and produce error messages. Here are some of those 
methods: 
 

get_cell_value 

Gets the cell value. You pass the address of the cell to 
the interface. 

modify_cell 

Modifies the cell value addressed via parameters. 

add_protocol_entry 

Add a log entry. You make use of standard message 
interface with message type, message id, etc… 

protocol_is_visible 

Make the error table visible or not. 

refresh_protocol 

Refreshing log entries. 

Table 16 – 

Methods to use for controlling data changes

 
 
 

With the reference of the instance, you can reach information about modifications. These useful 

attribute tables are: 
 

MT_MOD_CELLS 

Contains addresses of modified cells with “

row_id

”s 

and “

fieldname

”s. 

MP_MOD_ROWS 

Contains modified rows. Its type is generic. 

MT_GOOD_CELLS 

Contains cells having proper values 

MT_DELETED_ROWS 

Contains rows deleted from the list 

MT_INSERTED_ROWS 

Contains rows inserted to the list 

Table 17 – 

Attribute tables to be used for controlling data changes

 
 

Utilizing these methods and attributes you can check and give proper message and also modify the 

cell content. 
 
 
 
 
 
 
 

© 2005 SAP AG 

49 

background image

An Easy Reference for ALV Grid Control

FORM handle_data_changed USING ir_data_changed 

                         TYPE REF TO cl_alv_changed_data_protocol. 

  DATA : ls_mod_cell  TYPE lvc_s_modi  , 

         lv_value      TYPE lvc_value  . 

  SORT ir_data_changed->mt_mod_cells BY row_id . 

  LOOP AT ir_data_changed->mt_mod_cells 

                         INTO ls_mod_cell 

                         WHERE fieldname = 'SEATSMAX' . 

    CALL METHOD ir_data_changed->get_cell_value 

                  EXPORTING i_row_id     =  ls_mod_cell-row_id 

                            i_fieldname  =  'CARRID' 

                  IMPORTING e_value      =  lv_value . 

    IF lv_value = 'THY' AND ls_mod_cell-value > '500' . 

      CALL METHOD ir_data_changed->add_protocol_entry 

           EXPORTING 

             i_msgid = 'SU' 

             i_msgno = '000' 

             i_msgty = 'E' 

             i_msgv1 = 'This number can not exceed 500 for ' 

             i_msgv2 = lv_value 

             i_msgv3 = 'The value is et to ''500''' 

             i_fieldname = ls_mod_cell-fieldname 

             i_row_id = ls_mod_cell-row_id . 

      CALL METHOD ir_data_changed->modify_cell 

             EXPORTING i_row_id    = ls_mod_cell-row_id 

                       i_fieldname = ls_mod_cell-fieldname 

                       i_value     = '500' . 

    ENDIF . 

  ENDLOOP . 

ENDFORM

" handle data changed

Code Part 34 – 

Checking the input together with anon-input value, adding a log and modifying the cell 

content

© 2005 SAP AG 

50 

Источник: https://files.student-it.ru/previewfile/15386