Reputation: 183
I have a datatable loaded by server side data, everything is ok with this. Now, I want to update the data rows by listen notifications from AWS SQS, when I get a new row data and added to table API and then call the "draw" method, the API trigger an ajax refresh from server side instead (the table was setup with server procesing).
There is a way to "disable" ajax calls temporary? Because, I don't want disable for all time, I want server side processing for pagination and search, only want to add my new row without call the server.
I try this:
var table = $('#tblModel').DataTable(); // Get the API object
// Initialize the Amazon Cognito credentials provider
AWS.config.region = 'us-west-2'; // Region
/**
* Gets the user's Identity
*/
$.getJSON("/cognito", function(data) {
if (data) {
IdentityId = data.IdentityId;
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: IdentityPoolId,
IdentityId: data.IdentityId,
Logins: {
"cognito-identity.amazonaws.com": data.Token
}
});
var queue = new AWS.SQS({params: {QueueUrl: QueueUrl, WaitTimeSeconds: 20}}); // using url to queue
getMessages(queue);
}
});
/**
* Gets the message from SQS
*/
function getMessages(queue) {
queue.receiveMessage(function (err, data) {
if (data) {
if (data.Messages.length == 0) return;
try {
// here add a row or rows, but it trigger a call to refresh data from server side instead.
if (data.Messages.length > 1)
table.rows.add(data.Messages.map(transformMessage)).draw();
else
table.row.add(transformMessage(data.Messages[0])).draw();
// now delete the messages
queue.deleteMessageBatch({
QueueUrl: QueueUrl,
Entries: data.Messages.map(function(Message) {
return {
Id: Message.MessageId,
ReceiptHandle: Message.ReceiptHandle
};
})
}, function(err, data) {
if (err) console.error(err);
});
getMessages(queue);
} catch (e) {
console.error(e);
}
}
});
}
Upvotes: 2
Views: 2969
Reputation: 183
I get the solution, if someone want it: if do you want temporary disable ajax requests must to set two flags of settings: oFeatures.bServerSide and ajax.
// here temporary disable ajax
table.settings()[0].oFeatures.bServerSide = false;
table.settings()[0].ajax = false;
if (data.Messages.length > 1)
table.rows.add(data.Messages.map(transformMessage)).draw();
else
table.row.add(transformMessage(data.Messages[0])).draw();
// here activate it again
table.settings()[0].oFeatures.bServerSide = true;
table.settings()[0].ajax = sUrlServerList;
Upvotes: 8