06-25-2008 3:36 PM
I have created an ALV grid with a dropdown as one of the columns. This all works fine, except that I want to be able to react to a change in the value of each line's dropdown.
Currently I am reacting to the DATA_CHANGED event of the ALV grid, but that is too late. This event is only triggered if you change the value of the dropdown and then hit enter, or click on another cell or row.
I want to be able to react to a change in the value immediately without having to lose focus of the dropdown.
Is this possible?
As an example, I have a table of records with one column as a dropdown called "Reason". A change in "reason" should change which fields in the row are editable. I'm sure I could just make sure to call the check_changed_data method before saving but I am hoping for a more user-friendly approach where as the new value is selected, the columns in the row can change to their required editable status.
Of course points will be awarded.
07-17-2008 7:55 AM
Hi David,
Instead of using delayed call back event here i hav used data_changed event and this exacty suits ur requirement.....
even here we have to register the edit events............
PROGRAM bcalv_edit_06.
*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
* Purpose:
* ~~~~~~~~
* This example shows how to define a dropdown listbox for all cells
* of one column in an editable ALV Grid Control.
*-----------------------------------------------------------------
* To check program behavior
* ~~~~~~~~~~~~~~~~~~~~~~~~~
* Klick on the dropdown button of column 'WUNIT'. It shows
* 'KG' and 'G' as suitable units for luggage weight.
* (The standard F4-Help shows many other units that does not
* make sense in this context).
*-----------------------------------------------------------------
* Essential steps (search for '§')
* ~~~~~~~~~~~~~~~
* 1.Define a dropdown table and pass it to ALV.
* 2.Set status of column WUNIT to editable and set a dropdown handle.
*-----------------------------------------------------------------------
*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
CLASS lcl_event_responder DEFINITION.
PUBLIC SECTION.
METHODS refresh_changed_data FOR EVENT data_changed
OF cl_gui_alv_grid
IMPORTING er_data_changed
e_ucomm.
ENDCLASS. "event_responder DEFINITION
DATA: ok_code LIKE sy-ucomm,
save_ok LIKE sy-ucomm,
g_container TYPE scrfname VALUE 'BCALV_GRID_DEMO_0100_CONT1',
handler TYPE REF TO lcl_event_responder,
g_grid TYPE REF TO cl_gui_alv_grid,
g_custom_container TYPE REF TO cl_gui_custom_container,
gt_fieldcat TYPE lvc_t_fcat,
gs_layout TYPE lvc_s_layo,
g_max TYPE i VALUE 100.
DATA: gt_outtab TYPE TABLE OF sbook.
*---------------------------------------------------------------------*
* MAIN *
*---------------------------------------------------------------------*
END-OF-SELECTION.
CALL SCREEN 100.
*---------------------------------------------------------------------*
* MODULE PBO OUTPUT *
*---------------------------------------------------------------------*
MODULE pbo OUTPUT.
SET PF-STATUS 'MAIN100'.
SET TITLEBAR 'MAIN100'.
IF g_custom_container IS INITIAL.
PERFORM create_and_init_alv CHANGING gt_outtab
gt_fieldcat.
ENDIF.
ENDMODULE. "pbo OUTPUT
*---------------------------------------------------------------------*
* MODULE PAI INPUT *
*---------------------------------------------------------------------*
MODULE pai INPUT.
save_ok = ok_code.
CLEAR ok_code.
CASE save_ok.
WHEN 'EXIT'.
PERFORM exit_program.
WHEN OTHERS.
* do nothing
ENDCASE.
ENDMODULE. "pai INPUT
*---------------------------------------------------------------------*
* FORM EXIT_PROGRAM *
*---------------------------------------------------------------------*
FORM exit_program.
LEAVE PROGRAM.
ENDFORM. "exit_program
*&---------------------------------------------------------------------*
*& Form BUILD_FIELDCAT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_GT_FIELDCAT text
*----------------------------------------------------------------------*
FORM build_fieldcat CHANGING pt_fieldcat TYPE lvc_t_fcat.
DATA ls_fcat TYPE lvc_s_fcat.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'SBOOK'
CHANGING
ct_fieldcat = pt_fieldcat.
LOOP AT pt_fieldcat INTO ls_fcat.
IF ls_fcat-fieldname EQ 'WUNIT'.
*§2.Set status of column WUNIT to editable and set a dropdown handle.
ls_fcat-edit = 'X'.
ls_fcat-drdn_hndl = '1'.
ls_fcat-outputlen = 7.
* Field 'checktable' is set to avoid shortdumps that are caused
* by inconsistend data in check tables. You may comment this out
* when the test data of the flight model is consistent in your system.
ls_fcat-checktable = '!'. "do not check foreign keys
MODIFY pt_fieldcat FROM ls_fcat.
ENDIF.
ENDLOOP.
ENDFORM. "build_fieldcat
*&---------------------------------------------------------------------*
*& Form CREATE_AND_INIT_ALV
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_GT_OUTTAB text
* <--P_GT_FIELDCAT text
* <--P_GS_LAYOUT text
*----------------------------------------------------------------------*
FORM create_and_init_alv CHANGING pt_outtab LIKE gt_outtab[]
pt_fieldcat TYPE lvc_t_fcat.
DATA: lt_exclude TYPE ui_functions,
lt_f4 TYPE lvc_t_f4 WITH HEADER LINE.
CREATE OBJECT g_custom_container
EXPORTING container_name = g_container.
CREATE OBJECT g_grid
EXPORTING i_parent = g_custom_container.
* Build fieldcat and set column WUNIT
* edit enabled. Assign a handle for the dropdown listbox.
PERFORM build_fieldcat CHANGING pt_fieldcat.
* Optionally restrict generic functions to 'change only'.
* (The user shall not be able to add new lines).
PERFORM exclude_tb_functions CHANGING lt_exclude.
* Define a drop down table.
PERFORM set_drdn_table.
SELECT * FROM sbook INTO TABLE pt_outtab UP TO g_max ROWS.
IF sy-subrc NE 0.
* generate own entries if database table is empty
PERFORM generate_entries CHANGING pt_outtab.
ENDIF.
CALL METHOD g_grid->set_table_for_first_display
EXPORTING
it_toolbar_excluding = lt_exclude
CHANGING
it_fieldcatalog = pt_fieldcat
it_outtab = pt_outtab.
* Set editable cells to ready for input initially
CALL METHOD g_grid->set_ready_for_input
EXPORTING
i_ready_for_input = 1.
g_grid->register_edit_event(
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_modified ). "Registering edit events
CREATE OBJECT handler.
SET HANDLER handler->refresh_changed_data FOR g_grid.
CLEAR lt_f4.
lt_f4-fieldname = 'WUNIT'.
lt_f4-register = 'X'.
APPEND lt_f4.
ENDFORM. "CREATE_AND_INIT_ALV
*&---------------------------------------------------------------------*
*& Form EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_LT_EXCLUDE text
*----------------------------------------------------------------------*
FORM exclude_tb_functions CHANGING pt_exclude TYPE ui_functions.
* Only allow to change data not to create new entries (exclude
* generic functions).
DATA ls_exclude TYPE ui_func.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_delete_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_append_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_insert_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_move_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_cut.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste_new_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_undo.
APPEND ls_exclude TO pt_exclude.
ENDFORM. " EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
*& Form set_drdn_table
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM set_drdn_table.
*§1.Define a dropdown table and pass it to ALV.
* One listbox is referenced by a handle, e.g., '1'.
* For each entry that shall appear in this listbox
* you have to append a line to the dropdown table
* with handle '1'.
* This handle can be assigned to several columns
* of the output table using the field catalog.
*
DATA: lt_dropdown TYPE lvc_t_drop,
ls_dropdown TYPE lvc_s_drop.
* First listbox (handle '1').
ls_dropdown-handle = '1'.
ls_dropdown-value = 'KG'.
APPEND ls_dropdown TO lt_dropdown.
ls_dropdown-handle = '1'.
ls_dropdown-value = 'G'.
APPEND ls_dropdown TO lt_dropdown.
CALL METHOD g_grid->set_drop_down_table
EXPORTING
it_drop_down = lt_dropdown.
ENDFORM. " set_drdn_table
*&---------------------------------------------------------------------*
*& Form generate_entries
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_LT_SLFIGHT text
*----------------------------------------------------------------------*
FORM generate_entries CHANGING pt_sbook TYPE STANDARD TABLE.
*
* This form is only needed if database table sbook is empty.
* It generates some entries so that you may
* still try out this example program.
*
DATA: ls_sbook TYPE sbook,
l_month(2) TYPE c,
l_day(2) TYPE c,
l_date(8) TYPE c,
l_prebookid TYPE i.
ls_sbook-carrid = 'LH'.
ls_sbook-connid = '0400'.
ls_sbook-forcurkey = 'DEM'.
ls_sbook-loccurkey = 'USD'.
ls_sbook-custtype = 'B'.
DO 110 TIMES.
l_prebookid = sy-index.
ls_sbook-forcuram = sy-index * 10.
ls_sbook-loccuram = ls_sbook-loccuram * 2.
ls_sbook-customid = sy-index.
ls_sbook-counter = 18.
ls_sbook-agencynum = 11.
l_month = sy-index / 10 + 1.
DO 2 TIMES.
l_day = 3 + l_month + sy-index * 2.
l_date+0(4) = '2000'.
l_date+4(2) = l_month.
l_date+6(2) = l_day.
ls_sbook-fldate = l_date.
SUBTRACT 3 FROM l_day.
ls_sbook-order_date+0(6) = l_date+0(6).
ls_sbook-order_date+6(2) = l_day.
ls_sbook-bookid = l_prebookid * 2 + sy-index.
IF sy-index EQ 1.
ls_sbook-smoker = 'X'.
ELSE.
ls_sbook-smoker = space.
ENDIF.
ls_sbook-luggweight = l_prebookid * 10.
IF ls_sbook-luggweight GE 1000.
ls_sbook-wunit = 'G'.
ls_sbook-class = 'C'.
ELSE.
ls_sbook-wunit = 'KG'.
ls_sbook-class = 'Y'.
ENDIF.
IF ls_sbook-bookid > 40 AND ls_sbook-wunit EQ 'KG'.
ls_sbook-invoice = 'X'.
ENDIF.
IF ls_sbook-bookid EQ 2.
ls_sbook-cancelled = 'X'.
ls_sbook-class = 'F'.
ENDIF.
APPEND ls_sbook TO pt_sbook.
ENDDO.
ENDDO.
ENDFORM. " generate_entries
*---------------------------------------------------------------------*
* CLASS event_responder IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_event_responder IMPLEMENTATION.
METHOD refresh_changed_data.
BREAK-POINT.
ENDMETHOD. "click
ENDCLASS. "event_responder IMPLEMENTATION
06-26-2008 1:57 PM
Hi,
Kindly check the code below.......
here i hav cloned the std SAP demo prog and added the delayed_callback* event...might be helpful for u
*---------------------------------------------------------------------*
* CLASS lcl_event_responder DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_event_responder DEFINITION.
PUBLIC SECTION.
METHODS click_a FOR EVENT delayed_callback OF cl_gui_alv_grid.
ENDCLASS. "event_responder DEFINITION
DATA: ok_code LIKE sy-ucomm,
save_ok LIKE sy-ucomm,
g_container TYPE scrfname VALUE 'CUSTOM',
g_grid TYPE REF TO cl_gui_alv_grid,
g_custom_container TYPE REF TO cl_gui_custom_container,
handler TYPE REF TO lcl_event_responder,
gt_fieldcat TYPE lvc_t_fcat,
gs_layout TYPE lvc_s_layo,
g_max TYPE i VALUE 100.
DATA: gt_outtab TYPE TABLE OF sbook.
*---------------------------------------------------------------------*
* MAIN *
*---------------------------------------------------------------------*
END-OF-SELECTION.
CALL SCREEN 100.
*---------------------------------------------------------------------*
* MODULE PBO OUTPUT *
*---------------------------------------------------------------------*
MODULE pbo OUTPUT.
SET PF-STATUS 'BASIC'.
IF g_custom_container IS INITIAL.
PERFORM create_and_init_alv CHANGING gt_outtab
gt_fieldcat.
ENDIF.
ENDMODULE. "MODULE
*---------------------------------------------------------------------*
* MODULE PAI INPUT *
*---------------------------------------------------------------------*
MODULE pai INPUT.
CASE sy-ucomm.
WHEN 'EXIT'.
PERFORM exit_program.
WHEN OTHERS.
* do nothing
ENDCASE.
ENDMODULE. "pai INPUT
*---------------------------------------------------------------------*
* FORM EXIT_PROGRAM *
*---------------------------------------------------------------------*
FORM exit_program.
LEAVE PROGRAM.
ENDFORM. "exit_program
*&---------------------------------------------------------------------*
*& Form BUILD_FIELDCAT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_GT_FIELDCAT text
*----------------------------------------------------------------------*
FORM build_fieldcat CHANGING pt_fieldcat TYPE lvc_t_fcat.
DATA ls_fcat TYPE lvc_s_fcat.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'SBOOK'
CHANGING
ct_fieldcat = pt_fieldcat.
LOOP AT pt_fieldcat INTO ls_fcat.
IF ls_fcat-fieldname EQ 'WUNIT'.
*§2.Set status of column WUNIT to editable and set a dropdown handle.
ls_fcat-edit = 'X'.
ls_fcat-drdn_hndl = '1'.
ls_fcat-outputlen = 7.
* Field 'checktable' is set to avoid shortdumps that are caused
* by inconsistend data in check tables. You may comment this out
* when the test data of the flight model is consistent in your system.
ls_fcat-checktable = '!'. "do not check foreign keys
MODIFY pt_fieldcat FROM ls_fcat.
ENDIF.
ENDLOOP.
ENDFORM. "build_fieldcat
*&---------------------------------------------------------------------*
*& Form CREATE_AND_INIT_ALV
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_GT_OUTTAB text
* <--P_GT_FIELDCAT text
* <--P_GS_LAYOUT text
*----------------------------------------------------------------------*
FORM create_and_init_alv CHANGING pt_outtab LIKE gt_outtab[]
pt_fieldcat TYPE lvc_t_fcat.
DATA: lt_exclude TYPE ui_functions,
lt_f4 TYPE lvc_t_f4 WITH HEADER LINE.
CREATE OBJECT g_custom_container
EXPORTING container_name = g_container.
CREATE OBJECT g_grid
EXPORTING i_parent = g_custom_container.
* Build fieldcat and set column WUNIT
* edit enabled. Assign a handle for the dropdown listbox.
PERFORM build_fieldcat CHANGING pt_fieldcat.
* Optionally restrict generic functions to 'change only'.
* (The user shall not be able to add new lines).
PERFORM exclude_tb_functions CHANGING lt_exclude.
* Define a drop down table.
PERFORM set_drdn_table.
SELECT * FROM sbook INTO TABLE pt_outtab UP TO g_max ROWS.
IF sy-subrc NE 0.
* generate own entries if database table is empty
PERFORM generate_entries CHANGING pt_outtab.
ENDIF.
CALL METHOD g_grid->set_table_for_first_display
EXPORTING
it_toolbar_excluding = lt_exclude
CHANGING
it_fieldcatalog = pt_fieldcat
it_outtab = pt_outtab.
* Set editable cells to ready for input initially
CALL METHOD g_grid->set_ready_for_input
EXPORTING
i_ready_for_input = 1.
g_grid->register_delayed_event( i_event_id = cl_gui_alv_grid=>mc_evt_delayed_move_curr_cell ).
CREATE OBJECT handler.
SET HANDLER handler->click_a FOR g_grid.
CLEAR lt_f4.
lt_f4-fieldname = 'WUNIT'.
lt_f4-register = 'X'.
APPEND lt_f4.
ENDFORM. "CREATE_AND_INIT_ALV
*&---------------------------------------------------------------------*
*& Form EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_LT_EXCLUDE text
*----------------------------------------------------------------------*
FORM exclude_tb_functions CHANGING pt_exclude TYPE ui_functions.
* Only allow to change data not to create new entries (exclude
* generic functions).
DATA ls_exclude TYPE ui_func.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_delete_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_append_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_insert_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_move_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_cut.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste_new_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_undo.
APPEND ls_exclude TO pt_exclude.
ENDFORM. " EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
*& Form set_drdn_table
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM set_drdn_table.
*§1.Define a dropdown table and pass it to ALV.
* One listbox is referenced by a handle, e.g., '1'.
* For each entry that shall appear in this listbox
* you have to append a line to the dropdown table
* with handle '1'.
* This handle can be assigned to several columns
* of the output table using the field catalog.
*
DATA: lt_dropdown TYPE lvc_t_drop,
ls_dropdown TYPE lvc_s_drop.
* First listbox (handle '1').
ls_dropdown-handle = '1'.
ls_dropdown-value = 'KG'.
APPEND ls_dropdown TO lt_dropdown.
ls_dropdown-handle = '1'.
ls_dropdown-value = 'G'.
APPEND ls_dropdown TO lt_dropdown.
CALL METHOD g_grid->set_drop_down_table
EXPORTING
it_drop_down = lt_dropdown.
ENDFORM. " set_drdn_table
*&---------------------------------------------------------------------*
*& Form generate_entries
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_LT_SLFIGHT text
*----------------------------------------------------------------------*
FORM generate_entries CHANGING pt_sbook TYPE STANDARD TABLE.
*
* This form is only needed if database table sbook is empty.
* It generates some entries so that you may
* still try out this example program.
*
DATA: ls_sbook TYPE sbook,
l_month(2) TYPE c,
l_day(2) TYPE c,
l_date(8) TYPE c,
l_prebookid TYPE i.
ls_sbook-carrid = 'LH'.
ls_sbook-connid = '0400'.
ls_sbook-forcurkey = 'DEM'.
ls_sbook-loccurkey = 'USD'.
ls_sbook-custtype = 'B'.
DO 110 TIMES.
l_prebookid = sy-index.
ls_sbook-forcuram = sy-index * 10.
ls_sbook-loccuram = ls_sbook-loccuram * 2.
ls_sbook-customid = sy-index.
ls_sbook-counter = 18.
ls_sbook-agencynum = 11.
l_month = sy-index / 10 + 1.
DO 2 TIMES.
l_day = 3 + l_month + sy-index * 2.
l_date+0(4) = '2000'.
l_date+4(2) = l_month.
l_date+6(2) = l_day.
ls_sbook-fldate = l_date.
SUBTRACT 3 FROM l_day.
ls_sbook-order_date+0(6) = l_date+0(6).
ls_sbook-order_date+6(2) = l_day.
ls_sbook-bookid = l_prebookid * 2 + sy-index.
IF sy-index EQ 1.
ls_sbook-smoker = 'X'.
ELSE.
ls_sbook-smoker = space.
ENDIF.
ls_sbook-luggweight = l_prebookid * 10.
IF ls_sbook-luggweight GE 1000.
ls_sbook-wunit = 'G'.
ls_sbook-class = 'C'.
ELSE.
ls_sbook-wunit = 'KG'.
ls_sbook-class = 'Y'.
ENDIF.
IF ls_sbook-bookid > 40 AND ls_sbook-wunit EQ 'KG'.
ls_sbook-invoice = 'X'.
ENDIF.
IF ls_sbook-bookid EQ 2.
ls_sbook-cancelled = 'X'.
ls_sbook-class = 'F'.
ENDIF.
APPEND ls_sbook TO pt_sbook.
ENDDO.
ENDDO.
ENDFORM. " generate_entries
*---------------------------------------------------------------------*
* CLASS lcl_event_responder IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_event_responder IMPLEMENTATION.
METHOD click_a.
BREAK-POINT. "method check_changed_data can be used here
ENDMETHOD. "click
ENDCLASS. "event_responder IMPLEMENTATION
Cheers,
Jose.
07-17-2008 7:55 AM
Hi David,
Instead of using delayed call back event here i hav used data_changed event and this exacty suits ur requirement.....
even here we have to register the edit events............
PROGRAM bcalv_edit_06.
*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
* Purpose:
* ~~~~~~~~
* This example shows how to define a dropdown listbox for all cells
* of one column in an editable ALV Grid Control.
*-----------------------------------------------------------------
* To check program behavior
* ~~~~~~~~~~~~~~~~~~~~~~~~~
* Klick on the dropdown button of column 'WUNIT'. It shows
* 'KG' and 'G' as suitable units for luggage weight.
* (The standard F4-Help shows many other units that does not
* make sense in this context).
*-----------------------------------------------------------------
* Essential steps (search for '§')
* ~~~~~~~~~~~~~~~
* 1.Define a dropdown table and pass it to ALV.
* 2.Set status of column WUNIT to editable and set a dropdown handle.
*-----------------------------------------------------------------------
*&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
CLASS lcl_event_responder DEFINITION.
PUBLIC SECTION.
METHODS refresh_changed_data FOR EVENT data_changed
OF cl_gui_alv_grid
IMPORTING er_data_changed
e_ucomm.
ENDCLASS. "event_responder DEFINITION
DATA: ok_code LIKE sy-ucomm,
save_ok LIKE sy-ucomm,
g_container TYPE scrfname VALUE 'BCALV_GRID_DEMO_0100_CONT1',
handler TYPE REF TO lcl_event_responder,
g_grid TYPE REF TO cl_gui_alv_grid,
g_custom_container TYPE REF TO cl_gui_custom_container,
gt_fieldcat TYPE lvc_t_fcat,
gs_layout TYPE lvc_s_layo,
g_max TYPE i VALUE 100.
DATA: gt_outtab TYPE TABLE OF sbook.
*---------------------------------------------------------------------*
* MAIN *
*---------------------------------------------------------------------*
END-OF-SELECTION.
CALL SCREEN 100.
*---------------------------------------------------------------------*
* MODULE PBO OUTPUT *
*---------------------------------------------------------------------*
MODULE pbo OUTPUT.
SET PF-STATUS 'MAIN100'.
SET TITLEBAR 'MAIN100'.
IF g_custom_container IS INITIAL.
PERFORM create_and_init_alv CHANGING gt_outtab
gt_fieldcat.
ENDIF.
ENDMODULE. "pbo OUTPUT
*---------------------------------------------------------------------*
* MODULE PAI INPUT *
*---------------------------------------------------------------------*
MODULE pai INPUT.
save_ok = ok_code.
CLEAR ok_code.
CASE save_ok.
WHEN 'EXIT'.
PERFORM exit_program.
WHEN OTHERS.
* do nothing
ENDCASE.
ENDMODULE. "pai INPUT
*---------------------------------------------------------------------*
* FORM EXIT_PROGRAM *
*---------------------------------------------------------------------*
FORM exit_program.
LEAVE PROGRAM.
ENDFORM. "exit_program
*&---------------------------------------------------------------------*
*& Form BUILD_FIELDCAT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_GT_FIELDCAT text
*----------------------------------------------------------------------*
FORM build_fieldcat CHANGING pt_fieldcat TYPE lvc_t_fcat.
DATA ls_fcat TYPE lvc_s_fcat.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'SBOOK'
CHANGING
ct_fieldcat = pt_fieldcat.
LOOP AT pt_fieldcat INTO ls_fcat.
IF ls_fcat-fieldname EQ 'WUNIT'.
*§2.Set status of column WUNIT to editable and set a dropdown handle.
ls_fcat-edit = 'X'.
ls_fcat-drdn_hndl = '1'.
ls_fcat-outputlen = 7.
* Field 'checktable' is set to avoid shortdumps that are caused
* by inconsistend data in check tables. You may comment this out
* when the test data of the flight model is consistent in your system.
ls_fcat-checktable = '!'. "do not check foreign keys
MODIFY pt_fieldcat FROM ls_fcat.
ENDIF.
ENDLOOP.
ENDFORM. "build_fieldcat
*&---------------------------------------------------------------------*
*& Form CREATE_AND_INIT_ALV
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_GT_OUTTAB text
* <--P_GT_FIELDCAT text
* <--P_GS_LAYOUT text
*----------------------------------------------------------------------*
FORM create_and_init_alv CHANGING pt_outtab LIKE gt_outtab[]
pt_fieldcat TYPE lvc_t_fcat.
DATA: lt_exclude TYPE ui_functions,
lt_f4 TYPE lvc_t_f4 WITH HEADER LINE.
CREATE OBJECT g_custom_container
EXPORTING container_name = g_container.
CREATE OBJECT g_grid
EXPORTING i_parent = g_custom_container.
* Build fieldcat and set column WUNIT
* edit enabled. Assign a handle for the dropdown listbox.
PERFORM build_fieldcat CHANGING pt_fieldcat.
* Optionally restrict generic functions to 'change only'.
* (The user shall not be able to add new lines).
PERFORM exclude_tb_functions CHANGING lt_exclude.
* Define a drop down table.
PERFORM set_drdn_table.
SELECT * FROM sbook INTO TABLE pt_outtab UP TO g_max ROWS.
IF sy-subrc NE 0.
* generate own entries if database table is empty
PERFORM generate_entries CHANGING pt_outtab.
ENDIF.
CALL METHOD g_grid->set_table_for_first_display
EXPORTING
it_toolbar_excluding = lt_exclude
CHANGING
it_fieldcatalog = pt_fieldcat
it_outtab = pt_outtab.
* Set editable cells to ready for input initially
CALL METHOD g_grid->set_ready_for_input
EXPORTING
i_ready_for_input = 1.
g_grid->register_edit_event(
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_modified ). "Registering edit events
CREATE OBJECT handler.
SET HANDLER handler->refresh_changed_data FOR g_grid.
CLEAR lt_f4.
lt_f4-fieldname = 'WUNIT'.
lt_f4-register = 'X'.
APPEND lt_f4.
ENDFORM. "CREATE_AND_INIT_ALV
*&---------------------------------------------------------------------*
*& Form EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_LT_EXCLUDE text
*----------------------------------------------------------------------*
FORM exclude_tb_functions CHANGING pt_exclude TYPE ui_functions.
* Only allow to change data not to create new entries (exclude
* generic functions).
DATA ls_exclude TYPE ui_func.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_delete_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_append_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_insert_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_move_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_copy.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_cut.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_paste_new_row.
APPEND ls_exclude TO pt_exclude.
ls_exclude = cl_gui_alv_grid=>mc_fc_loc_undo.
APPEND ls_exclude TO pt_exclude.
ENDFORM. " EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
*& Form set_drdn_table
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM set_drdn_table.
*§1.Define a dropdown table and pass it to ALV.
* One listbox is referenced by a handle, e.g., '1'.
* For each entry that shall appear in this listbox
* you have to append a line to the dropdown table
* with handle '1'.
* This handle can be assigned to several columns
* of the output table using the field catalog.
*
DATA: lt_dropdown TYPE lvc_t_drop,
ls_dropdown TYPE lvc_s_drop.
* First listbox (handle '1').
ls_dropdown-handle = '1'.
ls_dropdown-value = 'KG'.
APPEND ls_dropdown TO lt_dropdown.
ls_dropdown-handle = '1'.
ls_dropdown-value = 'G'.
APPEND ls_dropdown TO lt_dropdown.
CALL METHOD g_grid->set_drop_down_table
EXPORTING
it_drop_down = lt_dropdown.
ENDFORM. " set_drdn_table
*&---------------------------------------------------------------------*
*& Form generate_entries
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_LT_SLFIGHT text
*----------------------------------------------------------------------*
FORM generate_entries CHANGING pt_sbook TYPE STANDARD TABLE.
*
* This form is only needed if database table sbook is empty.
* It generates some entries so that you may
* still try out this example program.
*
DATA: ls_sbook TYPE sbook,
l_month(2) TYPE c,
l_day(2) TYPE c,
l_date(8) TYPE c,
l_prebookid TYPE i.
ls_sbook-carrid = 'LH'.
ls_sbook-connid = '0400'.
ls_sbook-forcurkey = 'DEM'.
ls_sbook-loccurkey = 'USD'.
ls_sbook-custtype = 'B'.
DO 110 TIMES.
l_prebookid = sy-index.
ls_sbook-forcuram = sy-index * 10.
ls_sbook-loccuram = ls_sbook-loccuram * 2.
ls_sbook-customid = sy-index.
ls_sbook-counter = 18.
ls_sbook-agencynum = 11.
l_month = sy-index / 10 + 1.
DO 2 TIMES.
l_day = 3 + l_month + sy-index * 2.
l_date+0(4) = '2000'.
l_date+4(2) = l_month.
l_date+6(2) = l_day.
ls_sbook-fldate = l_date.
SUBTRACT 3 FROM l_day.
ls_sbook-order_date+0(6) = l_date+0(6).
ls_sbook-order_date+6(2) = l_day.
ls_sbook-bookid = l_prebookid * 2 + sy-index.
IF sy-index EQ 1.
ls_sbook-smoker = 'X'.
ELSE.
ls_sbook-smoker = space.
ENDIF.
ls_sbook-luggweight = l_prebookid * 10.
IF ls_sbook-luggweight GE 1000.
ls_sbook-wunit = 'G'.
ls_sbook-class = 'C'.
ELSE.
ls_sbook-wunit = 'KG'.
ls_sbook-class = 'Y'.
ENDIF.
IF ls_sbook-bookid > 40 AND ls_sbook-wunit EQ 'KG'.
ls_sbook-invoice = 'X'.
ENDIF.
IF ls_sbook-bookid EQ 2.
ls_sbook-cancelled = 'X'.
ls_sbook-class = 'F'.
ENDIF.
APPEND ls_sbook TO pt_sbook.
ENDDO.
ENDDO.
ENDFORM. " generate_entries
*---------------------------------------------------------------------*
* CLASS event_responder IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_event_responder IMPLEMENTATION.
METHOD refresh_changed_data.
BREAK-POINT.
ENDMETHOD. "click
ENDCLASS. "event_responder IMPLEMENTATION
09-10-2014 11:14 AM
Do one thing ,
In the preparation of events prepare just register this event
ex:
CALL METHOD <grid_reference>->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_modified.