question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Datatable Angular way - img - Lazy load - fnRowCallback

See original GitHub issue

Hello ;

I’m new in the Angular world and I fail on something which could seem easy… We have used datatable with jQuery for years now but we want now use angular-datatable. A problem we are facing is when each datatable row has an img. We use the angular way (needed for us). We have a template such as:

<div class="aps" ng-app="aps_standard" ng-controller="apsPackageCtrl as controller">
    <table datatable="ng" dt-options="controller.dtOptions">
        <thead>
        <tr>
            <th></th>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="package in ::controller.packages">
            <td>
                <div>
                    <div class="aps_logo">
                        <a href="?aps_action=details&id={{ package.id }}">
                            <img ng-src="{{ package.icon_url }}" alt="{{ package.name }}" />
                        </a>
                    </div>
                    <div class="aps_description">
                        <h4>
                            <a href="?aps_action=details&id={{ package.id }}">
                                {{ package.name + ' - ' + package.version  }}
                            </a>
                        </h4>
                        {{ package.summary }}
                    </div>
                    <div class="aps_details">
                        <a href="?aps_action=details&id={{ package.id }}">{TR_DETAILS}</a>
                    </div>
                </div>
                <div>
                    <div class="aps_version">
                        APS
                        <b>{{ package.aps_version }}</b>
                    </div>
                    <div class="aps_info">
                        <span ng-if="package.package_cert != 'none'">Certified</span>
                        {TR_CATEGORY}: <b>{{ package.category }}</b>
                        {TR_VENDOR}: <a href="{{ package.vendor_uri }}">{{ package.vendor }}</a>
                    </div>
                    <div class="aps_status">
                        <button><b>{TR_LOCK}</b></button>
                    </div>
                </div>
            </td>
        </tr>
        </tbody>
    </table>
</div>

The problem here is that when we load ~ 300 rows, the images are all loaded when the template is compiled, even when the rows are not showed yet. The question here is quick simple: How to implement lazy-loading for images? I think about a custom Angular directive such as ngLazySrc which should hold the real img src and replace the src when the row get showed but I don’t really know on which event I must listen and also where I should implement the event listener…

Currently, my angular module look like this:

var aps_standard = angular.module('aps_standard', ['datatables', 'ngResource']);

aps_standard.controller('apsPackageCtrl', apsPackageCtrl);
aps_standard.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
}]);

function apsPackageCtrl($resource, DTOptionsBuilder) {
    var vm = this;
    vm.dtOptions = DTOptionsBuilder.newOptions().withPaginationType('simple');

    $resource('aps_packages.php', { aps_action: 'getPackages' }).query().$promise.then(function (packages) {
        vm.packages = packages;
    });
}

The datatable look like this:

usecase

Thanks you for your help.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ksoboncommented, Aug 30, 2018

@l-lin the link to the “documentation” is dead. Can you refresh it?

1reaction
l-lincommented, Sep 1, 2018

Use the columns.render option. There is an example on the documentation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

View pictures or images inside Jquery DataTable
For example, a field called "image link" just has the Images/PictureName.png part. Then in fnRowCallback you use this data to create an image...
Read more >
image thumbs in table column. lazy loading? - DataTables
Hi, I have a table with column containing unique image thumbnails. The problem i have is that when the page is opened every...
Read more >
Bountysource
Datatable Angular way - img - Lazy load - fnRowCallback.
Read more >
Lazy Load Images in Angular with Two Lines of Code
Such an image will be loaded only when the user scrolls and the image becomes visible. Using this technique, we can gain better...
Read more >
Angular PrimeNG Lazy Loading Datatable (2023) Example
ts , will use getFlightsData method to load the data for dynamic data. import { Injectable } from '@angular/core'; import ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found