原文链接:https://abapblog.com/articles/tricks/22-refresh-alv-grid-and-keep-position-and-current-cell
当您在编辑模式下使用 CL_GUI_ALV_GRID
或更改用于在 ALV 网格上显示数据的内部表时,可能会发生这样的情况:使用 refresh_table_display
刷新网格后,光标或滚动条会转到网格的起始位置。在这种情况下,用户会感到有些失落,但有一个简单的解决方案可以解决这个问题。
data: is_stable type lvc_s_stbl.
is_stable-row = 'X'.
is_stable-col = 'X'.
"or
is_stable = 'XX'.
grid->refresh_table_display(
exporting
is_stable = is_stable " With Stable Rows/Columns
* i_soft_refresh = i_soft_refresh " Without Sort, Filter, etc.
exceptions
finished = 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.
编辑:您可以在下面找到我的方法,但不一定非要使用它才能实现您的目标。老实说,我已经不记得为什么要创建一个单独的版本了,因为只有光标的行为与最初的版本有些不同。我当时一定是心神不宁:-) . 请原谅我。
因此,为了保持滚动位置和当前单元格,我们需要使用 CL_GUI_ALV_GRID
的以下方法:
- get_scroll_info_via_id - gets current scroll informatio
- get_current_cell - gets current cell, we will use that data to set the cursor at the en
- get_selected_rows - gets selected row
- get_selected_cells_id - if we didn’t select any rows, we check for the selected cell informatio
- refresh_table_display - then simple refresh of gri
- set_selected_cells_id - after refresh it’s time to set back all information, so set selected cel
- set_selected_rows - or set seleted rows (depanding what we received at the begining
- set_scroll_info_via_id - set scroll position bac
- set_current_cell_via_id - set cursor back.
这里没有特殊的编码,只是按照正确的顺序使用标准的 ALV 网格方法。
方法定义,导入参数:
I_SOFT TYPE CHAR01 DEFAULT 'X'
I_SET_CURRENT TYPE CHAR01 DEFAULT SPACE
I_SET_SELECTED TYPE CHAR01 DEFAULT SPACE
Changing 参数:
GRID TYPE REF TO CL_GUI_ALV_GRID
代码实现部分:
method grid_refresh_and_keep_position.
data: es_row_no type lvc_s_roid.
data: es_row_info type lvc_s_row.
data: es_col_info type lvc_s_col.
data: fes_row_no type lvc_s_roid.
data: fes_row_id type lvc_s_row.
data: fes_col_id type lvc_s_col.
data: mt_cells type lvc_t_ceno.
data: mt_rows type lvc_t_row.
grid->get_scroll_info_via_id(
importing
es_row_no = es_row_no
es_row_info = es_row_info
es_col_info = es_col_info
).
grid->get_current_cell(
importing
* e_row = e_row
* e_value = e_value
* e_col = e_col
es_row_id = fes_row_id
es_col_id = fes_col_id
es_row_no = fes_row_no
).
grid->get_selected_rows(
importing
et_index_rows = mt_rows
* et_row_no = et_row_no
).
if mt_rows[] is initial.
grid->get_selected_cells_id(
importing et_cells = mt_cells ).
endif.
grid->refresh_table_display( i_soft_refresh = i_soft ).
if i_set_selected eq 'X'.
if mt_cells[] is not initial.
grid->set_selected_cells_id( it_cells = mt_cells ).
else.
grid->set_selected_rows(
it_index_rows = mt_rows
* it_row_no = it_row_no
* is_keep_other_selections = is_keep_other_selections
).
endif.
endif.
grid->set_scroll_info_via_id(
is_row_info = es_row_info
is_col_info = es_col_info
is_row_no = es_row_no
).
if i_set_current eq 'X'.
grid->set_current_cell_via_id( is_row_id = fes_row_id
is_column_id = fes_col_id
is_row_no = fes_row_no ).
endif.
refresh: mt_rows[], mt_cells[].
endmethod.