Skip to content

Commit

Permalink
New: -api $.fn.dataTable.isDataTable() will now check for DataTable…
Browse files Browse the repository at this point in the history
… API instances as well (i.e. variables passed in)

- Thread 38053
  • Loading branch information
Allan Jardine committed Sep 29, 2016
1 parent ec2fe61 commit 0a26b7c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
13 changes: 10 additions & 3 deletions docs/api/$.fn.dataTable.isDataTable().xml
Expand Up @@ -7,9 +7,16 @@
<type type="function">
<signature>isDataTable( table )</signature>
<description>Check if a `-tag table` node is a DataTable table already or not.</description>
<param type="string|node|jQuery" name="version">
The table to check. This can be given as the DOM element, a jQuery selector for the node to check, or a jQuery object containing the node to check. Note that only the first node is checked if the jQuery selector or object match multiple nodes.
</param>
<parameter type="string|node|jQuery|DataTable.Api" name="version">
The table to check. This can be given as one of:

* A DOM element
* A jQuery selector
* A jQuery object containing the node to check
* A DataTables API instance.

Note that only the first node is checked if the jQuery selector or object match multiple nodes.
</parameter>
<returns type="boolean">`true` the given table is a DataTable, `false` otherwise</returns>
</type>

Expand Down
4 changes: 4 additions & 0 deletions js/api/api.static.js
Expand Up @@ -58,6 +58,10 @@ DataTable.isDataTable = DataTable.fnIsDataTable = function ( table )
var t = $(table).get(0);
var is = false;

if ( table instanceof DataTable.Api ) {
return true;
}

$.each( DataTable.settings, function (i, o) {
var head = o.nScrollHead ? $('table', o.nScrollHead)[0] : null;
var foot = o.nScrollFoot ? $('table', o.nScrollFoot)[0] : null;
Expand Down
62 changes: 62 additions & 0 deletions test/api/static/isDataTable.js
@@ -0,0 +1,62 @@

describe( 'Static method - isDataTable', function() {
dt.libs( {
js: [ 'jquery', 'datatables' ],
css: [ 'datatables' ]
} );

var table;

it( 'Exists', function () {
expect( typeof $.fn.dataTable.isDataTable ).toBe( 'function' );
} );

it( 'Accepts a DataTable table node', function () {
table = $('#example').DataTable();

expect( $.fn.dataTable.isDataTable( $('#example').get(0) ) ).toBe( true );
} );

it( 'Other nodes return false', function () {
table = $('#example').DataTable();

expect( $.fn.dataTable.isDataTable( $('th').get(0) ) ).toBe( false );
expect( $.fn.dataTable.isDataTable( $('td').get(0) ) ).toBe( false );
expect( $.fn.dataTable.isDataTable( $('div').get(0) ) ).toBe( false );
} );

it( 'Can accept in a jQuery selector', function () {
expect( $.fn.dataTable.isDataTable( 'table.dataTable' ) ).toBe( true );
expect( $.fn.dataTable.isDataTable( 'div' ) ).toBe( false );
} );

it( 'Can accept a DataTable API instance', function () {
expect( $.fn.dataTable.isDataTable( table ) ).toBe( true );
expect( $.fn.dataTable.isDataTable( 1 ) ).toBe( false );
} );


dt.libs( {
js: [ 'jquery', 'datatables' ],
css: [ 'datatables' ]
} );

it( 'Returns true for the header in a scrolling table', function () {
table = $('#example').DataTable( {
scrollY: 200
} );

var scrollingTable = $(table.table().header()).closest('table')
expect( $.fn.dataTable.isDataTable( scrollingTable ) ).toBe( true );
} );

it( 'Returns true for the body in a scrolling table', function () {
var scrollingTable = $(table.table().body()).closest('table')
expect( $.fn.dataTable.isDataTable( scrollingTable ) ).toBe( true );
} );

it( 'Returns true for the footer in a scrolling table', function () {
var scrollingTable = $(table.table().footer()).closest('table')
expect( $.fn.dataTable.isDataTable( scrollingTable ) ).toBe( true );
} );
} );

0 comments on commit 0a26b7c

Please sign in to comment.