Thursday, January 22, 2015

Sometimes solution for: Cannot read property "aDataSort" of undefined

At my company, we use DataTables frequently.  (Code available at Datatables.net)

Like my last post, I wanted to put up another post either to help others, or more likely, help myself when I can't solve the answer.

When I'm building out my datatables, I am often converting from an older syntax to the (currently) latest version.   I bang my head on the wall for at least 30 minutes over this error in the Javascript console:

Cannot read property "aDataSort" of undefined

Take this Datatable setup.

In my case, this is my (overly simplified) columnDefs section of my Datatable definition

, 'columnDefs': [
 { 'title':'ID', 'name': 'BatchID', 'data': 'BatchID' }
, { ''title':'Type', 'name': 'Type', 'data': 'Type' }
, { 'title':'Status', 'name': 'Status', 'data': 'Status'

This error is thrown because I'm missing (what should be an obvious addition) below:

var nColNumber = -1;
...
, 'columnDefs': [
  { 'targets': [ ++nColNumber ], 'title':'ID', 'name': 'BatchID', 'data': 'BatchID' }
, { 'targets': [ ++nColNumber ], 'title':'Type', 'name': 'Type', 'data': 'Type' }
, { 'targets': [ ++nColNumber ], 'title':'Status', 'name': 'Status', 'data': 'Status'

When you build your Datatable with a columnDefs configuration option, it's obviously VERY important to tell it the "target" of the definition.   I typically have only an empty <table id="someID"></table> definition in my HTML, then build it completely from Javascript.

I use the ++nColNumber, simply so I can easily move columns around later, and not have to renumber them, and I try to always reference columns by name not position, such as for hiding and showing columns at runtime.

Hope this helps someone solve this issue!

- Will Belden
January 22, 2015

1 comment:

Unknown said...

how to appl targets for dynamic javascript objects.please suggest me................