最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Working example of datatables with lazy loading of images - Stack Overflow

programmeradmin0浏览0评论

I'm looking for a working example of lazy-loading of images within datatables that continues to work after clicking on a column heading to change the sorting.

The method I've had the most success with uses the jquery.lazyload plug:

<script src="//cdnjs.cloudflare/ajax/libs/jquery.lazyload/1.9.1/jquery.lazyload.min.js"></script>
<script type="text/javascript">
    $(function() {
    $("img.lazy").lazyload();
    });
</script>   

Here's how I reference the image:

<img class="lazy" data-original=".jpg" width="50" height="50" />

The lazy loading works fine if I never change the sorting. However, if I decide to change the sorting of the table by clicking on one of the column headings, the lazy loading stops working and any images that were not downloaded at this point will remain blank when I scroll them into view.

I wasn't born a javascript or jQuery expert, so had to learn from examples here on SO and elsewhere. But this problem stumps me. From my research, I see a lot of comments and snippets about how it is possible, but no actual working example that proves this is supported.

jquery-lazyload images in jquery-databables

I wont rattle off the numerous other links I found here and elsewhere, I just need some kind soul to help.

I'm looking for a working example of lazy-loading of images within datatables that continues to work after clicking on a column heading to change the sorting.

The method I've had the most success with uses the jquery.lazyload plug:

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.lazyload/1.9.1/jquery.lazyload.min.js"></script>
<script type="text/javascript">
    $(function() {
    $("img.lazy").lazyload();
    });
</script>   

Here's how I reference the image:

<img class="lazy" data-original="https://example.com/image.jpg" width="50" height="50" />

The lazy loading works fine if I never change the sorting. However, if I decide to change the sorting of the table by clicking on one of the column headings, the lazy loading stops working and any images that were not downloaded at this point will remain blank when I scroll them into view.

I wasn't born a javascript or jQuery expert, so had to learn from examples here on SO and elsewhere. But this problem stumps me. From my research, I see a lot of comments and snippets about how it is possible, but no actual working example that proves this is supported.

jquery-lazyload images in jquery-databables

https://datatables.net/forums/discussion/1959/image-thumbs-in-table-column-lazy-loading

I wont rattle off the numerous other links I found here and elsewhere, I just need some kind soul to help.

Share Improve this question edited May 23, 2017 at 12:13 CommunityBot 11 silver badge asked Dec 10, 2016 at 13:00 Wonko the SaneWonko the Sane 8324 gold badges14 silver badges31 bronze badges 1
  • If the sorting is on client side the library may be fetching the data first and would then resort to sorting. – randominstanceOfLivingThing Commented Dec 16, 2016 at 14:41
Add a comment  | 

2 Answers 2

Reset to default 18 +50

Use drawCallback option to define a function that will be called every time DataTables performs a draw.

For example:

$('.table-data').DataTable({
   drawCallback: function(){
        $("img.lazy").lazyload();
   }
});

See this jsFiddle for code and demonstration.

I'm sort of stuck with this one seeing as I don't have access to your backend or more information about your circumstances but I think what will work will be to call your lazy load code in the draw event like this:

$(document).ready(function() {
    $("img.lazy").lazyload();
    $('.table-data').DataTable({
        "drawCallback": function(){
            $("img.lazy").lazyload();
            setTimeout(function(){
                $(window).trigger("scroll")
            }, 1000);
        }
    });
});

I've got an example up here, but I think you might be using server-side processing?

发布评论

评论列表(0)

  1. 暂无评论