DataTables stuck on "Processing" when sorting - javascript

Below is the document ready function
$('#example').dataTable({
"bProcessing": true,
"bServerSide": true,
"aaSorting": [[2, "asc"]],
"sAjaxSource": "/userControl/GetUser.php",
"aoColumnDefs": [{
"aTargets": [0],
"mData": "download_link",
"mRender": function (data, type, full) {
return 'Detail<br/>Delete';
}
}],
"aoColumns": [
{ "mData": null },
{ "mData": "LoginId" },
{ "mData": "FirstName" },
{ "mData": "LastName" }
]
});
var oTable = $('#example').dataTable();
oTable.fnSort([1, 'asc']);
With the above code, the datatable was stuck on "Processing..." like the below screen shows, but if I remove the sorting, the data was shown correctly, but whenever the user request a column to be sort, the result was still the same, is there anything that I did wrong?

I removed "bServerSide": true, and the DataTables can sort and filter properly now

Your server-side implementation has to handle sorting via the iSortCol parameters, using fnSort is for client-side implementations and will not work for server-side
As #mainguy said in his comment, removing bServerSide will disable pagination and more than likely search
Look at the examples for asp.net on the site, as thats the lang you tagged, if you need more assistance, update your question with the asp.net source code

Please see if your server response has the same counter value for draw or sEcho property as sent by the client.
Example, request may contain draw: 11 or sEcho: 11 parameters, then, the server response must contain draw: "11" or sEcho: "11".

Related

JQuery Datatables : Cannot read property 'aDataSort' of undefined

