How to disable sorting on jQuery.DataTables init? - javascript

Is anyone tried DataTables, when I bind(init) it to a <table>, data is sorted.
Is there anyway to disable the default sorting?

According to DataTabe's reference manual, this should work:
$('#example').dataTable({
"aaSorting": []
});

try this:
$(document).ready(function () {
$('#example').dataTable({
"order": [[3, "desc"]]
});
});

Related

JavaScript Datatable.JS / PHP MySQLi Sorting Issue

So I'm using Datatables.JS to create a table of results returned from a MySQL database. When I return the results I'm trying to display them in DESC order. I have tried using ORDER BY DESC in my MySQLi query, this does return the results in the correct order, however when datatables is reading the results it's displaying them in some random order. So I've tried playing with the datatable settings to sort by my ID column but keep that column hidden. Whenever I attempt to add any code to handle the sorting, the sorting issue itself becomes resolved but all my pagination and buttons such as the buttons that allow the user to select which columns they would like to view just disappear. Below is the JS I'm using to select the features and setup I need for this datatable, can anyone show me how to add sorting by the ID column DESC into this without breaking it?
$(document).ready(function() {
$('#datatable').DataTable();
//Buttons examples
var table = $('#datatable-buttons').DataTable({
pageLength: 20,
lengthChange: false,
searching: false,
buttons: ['copy', 'excel', 'pdf', 'colvis']
});
table.buttons().container()
.appendTo('#datatable-buttons_wrapper .col-md-6:eq(0)');
} );
Adding ordering: and then selecting the column and defining the order seems to break all the other settings, I lose my pagination and default results length as well as all control buttons on the page.
$(document).ready(function() {
$('#datatable').DataTable();
//Buttons examples
var table = $('#datatable-buttons').DataTable({
pageLength: 20,
lengthChange: false,
searching: false,
buttons: ['copy', 'excel', 'pdf', 'colvis'],
order: [[ 1, desc ]]
});
table.buttons().container()
.appendTo('#datatable-buttons_wrapper .col-md-6:eq(0)');
} );
I have no idea why this solutions works and I still don't understand what caused the original problem but I did find a working piece of code posted under a loosely relevant problem on another forum. I combined that code with what I had already and it solved my problem. Hopefully this helps someone else out who may encounter the same issue.
$(document).ready(function() {
$('#datatable-buttons').DataTable( {
pageLength: 20,
lengthChange: false,
searching: false,
ordering: true,
order: [[ 3, 'DESC' ]],
dom: 'B1frtip',
buttons: [
'copy',
'excel',
'pdf',
'colvis'
]
});
table.buttons().container()
.appendTo('#datatable-buttons_wrapper .col-md-6:eq(0)');
});
Just wanted to mention this JS was for a datatable that's part of the Fonik Bootstrap Admin Template.

DataTables warning: table id=trainingMaterialTable - Cannot reinitialise DataTable.

DataTables warning: table id=trainingMaterialTable - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3 how to solve this error.
Don't call twice in a page
For Example :
index.html - In javascript section :
Avoid :
<script>
$('#example').dataTable( {
paging: false
} );
$('#example').dataTable( {
paging: false
} );
</script>
Use :
<script>
$('#example').dataTable( {
paging: false
} );
</script>

JQuery Datatable Responsiveness Issue with Fixed Column

When I use fixed column in JQuery Datatable it doesn't work properly if I resize the window.
If I remove scrollX from options, it appears just fine however fixedColumn feature doesn't work then.
See my options below:
$(document).ready(function() {
$('#example').DataTable( {
scrollY: 300,
scrollX: true,
scrollCollapse: true,
paging: false,
fixedColumns: true,
} );
} );
Link To Jsfiddle
Just Add width="100%" in your table tag.
It should be working by doing this.

Dropdown inside datatable elements with pagination

