Skip to Content

This is a very short and sweet explanation of scrolling your view to an element and also setting the focus to an element.  I was asked several times how to do this, so I thought I’d put a short document together.  I have found that you often want to scroll into view and set the focus of elements especially when doing dynamic programming.  For example, if you are building elements on a screen dynamically, you may always want to make sure that the last dynamic element created is visible on the screen.  Note: if you have a scrolling screen (you might have configured scrolling on the rootuielementcontainer), and you are adding dynamic elements to your view, you won’t want to scroll to the next dynamic control created. Rather, create an invisible static control at the end of the view.  I do this because depending on the type of dynamic control being created, you might only get the very top of it displayed if you scroll to it.  I prefer to create the invisible control (statically) and place it at the bottom of the screen and scroll to it.  This always makes sure that my last dynamically created element is in view.

Example:

  

I created a view with a scrollable rootuielementcontainer:

/wp-content/uploads/2013/02/pic__183877.jpg

I then added a transparentcontainer (TC_QUESTIONS) where I will build dynamic controls:

/wp-content/uploads/2013/02/pic__183877.jpg

I then add an invisible horizontalgutter control for scrolling to:

/wp-content/uploads/2013/02/pic__183877.jpg

Code:

“Insert code here for building dynamic controls (not shown)

“Code below is for scrolling and setting focus

data lr_elem type ref to if_wd_view_element.
data lr_bottom_elem type ref to if_wd_view_element.

“Scroll to Invisible Control

          try.
              lr_bottom_elem
= view->get_element( ‘HG_BOTTOM’  ).
             
if lr_bottom_elem is bound.
                view
->scroll_into_view( lr_bottom_elem ).
             
endif.
           
catch cx_root into wd_this->g_cxroot.
         
endtry.

“Set Focus on Control

          lr_elem = view->get_element( ‘ID_OF_CONTROL’ ).
         
if lr_elem is bound.
            view
->request_focus_on_view_elem( lr_elem ).
         
endif.

Notes:

In the WDDOMODIFYVIEW, you can set a pointer to your view so it is globally available:

define the view attribute: G_VIEW RefTo IF_WD_VIEW

  if first_time = abap_true.
    try.
        wd_this->g_view ?= view.
      catch cx_root into wd_comp_controller->g_cxroot.

    endtry.
  endif.

then in any of you view methods, you can use the scroll and focus like:

    wd_this->g_view->scroll_into_view( lr_scroll_to_elem ).
    wd_this->g_view->request_focus_on_view_elem( lr_scroll_to_elem ).

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply