Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use DataTables fnDestroy method

I am using the DataTables plugin for jQuery but keep getting the following error when I attempt to use the fnDestroy method:

Undefined

I have tried using all of the following variants:

1)

 $('#data').dataTable().fnDestroy();

2)

var dt = $('#data').dataTable();
dt.fnDestroy();

3)

var data = document.getElementById('data');
data.fnDestroy();

The 'data' object exists - The HTML is as follows:

<div class="resultset">
     <table class="display" id="data">              
         <tbody>

         </tbody>
      </table>  
</div>

The DataTable is built with Javascript (not shown here), but the base object is hard-coded.

The API documentation shows that the second method should work:

$(document).ready(function() {
  // This example is fairly pointless in reality, but shows how fnDestroy can be used
  var oTable = $('#example').dataTable();
  oTable.fnDestroy();
} );

EDIT

The DataTable displays fine and otherwise works well. The issue arises when I attempt to execute this function.

like image 591
Roy Hinkley Avatar asked Jan 08 '14 14:01

Roy Hinkley


People also ask

How to create a DataTable using jQuery?

1. First create a HTML Table so that the column names are under thead and column data under tbody. ..... 2. Then add the jQuery and DataTables scripts reference on the page. 3. Finally inside the jQuery .ready () function call the .DataTable () function for the table.

What is DataTables?

Last Updated: June 4, 2021 DataTables is a powerful jQuery plugin for creating for displaying information in tables and adding interactions to them. It provides s earching, sorting and pagination without any configuration. In this 2 minutes tutorial you will learn the basics of DataTables and use it in your website.

What is the use of Y-width in a DataTable?

When a table is too wide to fit into a certain layout, or you have a large number of columns in the table, y... This property can be used to force a DataTable to use more width than it might otherwise do when x-scrolling is enabled. For example if y...

How to check if a table is a DataTable or not?

Check if a TABLE node is a DataTable table already or not. Check to see if a row is 'open' or not. This function will place a new row directly after a row which is currently on display on the page, with the HTML contents that is passed...


1 Answers

It seems to be the difference between...

_table = jQuery('table#fp-table-table').dataTable(); // .fnDestroy() works

and

_table = jQuery('table#fp-table-table').DataTable(); // .fnDestroy() doesn't work

DataTable seems to be for API calls back into the object and dataTable seems to be the intialisation method.

In my project I had changed the initialisation to use DataTable instead of dataTable to perform a filtering task. After this my AJAX reloads would throw the 'undefined' error, so I changed it back... i esta.

like image 134
ness-EE Avatar answered Oct 06 '22 06:10

ness-EE