0

I'm working with a mix of legacy code that uses the jQuery DataTables plugin (version 1.10. Some of the old code passes around jQuery object, and some of the old code passes around DataTables API objects. I'd like to make all new code accept either a jQuery object or a DataTables API object, but I haven't been able to detect when a variable refers to a DataTables API object.

The only detection method I could find in the API, isDataTable, is not suitable - it take a string selector for a table and returns whether the selection has been turned into a DataTable.

The "class detection" methods in this answer don't work; typeof returns "object", constructor.name returns "Object", and I don't know of a constructor to use for instanceof or isPrototypeOf.

As suggested in this answer (to same question), Object.prototype.toString.call(var) returns "[object Object]"

In Chrome, console.log(var) produces a summary that looks like it has some other class information:

▶ _Api {context: Array[1], selector: Object, ajax: Object}

I imagine that means there is something I could use somewhere but this question about where Chrome gets that name has no useful answers.

How can I detect when a variable refers to a jQuery-DataTables API object?

Community
  • 1
  • 1
ShadSterling
  • 1,792
  • 1
  • 21
  • 40

1 Answers1

1

You can dig around more by using a breakpoint in Chrome. If you set a breakpoint just after a DataTable variable, and evaluate the variable, you'll get a complete map. I did that, and found this:

var myDataTable = $('#myTable').DataTable(myOptions); 
alert(myDataTable.$.__dt_wrapper);

The alert returns true. (myOptions is an object containing all the options for the DataTable.) So, I looked at an object (I used myOptions) to see what I could test for. You can't test directly for whether myOptions.$.__dt_wrapper) is false, because myOptions.$ evaluates to undefined. So:

if(undefined != myObjectVariable.$ && myObjectVariable.$.__dt_wrapper) {
    alert('A DataTable Object')
} else {
    alert('Not a DataTable Object')
}

Googling "__dt_wrapper" doesn't show any links that are unrelated to DataTables. Looking at datatables.js, it occurs twice. Both cases suggest that it's an internal attribute used to denote a DataTable object, or more specifically whether an object is a DataTable wrapper.

Since this is undocumented there's no guarantee that it will work in all future versions of DataTables. But this works in DataTables 1.10.11.

Edit: I asked about this on the DataTables forum (link), and there indeed is a cleaner way to do this:

myObjectVariable instanceof $.fn.DataTable.Api

Is true if the object is a DataTable. (This also works for the lower case dataTable object.)

BobRodes
  • 5,990
  • 2
  • 24
  • 26
  • "!+" is a syntax error, I'm assuming that's a typo for "!=" – ShadSterling Aug 01 '16 at 13:50
  • Turns out I'm using DataTables 1.10.3, and this solution works. Thanks! – ShadSterling Aug 01 '16 at 13:55
  • You're welcome. Pleased to hear it, but I'm going to post this question on the DataTables forum and see if Allan Jardine has a better way to handle it. I'm concerned that this might break somewhere down the line. Yes, that's a syntax error, my bad. I'll fix it, thanks. Stupid shift key. – BobRodes Aug 01 '16 at 17:13
  • I posted a question about this [here](https://datatables.net/forums/discussion/36611/using-dt-wrapper-to-determine-if-an-object-is-a-datatable-object-fragile?new=1) on the DataTables forum. – BobRodes Aug 01 '16 at 17:25
  • @Polyergic Allan Jardine posted an improvement to my answer. – BobRodes Aug 02 '16 at 15:27