Skyp
Skyp

Reputation: 1197

Knockout: Scrolling to one of newly inserted elements

So I have an asynchronous call to a controller in c# to obtain say 100 objects. After this is done I load it into my html page and through a parameter in the link I intend to scroll to it.

So for instance: localhost/page.aspx?scrollToId=85

I do this as follows:

var selectedItem = $("tr[data-scrollToId='" + selectedItemId() + "']");
$('body').scrollTo(selectedItem );
selectedItem.fadeOut().fadeIn();

This works when I have static elements but because of the asynchronous call the dom is loaded way later and it doesnt scroll to it. (It does fade out and in again).

I have been looking at the afterRender method of knockout itself but this too didn't give any change.

Is there a way to wait for the actual dom to render all elements and only after this is done, scroll to it?

Many thanks in advance.

Upvotes: 2

Views: 173

Answers (1)

Skyp
Skyp

Reputation: 1197

The issue was actually that afterRender only works on templates. I moved my view to a template then afterRender works.

Upvotes: 1

Related Questions