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.

Cannot read property 'aDataSort' of undefined in agnular js datatables

See original GitHub issue

Hello everyone, I am trying to implement angular-datatables in my project but it returns "TypeError: Cannot read property ‘aDataSort’ of undefined**"

I am using Angular js version 1.4.9. Jquery version 2.1.1 DataTable version 1.10.10

Refrence site http://l-lin.github.io/angular-datatables/ Below is my code

Html <div class="col-md-12" ng-controller="WithAjaxCtrl as showCase"> <table datatable="" dt-options="showCase.dtOptions" dt-columns="showCase.dtColumns" class="row-border hover"></table></div>

Angular js controller

`angular.module( 'admin.package', [
  'ui.router',
  'ui.bootstrap',
  'datatables',
  'datatables.bootstrap',
  'ngResource',
  'plusOne'
]).controller('WithAjaxCtrl', WithAjaxCtrl);
function` WithAjaxCtrl(DTOptionsBuilder, DTColumnBuilder,$http,UserService,localStorageService) {
  UserService.obj.get('packages/index',localStorageService.get('userkey').token).then(function (results) { 
    if(results.status==200){
    var vm = this;
    vm.dtOptions = DTOptionsBuilder.fromSource(results.data.packages)
        .withPaginationType('full_numbers');
    vm.dtColumns = [
       DTColumnBuilder.newColumn('id').withTitle('id'),
        DTColumnBuilder.newColumn('package_name').withTitle('Packag Name'),
        DTColumnBuilder.newColumn('amount').withTitle('Amount'),
        DTColumnBuilder.newColumn('package_duration').withTitle('Amount'),
        DTColumnBuilder.newColumn('currency').withTitle('validity')

    ];
    console.log(vm.dtColumns);
    console.log( vm.dtOptions);
  }else{
         alert('You are not a authorized user');
      }
    }, function(reason) {
        console.log(reason);
    });
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
l-lincommented, Mar 14, 2016

Your table is rendered after you fetch the data. Thus, your table do not have any options, that’s why DataTables is throwing such error. Since you are rendering in two steps (one request to the server, then render the table), you need to tell the table not to render before the data is fetched. You can use the ngIf to do that:

<div class="col-md-12" ng-controller="WithAjaxCtrl as showCase">
<div ng-if="showCase.displayTable">
<table datatable
    dt-options="showCase.dtOptions"
    dt-columns="showCase.dtColumns"
    class="row-border hover"></table>
</div>
</div>
function WithAjaxCtrl(DTOptionsBuilder, DTColumnBuilder,$http,UserService,localStorageService) {
    var vm = this;
    vm.displayTable = false;
  UserService.obj.get('packages/index',localStorageService.get('userkey').token).then(function (results) { 
    if(results.status==200){
    vm.dtOptions = DTOptionsBuilder.newOptions().withOption('data', results.data.packages)
        .withPaginationType('full_numbers');
    vm.dtColumns = [
       DTColumnBuilder.newColumn('id').withTitle('id'),
        DTColumnBuilder.newColumn('package_name').withTitle('Packag Name'),
        DTColumnBuilder.newColumn('amount').withTitle('Amount'),
        DTColumnBuilder.newColumn('package_duration').withTitle('Amount'),
        DTColumnBuilder.newColumn('currency').withTitle('validity')
    ];
    vm.displayTable = true;
    console.log(vm.dtColumns);
    console.log( vm.dtOptions);
  }else{
         alert('You are not a authorized user');
      }
    }, function(reason) {
        console.log(reason);
    });
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

JQuery Datatables : Cannot read property 'aDataSort' of ...
Most of the time it occurs when something is undefined. I copied the code and removed few columns which disturbed the order by...
Read more >
Cannot read property 'aDataSort' of undefined - DataTables
Dear DataTables I get the following error Cannot read property 'aDataSort' of undefined. Is there anything I am missing ?
Read more >
TypeError: Cannot read property 'aDataSort' of undefined #122
Hi,. I get the same error and I'm using angular 1.3.8, and the last version of datatables. I'm rendering my table ...
Read more >
Cannot read property 'aDataSort' of undefined - YouTube
JavaScript : JQuery Datatables : Cannot read property ' aDataSort' of undefined [ Gift : Animated Search Engine : https://bit.ly/AnimSearch ] ...
Read more >
Cannot read property "aDataSort" of undefined - JSFiddle
1. var json = { ; 2.
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