I am new to data-tables and I am trying to do this but i wanted to do with pagination since the answer provided in the link did not involve any pagination. Is it possible? Because i have tried and when i set the pagination to true, the drop-down solution in the link given won't work.
<script type="text/javascript">
$(document).ready(function() {
$('#datatable-responsive').DataTable( {
"responsive": true,
"paging": true,
"dom": 't<"margin-dataTable-bottom"<"pull-left margin-dataTable-pagination">>'
} );
} );
</script>
You can try this alternative solution which is an extensible pagination mechanism in datatable,
$(document).ready(function() {
$('#example').DataTable( {
"pagingType": "full_numbers"
} );
} );
Please find below link for more details:
https://datatables.net/examples/basic_init/alt_pagination.html

Can't destroy() jQuery Data Table

I am having an issue with trying to destroy a JQuery Data Table.
Here is where I initialize it:
$(document).ready(function() {
var table = $(".dynamic-wide-table").DataTable({
"aaSorting": [],
"scrollX": true,
"scrollY": 530,
"scrollCollapse": true,
"lengthMenu": [
[100, 400, 1000, 5000, -1],
[100, 400, 1000, 5000, "All"]
],
"retrieve": true
});
});
Here is where I try to destroy it:
$(document).ready(function() { // Note that my page has two tables on it!
table.destroy();
table[0].destroy();
}); // Trying to delete both tables first, then just the first table
Here are my errors:
Uncaught TypeError: table.destroy is not a function
Uncaught TypeError: table[0].destroy is not a function
Does anybody know what's going on?! I'm very confused.
Edit:
Here is an image of what happens when I console.log table.
Instantiate the tables with an each() loop and store them in an array:
$(document).ready(function() {
var tables = [];
$("table").each(function(i){
var table = $(this).DataTable({
"aaSorting": [],
"scrollX": true,
"scrollY": 530,
"scrollCollapse": true,
"lengthMenu": [
[100, 400, 1000, 5000, -1],
[100, 400, 1000, 5000, "All"]
],
"retrieve": true
});
tables.push(table);
});
$('#button').click( function () {
// Then you can call destroy on that object
var elem = tables[0].table().node();
tables[0].destroy();
// And empty the element
$(elem).empty()
} );
} );
Link to jsFiddle
You can't both create and destroy a DataTable within $(document).ready(), since those events are triggered at the same time. See #CMedina's JSfiddle where he shows that they work when triggered by, say, a button click. (Assuming these are in the same file) Ignore this part; apparently they are different files.
Note that you said in a comment that the creation/destroy are in different files. If that's indeed the case, how is the destroying file made aware of table? If it's not aware of the DataTable (which could also be caused by not including the script file on that page) then it won't know what destroy() does, and throw an error.
Edit: I just noticed that you are trying to use destroy() on table and then on table[0]. You can't have table be a DataTable and also an array of DataTables. Try naming your tables uniquely or both in arrays; either
var table;
table[0] = $('#example').DataTable();
table[1] = $('#example2').DataTable();
Or (probably better)
var table1 = $('#example').DataTable();
var table2 = $('#example').DataTable();
I suppose that you have 2 datatables in your page, and your initializacion is for all tables with the class .dynamic-wide-table.
For destroy only one table you should get all datatables in your page with tables function.
Then apply destroy to specified table: (In my example work with 2 datatables).
$(document).ready(function() {
var table = $(".dynamic-wide-table").DataTable({
"aaSorting": [],
"scrollX": true,
"scrollY": 530,
"scrollCollapse": true,
"lengthMenu": [
[100, 400, 1000, 5000, -1],
[100, 400, 1000, 5000, "All"]
],
"retrieve": true
});
$('#destroy1').click( function () {
var dt1 = $.fn.dataTable.tables()[0];
$(dt1).DataTable().destroy();
} );
$('#destroy2').click( function () {
var dt2 = $.fn.dataTable.tables()[1];
$(dt2).DataTable().destroy();
} );
} );
Result: https://jsfiddle.net/cmedina/7kfmyw6x/80/
I had same issue. although there was only one table in my page, but I'd got error:
Uncaught (in promise) TypeError: tbl.destroy is not a function
the reason for me was because instead of initializing dataTable like:
let tbl = $('#myTable').DataTable();
I'd wrote it as:
let tbl = $('#myTable').dataTable();
please notice to write .DataTable with capital D.

Categories