image thumbs in table column. lazy loading?
image thumbs in table column. lazy loading?
Hi, I have a table with column containing unique image thumbnails. The problem i have is that when the page is opened every single thumb (for about 1000 rows) is downloaded even though only the first 10 rows are visible.
Whats the easiest way to get the table to 'lazy load' only the images for rows that are visible?
Whats the easiest way to get the table to 'lazy load' only the images for rows that are visible?
This discussion has been closed.
Replies
I can now have a hidden column in my data with the image urls in and create an image tag as the rows are drawn. Result = no more monster load up front of every bloody thumbnail. Happy days.
I have a similar problem.
Could you please provide the code as well?
Thank you :-)
@ed : Just take the data from the row/cell, and wrap it up in is what I think gregt is suggesting. I'd imgine fnRender would work as well - although that occurs when the row is 'added', so the browser _may_ load the image then... Go with fnRowCallback ;-)
Allan
[code]
oTable = $('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": 'my_json_url/',
"fnRowCallback": customFnRowCallback,
"aoColumns": [
{ sTitle:'', sWidth:'10px', sClass:'tableThumbnailCell', bSortable:false } /*the thumbnail*/
,{ sTitle:'', bVisible:false } /*url*/
,{ sTitle:'Name' }
]
});
//custom function to generate img tags at render time (pseudo lazy loading)
function customFnRowCallback( nRow, aData, iDisplayIndex )
{
$('td:eq(0)', nRow).html( '' );
return nRow;
}
[/code]
The data coming back from my server is
[code]
{
aaData:[
['myurl/thumb1.png', 'myurl/thingy1.html', 'thingy1']
, ['myurl/thumb2.png', 'myurl/thingy2.html', 'thingy2']
...
]
}
[/code]
So the data coming back has 3 columns; img url, link url & name. The table has 2 visible columns; the thumbnail & the name. Each time a table row is drawn, customFnRowCallback will be called and it will build a string for the thumbnail column that is an img wrapping with a link.
Hope that helps;
Greg
Thank you guys for your quick answer.
@Gregt: Thanks for sharing this peace of code. It helped me a lot to understand. It works great now. It's really a good idea to do it with the callback method
@Allan: Big respect brother. I like your DataTables. Really fantastic work.
Thanks guys you made my day :-)
Very helpful :) Thanks :)
Take care,
Alex
[code]function lazy_datatable(nRow, aData, iDisplayIndex)
{
var img = $('img.loading', nRow);
img.attr('src', img.attr('orig'));
return nRow;
}[/code]