abap lvc_alv 定制双击事件, 实现双击后,动态控制字段的编辑状态

实现需求:alv 中的可编辑字段,通过鼠标双击,如果该字段的当前状态是 可编辑的,双击后变为不可编辑,然后在双击的话,有变回可编辑状态。

 

 

OO alv 用户可以自己定义事件,并进行相应的控制。

 

首先自声明:class cl_event_receiver definition  deferred.  "for event handling

 

定一个class:

class cl_event_receiver definition .
public  section .
methods : catch_doubleclick                              "双击时间 方法
         for  event  double_click of  cl_gui_alv_grid     
         importing                                       "传入相应参数
            e_column                                     "选中的列
            es_row_no                                    "选中的行数(table)
            sender.                                      "触发该事件的对象

endclass .

 

对该class实例:

*注: typesbegin  of gs_outtab .

*       typescheckbox  type  c .

*       types : celltab  type  lvc_t_styl.

*       include  structure     sflight.

*       typesend  of  gs_outtab.

data it_data type standard table of gs_outtab.

data wa_data type gs_outtab.

 

 

class cl_event_receiver implementation .
method  catch_doubleclick.
  data : ls_outtab type   gs_outtab,                        "出力的对象data的结构
        ls_celltab type  lvc_s_styl.
  if  e_column-fieldname ne  'CHECKBOX' .                    "选择的该列为想要控制的列名
    exit .
  endif .


  read  table it_data into wa_data index  es_row_no-row_id.  "从出力的内表中选择选中的那行数据
 

  loop  at wa_data -celltab into  ls_celltab.
    if  ls_celltab-fieldname eq  'CHECKBOX' .             "只有双击 checkbox字段时,才能进行控制
      if  ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.   "当该字段为可编辑的时候
         ls_celltab-style = cl_gui_alv_grid=>mc_style_disabled.  "设置该字段为不可编辑
      else .                                                     
"当该字段为不可编辑的时候
         ls_celltab-style = cl_gui_alv_grid=>mc_style_enabled.   "设置该字段为可编辑
      endif .
    

      modify  ls_outtab-celltab from wa_data .
    endif .
  endloop .
 

  modify it_data from wa_data index  es_row_no-row_id.  


  call  method  sender->refresh_table_display.                      "重新刷新数据    
endmethod .

 

 

实现方法:

  data:event_receiver type  ref  to cl_event_receiver


  call  method cl_grid->set_table_for_first_display
       exporting  is_layout             = .......
                 it_toolbar_excluding  =
.......

                                  .......
       changing   it_fieldcatalog       = .......
                 it_outtab             = it_data

                 ....... .

 

 

*  创建一个实例对象

  create  object event_receiver .
  set  handler
event_receiver ->catch_doubleclick for cl_grid.

 

 

一下test结果图:

双击前:

 

双击该处后:已变灰

 

再双击该处:又变回编辑状态

 

 

 

 

 

详细代码实现参考abap(SE38)示例程序:BCALV_EDIT_05.

 

 

 

 

 

 

 

 

 

 

 


你可能感兴趣的:(layout,table,OO,Class,Types)