标准程序 bcalv_edit_05 提供了alv使用checkbox和各种操作。 下面程序将bcalv_edit_05 简化了。。要运行程序,需要自己创建 screen和GUI-status,GUI-title...
REPORT ZWTEST.
data: ok_code like sy-ucomm,
save_ok like sy-ucomm,
g_container type scrfname value 'PACKINGLIST_GRID_0100', "这个是屏幕控件名
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.
*
*§A1.Extend your output table by a checkbox field.
* If you do not want to lock sole checkboxes against input
* you do not need field 'celltab'.
types: begin of gs_outtab.
types: checkbox type c. "field for checkbox
* §B1.Extend your output table by a field to dis- or enable
* cells for input.
types: celltab type lvc_t_styl. "field to switch editability
include structure sflight.
types: end of gs_outtab.
data: gt_outtab type gs_outtab occurs 0 with header line.
*
********************************************************************
*---------------------------------------------------------------------*
* 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.
endif.
endmodule.
*---------------------------------------------------------------------*
* MODULE PAI INPUT *
*---------------------------------------------------------------------*
module pai input.
save_ok = ok_code.
clear ok_code.
case save_ok.
when 'EXIT'.
perform exit_program.
when 'SELECT'.
perform select_all_entries changing gt_outtab[].
when 'DESELECT'.
perform deselect_all_entries changing gt_outtab[].
when 'RESET'.
perform reset_selected_entries changing gt_outtab[].
endcase.
endmodule.
*---------------------------------------------------------------------*
* FORM EXIT_PROGRAM *
*---------------------------------------------------------------------*
form exit_program.
leave program.
endform.
*&---------------------------------------------------------------------*
*& 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 = 'SFLIGHT'
changing
ct_fieldcat = pt_fieldcat.
*§A2.Add an entry for the checkbox in the fieldcatalog
clear ls_fcat.
ls_fcat-fieldname = 'CHECKBOX'.
* Essential: declare field as checkbox and
* mark it as editable field:
ls_fcat-checkbox = 'X'.
ls_fcat-edit = 'X'.
* do not forget to provide texts for this extra field
ls_fcat-coltext = text-f01.
ls_fcat-tooltip = text-f02.
ls_fcat-seltext = text-f03.
* optional: set column width
ls_fcat-outputlen = 10.
*
append ls_fcat to pt_fieldcat.
endform.
*&---------------------------------------------------------------------*
*& Form CREATE_AND_INIT_ALV
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_GT_OUTTAB text
* <--P_GT_FIELDCAT text
* <--P_GS_LAYOUT text
*----------------------------------------------------------------------*
form create_and_init_alv.
data: lt_exclude type ui_functions.
create object g_custom_container
exporting container_name = g_container.
create object g_grid
exporting i_parent = g_custom_container.
perform build_fieldcat changing gt_fieldcat.
* Exclude all edit functions in this example since we do not need them:
perform exclude_tb_functions changing lt_exclude.
perform build_data.
*§ B3.Use the layout structure to aquaint additional field to ALV.
gs_layout-stylefname = 'CELLTAB'.
call method g_grid->set_table_for_first_display
exporting is_layout = gs_layout
it_toolbar_excluding = lt_exclude
changing it_fieldcatalog = gt_fieldcat
it_outtab = gt_outtab[].
* Set editable cells to ready for input initially
call method g_grid->set_ready_for_input
exporting
i_ready_for_input = 1.
endform. "CREATE_AND_INIT_ALV
*&---------------------------------------------------------------------*
*& Form EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_LT_EXCLUDE text
*----------------------------------------------------------------------*
form exclude_tb_functions changing pt_exclude type ui_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 build_data
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form build_data.
data: lt_sflight type table of sflight,
ls_sflight type sflight,
ls_celltab type lvc_s_styl,
lt_celltab type lvc_t_styl,
l_index type i.
select * from sflight into table lt_sflight up to g_max rows.
if sy-subrc ne 0.
* generate own entries if db-table is empty so that this example
* still works
perform generate_entries changing lt_sflight.
endif.
*§A3.Optionally, check checkboxes initially after selecting data.
* (Omitted in this example)
loop at lt_sflight into ls_sflight.
move-corresponding ls_sflight to gt_outtab.
* if gt_outtab-connid eq '400'.
* gt_outtab-checkbox = 'X'.
* endif.
append gt_outtab.
endloop.
* §B2.After selecting data,
* assign a style for each row of your checkbox column.
*
* Initially, set all checkbox cells editable.
ls_celltab-fieldname = 'CHECKBOX'.
ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
loop at gt_outtab.
l_index = sy-tabix.
refresh lt_celltab.
ls_celltab-fieldname = 'CHECKBOX'.
ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.
insert ls_celltab into table lt_celltab.
insert lines of lt_celltab into table gt_outtab-celltab.
modify gt_outtab index l_index.
endloop.
endform. " build_data
*&---------------------------------------------------------------------*
*& Form generate_entries
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_LT_SLFIGHT text
*----------------------------------------------------------------------*
form generate_entries changing pt_slfight type standard table.
*
* This form is only needed for the case that there is no
* data in database table SFLIGHT.
*
data: ls_sflight type sflight,
l_month(2) type c,
l_day(2) type c,
l_date(8) type c.
ls_sflight-carrid = 'LH'.
ls_sflight-connid = '0400'.
ls_sflight-currency = 'DEM'.
ls_sflight-planetype = '747-400'.
ls_sflight-seatsmax = 660.
do 110 times.
ls_sflight-price = sy-index * 100.
ls_sflight-seatsocc = 660 - sy-index * 6.
ls_sflight-paymentsum = ls_sflight-seatsocc * ls_sflight-price.
l_month = sy-index / 10 + 1.
do 2 times.
l_day = l_month + sy-index * 2.
l_date+0(4) = '2000'.
l_date+4(2) = l_month+0(2).
l_date+6(2) = l_day+0(2).
ls_sflight-fldate = l_date.
append ls_sflight to pt_slfight.
enddo.
enddo.
endform. " generate_entries
*&---------------------------------------------------------------------*
*& Form select_all_entries
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_GT_OUTTAB text
*----------------------------------------------------------------------*
form select_all_entries changing pt_outtab type standard table.
data: ls_outtab type gs_outtab.
data: l_valid type c.
*§A4ad. Before you (a)set, reset or (d)evaluate checkboxes,
* you must check the input cells.
*
* If all entries are ok, ALV transferes new values to the output
* table which you then can modify.
call method g_grid->check_changed_data
importing
e_valid = l_valid.
if l_valid eq 'X'.
loop at pt_outtab into ls_outtab.
if not ls_outtab-checkbox eq '-'.
ls_outtab-checkbox = 'X'.
endif.
modify pt_outtab from ls_outtab.
endloop.
call method g_grid->refresh_table_display.
endif.
endform. " select_all_entries
*&---------------------------------------------------------------------*
*& Form deselect_all_entries
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_GT_OUTTAB[] text
*----------------------------------------------------------------------*
form deselect_all_entries changing pt_outtab type standard table.
data: ls_outtab type gs_outtab.
data: l_valid type c,
l_locked type c.
call method g_grid->check_changed_data
importing
e_valid = l_valid.
if l_valid eq 'X'.
loop at pt_outtab into ls_outtab.
perform check_lock using ls_outtab
changing l_locked.
if l_locked is initial
and not ls_outtab-checkbox eq '-'.
ls_outtab-checkbox = ' '.
endif.
modify pt_outtab from ls_outtab.
endloop.
call method g_grid->refresh_table_display.
endif.
endform. " deselect_all_entries
*&---------------------------------------------------------------------*
*& Form reset_selected_entries
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_GT_OUTTAB[] text
*----------------------------------------------------------------------*
form reset_selected_entries changing pt_outtab type standard table.
data: ls_outtab type gs_outtab.
data: l_valid type c.
endform. " reset_selected_entries
*&---------------------------------------------------------------------*
*& Form check_lock
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_LS_OUTTAB text
* <--P_L_LOCKED text
*----------------------------------------------------------------------*
form check_lock using ps_outtab type gs_outtab
changing p_locked.
data ls_celltab type lvc_s_styl.
loop at ps_outtab-celltab into ls_celltab.
if ls_celltab-fieldname = 'CHECKBOX'.
if ls_celltab-style eq cl_gui_alv_grid=>mc_style_disabled.
p_locked = 'X'.
else.
p_locked = space.
endif.
endif.
endloop.
endform. " check_lock