ABAP校验ALV输入的内容是否为数字的思路
需要对输入的数字进行合法性校验,并自动计算结果。
1.fieldcat中对应字段要设置为可编辑。
1 |
ls_fieldcat-edit = 'X'. |
2.ALV中启用event参数,并给event参数写changed事件的处理逻辑。
1)ALV中使用event参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
form fm_alv_out . check <fs_table> is assigned and <fs_table> is not initial. data lv_title type lvc_title. data(lw_layout) = value lvc_s_layo( cwidth_opt = abap_true box_fname = 'BOX_NAME' info_fname = 'COLOR' ). data(lt_event) = value slis_t_event( ( name = slis_ev_caller_exit_at_start form = 'FM_CALLER' ) ( name = slis_ev_data_changed form = 'FM_CHANGED' ) ). perform fm_fldcat tables gt_fcat using <fs_table> . call function 'REUSE_ALV_GRID_DISPLAY_LVC' exporting i_callback_program = sy-repid i_callback_pf_status_set = 'FM_ALV_STATUS' i_callback_user_command = 'FM_ALV_UCOMM' is_layout_lvc = lw_layout it_fieldcat_lvc = gt_fcat it_events = lt_event i_save = '' "不许设置变式 i_default = '' "不使用任何变式 i_callback_html_top_of_page = 'FM_ALV_TOP' i_html_height_top = 12 tables t_outtab = <fs_table> exceptions program_error = 1 others = 2. if sy-subrc <> 0. message id sy-msgid type sy-msgty number sy-msgno with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. endif. endform. |
2)change事件的处理逻辑
FM_CHANGED和FM_CALLER
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
form fm_caller using ls_data type slis_data_caller_exit. call function 'GET_GLOBALS_FROM_SLVC_FULLSCR' importing e_grid = go_grid. * 设置回车事件 call method go_grid->register_edit_event exporting i_event_id = cl_gui_alv_grid=>mc_evt_enter exceptions error = 1 others = 2. call method go_grid->set_ready_for_input exporting i_ready_for_input = 1. * 设置光标焦点移开被修改单元格后触发事件 call method go_grid->register_edit_event exporting i_event_id = cl_gui_alv_grid=>mc_evt_modified exceptions error = 1 others = 2. endform. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
form fm_changed using u_data_changed type ref to cl_alv_changed_data_protocol. field-symbols: <dyn_wa>, <source>, <value>. data: ls_layout type lvc_s_layo. data: lv_fieldname type fieldname, lv_text type string. data: lv_hkont type bseg-hkont. call function 'GET_GLOBALS_FROM_SLVC_FULLSCR' importing e_grid = go_grid. ** Recored for save loop at u_data_changed->mt_mod_cells into data(lw_mod_cells). case lw_mod_cells-fieldname. when 'ZKKJE'. "自动计算优惠金额. 优惠金额 = 余额 - 扣款金额. read table <fs_table> assigning <dyn_wa> index lw_mod_cells-row_id. if sy-subrc = 0. assign component 'BANLC' of structure <dyn_wa> to field-symbol(<banlc>). assign component 'ZYHJE' of structure <dyn_wa> to field-symbol(<yhje1>). assign component 'COLOR' of structure <dyn_wa> to field-symbol(<color1>). assign component 'ZMESG' of structure <dyn_wa> to field-symbol(<zmesg1>). assign component 'ICONNAME' of structure <dyn_wa> to field-symbol(<iconname1>). replace all occurrences of ',' in lw_mod_cells-value with ''. "移除千分位符号 replace all occurrences of ' ' in lw_mod_cells-value with ''. "移除空格 condense lw_mod_cells-value no-gaps. "输入数字合法性校验 perform fm_val_numc using lw_mod_cells-value changing <zmesg1>. if <zmesg1> is not initial. <color1> = 'C600'. <iconname1> = '@0A@'. else. clear <color1>. clear <zmesg1>. clear <iconname1>. <yhje1> = <banlc> - lw_mod_cells-value. endif. endif. when others. endcase. endloop. * 刷新屏幕 data(is_stable) = value lvc_s_stbl( row = abap_true col = abap_true ). go_grid->refresh_table_display( exporting is_stable = is_stable exceptions finished = 1 others = 2 ). "屏幕刷新之后自动优化列宽. call method go_grid->get_frontend_layout importing es_layout = ls_layout. ls_layout-cwidth_opt = 'X'. call method go_grid->set_frontend_layout exporting is_layout = ls_layout. endform. |
3)输入字符是否为数字的校验逻辑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
form fm_val_numc using i_char type lvc_s_modi-value changing o_msg type zfit084b-zmesg. data: lv_number type acdoca-hsl. if i_char is initial. return. endif. if cl_abap_matcher=>matches( pattern = '^-?[0-9]+(.[0-9]+)?$' text = i_char ) = abap_true. o_msg = ''. else. o_msg = i_char && '不是一个有效的数字.'. * return. endif. "双保险. * try . * lv_number = i_char. * catch cx_sy_conversion_no_number . * o_msg = i_char && '不是一个有效的数字.'. ** return. * endtry. endform. |
如若转载,请注明出处:https://www.gavindong.com/3135.html