May 29, 2009

telerik radGrid - be careful with the virtual scrolling under Firefox

Again some issues under Firefox.
We have found a great example of how to populate on demand a radGrid (when scrolling grid scrollbar down to the end it will make ajaxRequest and ask for some more items to fill in the grid with).

We loved this feature and decided to incorporate it into one of our projects.
Unfortunatelly I discovered it won't work pretty well under the FireFox browser.
Let me briefly explain what does the code and then what is the problem, then I will suggest a simple fix.

Here is the demo:
http://www.telerik.com/help/aspnet-ajax/grdvirtualscrollpaging.html

Now, it shows you how to request the items and then reload the grid. So far everything works perfect.
If you however reach the end of the grid items (meaning you are at the last page, or the page count is 1), when you pull the scrollbar down to the bottom, firefox will execute the function which should pull some more records.
Then it will append them and.
So far everything is great!
But it will then magically decide to fire the OnScroll event once again, it will not pull any more records as you already have them in the grid.
And then it will append them.
Then it will again fire the OnScroll event ....
... then it will again fire the OnScroll event ...


Hm, sounds like endless trips to the server and back. That's what happened when we tested.

Now here is the code we had to achieve this:

<ClientEvents OnScroll="HandleScrolling" />

And here is the JS HandleScrolling function:

function HandleScrolling(sender, eventArgs) {
if (eventArgs.get_isOnBottom()) {
$find(
"<%= RadAjaxManager.GetCurrent(this.Page).ClientID %>").ajaxRequest(sender.get_masterTableView().get_name());
}
}

Now to be hones - I never have even dream of achieving such functionality with 4 lines of code. Kudos to telerik team for architecting the controls this way.
The fix to this issue also bring another feature ;) (not bad but good) - you will not need to go to the server when you already have all the records in your radGrid.

Here is the fixed version of the HandleScrolling javascript function:

function HandleScrolling(sender, eventArgs) {
if (eventArgs.get_isOnBottom() && sender.get_masterTableView().PageCount > 1) {
$find(
"<%= RadAjaxManager.GetCurrent(this.Page).ClientID %>").ajaxRequest(sender.get_masterTableView().get_name());
}
}

Did you spot the difference?
All i did was to add logical "and" operator to check if the masterTableView page is only one (which means that we don't have anything on the server which should be loaded on the client) and voilla!


Hope this helps someone!

2 comments:

Павелъ Дончевъ said...

Sorry for the delay.
On button click you need to first get the grid. Then you can use it to obtain its rows, something like:

var grid = $find("<%=RadGrid1.ClientID %>");

Then you can use the "grid" variable to do whatever you want.

You will have the radGrid client script object in the grid so you can use the documentation on its members from telerik.com documentation.

Rajasekhar said...

i have used radgrid virtual scrolling feature in my application. It is working fine in IE7, Firefox and chrome but not working in IE8. Please let me know what may be the reason?