I created this fiddle to and it works well as per my requirements: Fiddle
However, when I use the same in my application I get an error in the browser console saying Cannot read property 'aDataSort' of undefined
In my application, the javascript reads something like as below: I have checked the controller output...it works well and is printed on the console too.
$(document).ready(function() {
$.getJSON("three.htm", function(data) {
// console.log("loadDataTable >> "+JSON.stringify(data));
})
.fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ', ' + error;
alert(err);
console.log( "Request Failed: " + err);
})
.success(function(data){
loadDataTable(data);
});
function loadDataTable(data){
$("#recentSubscribers").dataTable().fnDestroy();
var oTable = $('#recentSubscribers').dataTable({
"aaData" : JSON.parse(data.subscribers),
"processing": true,
"bPaginate": false,
"bFilter": false,
"bSort": false,
"bInfo": false,
"aoColumnDefs": [{
"sTitle": "Subscriber ID",
"aTargets": [0]
}, {
"sTitle": "Install Location",
"aTargets": [1]
}, {
"sTitle": "Subscriber Name",
"aTargets": [2]
}, {
"aTargets": [0],
"mRender": function (data, type, full) {
return '<a style="text-decoration:none;" href="#" class="abc">' + data + '</a>';
}
}],
"aoColumns": [{
"mData": "code"
}, {
"mData": "acctNum"
}, {
"mData": "name"
}]
});
}
})
It's important that your THEAD not be empty in table.As dataTable requires you to specify the number of columns of the expected data .
As per your data it should be
<table id="datatable">
<thead>
<tr>
<th>Subscriber ID</th>
<th>Install Location</th>
<th>Subscriber Name</th>
<th>some data</th>
</tr>
</thead>
</table>
Also had this issue,
This array was out of range:
order: [1, 'asc'],
For me, the bug was in DataTables itself; The code for sorting in DataTables 1.10.9 will not check for bounds; thus if you use something like
order: [[1, 'asc']]
with an empty table, there is no row idx 1 -> this exception ensures.
This happened as the data for the table was being fetched asynchronously. Initially, on page loading the dataTable gets initialized without data. It should be updated later as soon as the result data is fetched.
My solution:
// add within function _fnStringToCss( s ) in datatables.js
// directly after this line
// srcCol = nestedSort[i][0];
if(srcCol >= aoColumns.length) {
continue;
}
// this line follows:
// aDataSort = aoColumns[ srcCol ].aDataSort;
I faced the same problem, the following changes solved my problem.
$(document).ready(function() {
$('.datatable').dataTable( {
bSort: false,
aoColumns: [ { sWidth: "45%" }, { sWidth: "45%" }, { sWidth: "10%", bSearchable: false, bSortable: false } ],
"scrollY": "200px",
"scrollCollapse": true,
"info": true,
"paging": true
} );
} );
the aoColumns array describes the width of each column and its sortable properties.
Another thing to mention this error will also appear when you order by a column
number that does not exist.
In my case I had
$(`#my_table`).empty();
Where it should have been
$(`#my_table tbody`).empty();
Note: in my case I had to empty the table since i had data that I wanted gone before inserting new data.
Just thought of sharing where it "might" help someone in the future!
In my case I solved the problem by establishing a valid column number when applying the order property inside the script where you configure the data table.
var table = $('#mytable').DataTable({
.
.
.
order: [[ 1, "desc" ]],
You need to switch single quotes ['] to double quotes ["] because of parse
if you are using data-order attribute on the table then use it like this data-order='[[1, "asc"]]'
Most of the time it occurs when something is undefined. I copied the code and removed few columns which disturbed the order by index. Carefully make changes and every variable after it.
"order": [[1, "desc"]], fixed my issues previous i was using "order": [[24, "desc"]], and that index was not avaialble.
I had this problem and it was because another script was deleting all of the tables and recreating them, but my table wasn't being recreated. I spent ages on this issue before I noticed that my table wasn't even visible on the page. Can you see your table before you initialize DataTables?
Essentially, the other script was doing:
let tables = $("table");
for (let i = 0; i < tables.length; i++) {
const table = tables[i];
if ($.fn.DataTable.isDataTable(table)) {
$(table).DataTable().destroy(remove);
$(table).empty();
}
}
And it should have been doing:
let tables = $("table.some-class-only");
... the rest ...
I got the error by having multiple tables on the page and trying to initialize them all at once like this:
$('table').DataTable();
After a lot of trial and error, I initialized them separately and the error went away:
$("#table1-id").DataTable();
$("#table2-id").DataTable();
My Solution
add this :
order: 1 ,
For me
adding columns in this format
columns: [
{ data: 'name' },
{ data: 'position' },
{ data: 'salary' },
{ data: 'state_date' },
{ data: 'office' },
{ data: 'extn' }
]
and ajax at this format
ajax: {
url: '/api/foo',
dataSrc: ''
},
solved for me .
Old question, but in my case I was passing an unrelated order= in the URL that would sometimes be empty. Once I changed the url variable to "sortorder" or anything other than "order" the plugin began working normally.
I got the same error, In my case one of the columns was not receiving proper data. some of the columns had strings in place of Integers. after i fixed it it started working properly. In most of the cases the issue is "table not receiveing proper data".
Just my two cents regarding order: [...];
It won't check items type, and in my case I was passing [arrOrders] as opposed to just arrOrders or [...arrOrders], resulting in [[[ 1, "desc" ], [ 2, "desc" ]]].

No matching records found in DataTable

I am using DataTable for retrieving the data from server side. Here. NO information on DataTable. It shows No matching records found error.
Here, oLanguage.sEmptyTable is not working and oLanguage.sZeroRecords is working refer http://datatables.net/ref#sZeroRecords
var DataTableApp = $('#DataTableApp').dataTable({
"sAjaxSource": "php/getAppDetails.php",
"bRetrieve":true,
"bDestroy":true,
"bServerSide": true,
//"bProcessing": true,
"sAjaxDataProp": "aaData",
//"bDeferRender": true,
"sServerMethod": "POST",
"iTotalDisplayRecords":1,
"iTotalRecords":1,
"oLanguage": {
"sZeroRecords": "No records to displays"
},
"fnServerParams": function ( aoData ) {
var imei_app = document.getElementById('imei').value;
console.log(imei_app);
aoData.push({"name":"imei","value":imei_app});
},
//aoColumns
"aoColumns": [{
"mData": "appName"
}, {
"mData": "appId"
}, {
"mData": "versionInstalled"
}, {
"mData": "appSize"
}, {
"mData":"dataSize"
},{
"mData": "appType"
},{
"mData":"installedLocation"
},{
"mData": "installedTime"
}]
});
oLanguage.sEmptyTable and oLanguage.sZeroRecords (or in the latest format language.emptyTable and language.zeroRecords) have different purposes.
language.emptyTable Displays when the table contains no rows at all.
language.zeroRecords Displays when, after applying filters, there are now no records to display.
It sounds like your table had rows before filters were applied.
You need to add .dataTables_empty CSS class with display: none attribute to your global style sheet (i.e. src/styles.css).
Note: in the angular, global style sheet is located at SCSS folder (i.e. scss/_custom.scss).
.dataTables_empty {
display: none;
}
https://l-lin.github.io/angular-datatables/#/basic/server-side-angular-way

Data Tables - Scroller Missing Column Headers on Refresh

I'm using the new Data Tables extra plugin - Scroller.
However, I am having trouble when refreshing the existing Data Table Grid with a new data source array from an ajax request. (Code below).
The first time I run this exact code on the page, it works properly. However, whenever I call this code snippet again given a different source, the data table is re-rendered but is missing all column headers. Does anyone know why the Columns are disappearing every subsequent time after the first?
oTable = $('#example').dataTable({
"aoColumns": [
{ "sTitle": "ID" },
{ "sTitle": "Test" },
{ "sTitle": "Type" },
{ "sTitle": "Date" },
{ "sTitle": "Revision" }
],
"aaData": source,
"bDestroy":true,
"sScrollY": "380px",
"sDom": 'frtiS',
"bDeferRender": true,
"fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
$(nRow).attr('id', "row-" + aData[0]);
return nRow;
}
});
I have also had the same problem and here is the answer from Allan and it worked perfectly in my case.
http://www.datatables.net/forums/discussion/14278/scroller-plugin-misplaces-datatables-column-header#Item_1

jquery datatables: adding extra column

Scenario
I am using datatables (#version 1.9.4) for the first time to display data to the user.
I succeed in retrieving the data via ajax and in binding them to the datatable.
Now I want to add extra columns to let the user process the records. For semplicity sake, the aim is to add a button with an onclick handler that retrieve the data of the 'clicked' record.
In my plan I would bind the json item corresponding to the 'clicked' record to the onclick handler.
Till now, if I add an additional TH for the action column to the DOM, an error occurs from datatables code:
Requested unknown parameter '5' from data source for row 0
Question
How to set custom columns? How to fill their content with HTML?
Here is my actual config.
HTML
<table id="tableCities">
<thead>
<tr>
<th>country</th>
<th>zip</th>
<th>city</th>
<th>district code</th>
<th>district description</th>
<th>actions</th>
</tr>
</thead>
<tbody></tbody>
</table>
Javascript
$('#tableCities').dataTable({
"bPaginate": true,
"bLengthChange": false,
"bFilter": true,
"bSort": true,
"bInfo": false,
"bAutoWidth": true
, "bJQueryUI": true
, "bProcessing": true
, "bServerSide": true
, "sAjaxSource": "../biz/GetCitiesByZip.asp?t=" + t
});
Sample Json result
{
"aaData":
[
[
"IT",
"10030",
"VILLAREGGIA",
"TO",
"Torino"
],
[
"IT",
"10030",
"VISCHE",
"TO",
"Torino"
]
],
"iTotalRecords": 2,
"iTotalDisplayRecords": 2,
"iDisplayStart": 0,
"iDisplayLength": 2
}
Edit
Daniel is right. The solution is to set up the custom column in the aoColumnDefs, specifying the mData and the mRender properties. In particular mRender lets to define custom html and javascript.
/* inside datatable initialization */
, "aoColumnDefs": [
{
"aTargets": [5],
"mData": null,
"mRender": function (data, type, full) {
return 'Process';
}
}
]
You can define your columns in a different way
like this
"aoColumns": [
null,
null,
null,
null,
null,
{ "mData": null }
]
or this
"aoColumnDefs":[
{
"aTargets":[5],
"mData": null
}
]
Here some docs Columns
Take a look at this DataTables AJAX source example - null data source for a column
Note that prior to DataTables 1.9.2 mData was called mDataProp. The name change reflects the flexibility of this property and is consistent with the naming of mRender. If 'mDataProp' is given, then it will still be used by DataTables, as it automatically maps the old name to the new if required.
Another solution/workaround could be adding that '5' parameter...
For example adding extra "" to each row
like this:
[
"IT",
"10030",
"VILLAREGGIA",
"TO",
"Torino",
""
],
[
"IT",
"10030",
"VISCHE",
"TO",
"Torino",
""
]
Just in case anyone using a newer version of DataTables (1.10+) is looking for an answer to this question, I followed the directions on this page:
https://datatables.net/examples/ajax/null_data_source.html
Posting this answer here, just to show that where the aoColumnDefs needs to be defined. I got help from this question it self, but I struggled for a while for where to put the aoColumnDefs. Further more also added the functionality for onclick event.
$(document).ready(function() {
var table = $('#userTable').DataTable( {
"sAjaxSource": "/MyApp/proctoring/user/getAll",
"sAjaxDataProp": "users",
"columns": [
{ "data": "username" },
{ "data": "name" },
{ "data": "surname" },
{ "data": "status" },
{ "data": "emailAddress" },
{ "data" : "userId" }
],
"aoColumnDefs": [
{
"aTargets": [5],
"mData": "userId",
"mRender": function (data, type, full) {
return '<button href="#"' + 'id="'+ data + '">Edit</button>';
}
}
]
} );
$('#userTable tbody').on( 'click', 'button', function () {
var data = table.row( $(this).parents('tr') ).data();
console.log(data);
$('#userEditModal').modal('show');
});
} );

Memory Management with Repeating jQuery Datatables AJAX Calls

I'm fairly new to javascript and jQuery, so please excuse any incorrect verbiage in my problem description.
Background: I have a site where I'm using the Datatables plugin for jQuery to display a live, realtime display of a SQL table. The SQL query is processed in ASP.NET and returned to the client as a JSON object. Right now, I'm only displaying ~500 records.
Once the JSON object has been parsed by the datatables plugin, I perform some conditional formatting of the table cells -- hence there could be some heavy DOM manipulation if I understand that concept correctly. I've read that this can be where a lot of memory leaks can occur, but I also thought that jQuery was pretty solid at cleaning up after itself.
I'm using a setInterval timer to update the datatable periodically such that it displays changes in real time.
My Problem: Memory consumption of my site is out of control. On every AJAX call (~every 2 sec), the memory usage for the page can jump as much as 2MB. To throttle this back, I installed an idle plugin for jQuery to detect when the user is active on the site - and I reduce the AJAX calls to ~every 1 hour when the user is idle. I heard that this can give more space for the browser to perform garbage collection.
What I find is that memory climbs pretty continuously while active, with a slight drop every 4 or 5 calls where it looks like some garbage collection is being performed. Memory does not climb when the AJAX calls are reduced while the user is idle. Below, I've pasted a stripped down version of my code (excluding some irrelevant snippets). I'm sure it's not the cleanest code - but if someone could provide a pointer as to what might be causing the memory consumption -- or how I could reduce the consumption, it would be greatly appreciated.
Thanks in advance!
//TIMER
var updateFreq = 5000;
var timer;
setIdleTimeout(5000); // 5 seconds
setAwayTimeout(50000); // 10 seconds
document.onIdle = function() {
clearInterval(timer);
updateTable(3600000); //update once an hour
//if (typeof (CollectGarbage) == "function") { CollectGarbage(); }
}
document.onAway = function() {
clearInterval(timer);
updateTable(3600000); //update once an hour
//if (typeof (CollectGarbage) == "function") { CollectGarbage(); }
}
document.onBack = function(isIdle, isAway) {
clearInterval(timer);
updateTable(5000); //update once every two seconds
}
//END TIMER
var oTable;
var oSettings;
$(document).ready(function() {
oTable = $("#production_table").dataTable({
"sDom": '<"TT"T><"tab"t><"F"fip>',
"iDisplayLength": -1,
"sAjaxSource": 'Data.ashx',
// "sScrollY": y - 217,
//"sScrollX": "100%",
"bJQueryUI": true,
"bDeferRender": true,
// "bStateSave": true,
"aaSorting": [[16, 'desc']],
"sPaginationType": "full_numbers",
// "bAutoWidth": false,
"fnDrawCallback": function(oSettings) {
addFormat();
try {
$('td').disableTextSelect();
}
catch (err) {
}
},
"fnServerData": function(sSource, aoData, fnCallback) {
$.ajax({
dataType: 'json',
type: "GET",
cache: false,
url: sSource,
data: aoData,
success: fnCallback
})
},
"fnInitComplete": function(oSettings, json) {
//alert('DataTables has finished its initialisation.');
// addFormat();
//$('td').disableTextSelect();
},
// "bProcessing": true,
"aoColumns": [
{ "mDataProp": null, "bSortable": false },
{ "mDataProp": "serial", "sClass": "serial" },
{ "mDataProp": "us", "sClass": "us" },
{ "mDataProp": "type", "sClass": "type" },
{ "mDataProp": "compartment", "sClass": "compartment" },
{ "mDataProp": "leg", "sClass": "leg", "bVisible": false },
{ "mDataProp": "comp", "sClass": "comp", "bVisible": false },
{ "mDataProp": "patient", "sClass": "patient", "bVisible": false },
{ "mDataProp": "dob", "sClass": "dob", "bVisible": false },
{ "mDataProp": "surgeon", "sClass": "surgeon", "bVisible": false },
{ "mDataProp": "surgery", "sClass": "surgery", "bVisible": false }
//I've truncated this section slightly...
]
});
updateTable(updateFreq);
});
function updateTable(uF) {
// Update the table using the fnReloadAjax call method
timer = setInterval(function() {
var iStart = oSettings._iDisplayStart;
oSettings = oTable.fnSettings();
oTable.fnReloadAjax();
//oTable.fnDisplayStart(oSettings, iStart, true);
}, uF);
}
I should have posted this a long while ago. What was causing this to spiral out of control was that I was requesting the entire data set (several thousand rows) every 2 seconds.
What I ended up doing to fix this was changing my data source such that on load, I would get all of the data from the source - but any time after that, I would pull from a different source that only returned changes since the last request. It really cut down on the data being sent down. As a result, memory usage was significantly cut down and garbage collection had no problems handling any slight climbs.

Categories

Resources