0

I have a sapui5 table and I am trying to log in the console if the table is being scrolled up or down. I can get it to working if I use a regular div.

Example here.

But I can't seem to get it to work for a sapui5 table. I have tried the following:

var lastScroll = 0;
$("#__xmlview0--players-vsb-sb").scroll(function () {
    var st = $(this).scrollTop();
    if (st > lastScroll) {
        console.log("scrolling down");
    } else {
       console.log("scrolling up");
    }
    lastScroll = st;
});

I'm getting the id #__xmlview0--players-vsb-sb when I inspect element on the scrollbar. This seems like the id I should use. Any ideas of how to get this to work?

Here is my JSBin.

Anthony
  • 1,439
  • 1
  • 19
  • 36

1 Answers1

1

If you put the scroll event inside a setTimout, it starts working.

        setTimeout(function(){  


        var lastScroll = 0;
        $("#__xmlview0--players-vsb-sb").scroll(function () {
                var st = $(this).scrollTop();
                if (st > lastScroll) {
                    console.log("scrolling down");
                } else {
                    console.log("scrolling up");
                }
                lastScroll = st;
            });
}, 700);

This is an indication that the UI library is clearing out events on the elements it uses when it initializes its own events on them. This is probably to remove the risk of memory leaks. I would suggest not using the timeout, and seeing this stack for more info: SAPUI5-Which method to call once a view is displayed everytime?

Community
  • 1
  • 1
Radio
  • 2,810
  • 1
  • 21
  • 43
  • This works. Thanks! Is there a downside to using `setTimeout`? – Anthony Jul 22 '15 at 18:31
  • A settimeout to solve this type of issue represents a race condition. If it takes sapi5 longer than 700ms to initialize the table, for example with a very large dataset, the code may not apply the scroll event on time. It's better to use a sap event if possible. – Radio Jul 22 '15 at 18:42