{"id":761,"date":"2013-10-29T23:36:15","date_gmt":"2013-10-29T22:36:15","guid":{"rendered":"http:\/\/oprsteny.cz\/?p=761"},"modified":"2015-02-27T14:51:07","modified_gmt":"2015-02-27T13:51:07","slug":"abap-keep-selection-and-scroll-position-after-alv-refresh","status":"publish","type":"post","link":"https:\/\/oprsteny.cz\/?p=761","title":{"rendered":"ALV &#8211; Keep selection and scroll position after ALV refresh"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"358\" data-permalink=\"https:\/\/oprsteny.cz\/?attachment_id=358\" data-orig-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" data-orig-size=\"44,50\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Picasa&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1365690880&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"SAP\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" class=\"alignleft size-full wp-image-358\" src=\"http:\/\/oprsteny.cz\/wp-content\/uploads\/SAP.jpg\" alt=\"SAP\" width=\"44\" height=\"50\" \/>There is an issue with ALV grid that if you refresh the grid being in edit mode using method REFRESH_TABLE_DISPLAY (in case you use CL_GUI_ALV_GRID) after you have changed some data, it may happen the cursor and\/or scroll goes to the first (top left) cell of the grid. It might be a little confusing for user, but you can avoid this issue by using the following methods (and attached piece of code)<!--more--><\/p>\n<ol>\n<li>GET_SCROLL_INFO_VIA_ID &#8211; gets current scroll information<\/li>\n<li>GET_CURRENT_CELL &#8211; gets current cell, we will use that data to set the cursor at the end<\/li>\n<li>GET_SELECTED_ROWS &#8211; gets selected rows<\/li>\n<li>GET_SELECTED_CELLS_ID &#8211; if we didn&#8217;t select any rows, we check for the selected cell information<\/li>\n<li>REFRESH_TABLE_DISPLAY &#8211; refresh the grid with updated data<\/li>\n<li>SET_SELECTED_CELLS_ID &#8211; set selected cell<\/li>\n<li>SET_SELECTED_ROWS &#8211; or set seleted rows (depanding what we received at the begining)<\/li>\n<li>SET_SCROLL_INFO_VIA_ID &#8211; refresh the previous scroll position<\/li>\n<li>SET_CURRENT_CELL_VIA_ID &#8211; Set cursor on the last selected cell<\/li>\n<\/ol>\n<p>In the below code (class method), there is just an example code of the methods mentioned in the list above.<\/p>\n<p>Method definition &#8211; Importing parameters<\/p>\n<pre lang=\"abap\">i_soft         TYPE xfeld  DEFAULT abap_true \r\ni_set_current  TYPE xfeld  DEFAULT abap_true\r\ni_set_selected TYPE xfeld  DEFAULT abap_false<\/pre>\n<p>&#8230; changing parameters<\/p>\n<pre lang=\"abap\">ir_grid TYPE REF TO cl_gui_alv_grid<\/pre>\n<p>&#8230; and the code implementation itself<\/p>\n<pre lang=\"abap\">METHOD refresh_grid.\r\n  DATA: \r\n    l_scroll_row_no   TYPE lvc_s_roid,\r\n    l_scroll_row_info TYPE lvc_s_row,\r\n    l_scroll_col_info TYPE lvc_s_col,\r\n    l_cell_row_no TYPE lvc_s_roid,\r\n    l_cell_row_id TYPE lvc_s_row,\r\n    l_cell_col_id TYPE lvc_s_col,\r\n\r\n    mt_sel_cells TYPE lvc_t_ceno,\r\n    mt_sel_rows  TYPE lvc_t_row.\r\n\r\n* Save the scroll info\r\n  mr_grid-&gt;get_scroll_info_via_id(\r\n  importing\r\n    es_row_no   = l_scroll_row_no\r\n    es_row_info = l_scroll_row_info\r\n    es_col_info = l_scroll_col_info\r\n    ).\r\n\r\n* Save info about last selected cell\r\n  mr_grid-&gt;get_current_cell(\r\n    IMPORTING\r\n*     e_row     = \r\n*     e_value   = \r\n*     e_col     = \r\n      es_row_id = l_cell_row_id\r\n      es_col_id = l_cell_col_id\r\n      es_row_no = l_cell_row_no\r\n  ).\r\n\r\n* Save info about selected rows\r\n  mr_grid-&gt;get_selected_rows(\r\n    IMPORTING\r\n      et_index_rows = mt_sel_rows\r\n  ).\r\n\r\n* If no row is selected save info about selected cells\r\n  IF mt_sel_rows[] IS INITIAL.\r\n    mr_grid-&gt;get_selected_cells_id(\r\n      IMPORTING\r\n        et_cells = mt_sel_cells \r\n    ).\r\n  ENDIF.\r\n\r\n* ALV Grid refresh\r\n  mr_grid-&gt;refresh_table_display( i_soft_refresh = i_soft ).\r\n\r\n* Restore the saved selection\r\n  IF i_set_selected = abap_true.\r\n    IF mt_sel_cells[] IS NOT INITIAL.\r\n      mr_grid-&gt;set_selected_cells_id( it_cells = mt_sel_cells   ).\r\n    ELSE.\r\n      mr_grid-&gt;set_selected_rows(\r\n        it_index_rows            = mt_sel_rows\r\n*       it_row_no                = \r\n*       is_keep_other_selections = \r\n      ).\r\n    ENDIF.\r\n  ENDIF.\r\n\r\n* Restore previously saved scroll position\r\n  mr_grid-&gt;set_scroll_info_via_id(\r\n    is_row_info = l_scroll_row_info\r\n    is_col_info = l_sroll_col_info\r\n    is_row_no   = l_scroll_row_no\r\n  ).\r\n\r\n* Set focus on previously selected cell\r\n  IF i_set_current = abap_true.\r\n    mr_grid-&gt;set_current_cell_via_id( \r\n      is_row_id    = l_cell_row_id\r\n      is_column_id = l_cell_col_id\r\n      is_row_no    = l_cell_row_no ).\r\n  ENDIF.\r\nENDMETHOD.<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There is an issue with ALV grid that if you refresh the grid being in edit mode using method REFRESH_TABLE_DISPLAY (in case you use CL_GUI_ALV_GRID) after you have changed some data, it may happen the cursor and\/or scroll goes to &hellip; <a href=\"https:\/\/oprsteny.cz\/?p=761\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"ABAP - Keep selection and scroll position position after ALV refresh http:\/\/wp.me\/p3nYbe-ch","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[16,86,9],"tags":[446,447,210,202,201,204,203,205,209,208,206,207],"class_list":["post-761","post","type-post","status-publish","format-standard","hentry","category-abap","category-alv","category-development","tag-abap","tag-alv","tag-cl_gui_alv_grid","tag-get_current_cell","tag-get_scroll_info_via_id","tag-get_selected_cells_id","tag-get_selected_rows","tag-refresh_table_display","tag-set_current_cell_via_id","tag-set_scroll_info_via_id","tag-set_selected_cells_id","tag-set_selected_rows"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3nYbe-ch","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=761"}],"version-history":[{"count":11,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/761\/revisions"}],"predecessor-version":[{"id":1299,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=\/wp\/v2\/posts\/761\/revisions\/1299"}],"wp:attachment":[{"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oprsteny.cz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}