2

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);
            }
        }
    });
}
3
  • Can't you just use a boolean variable that stores whether you should perform the ajax? Then set the boolean whenever you want to turn on or off the ajax, and check that boolean in the function that runs the ajax. Commented Nov 9, 2016 at 15:41
  • @KevinWorkman that seems to be the answer IMHO Commented Nov 9, 2016 at 15:56
  • 2
    @KevinWorkman I don't thik so, because in DataTables the ajax feature is activate on setup: $('#tblModel').dataTable({ ajax: "/path/to/data", .... }); And it must be like this on page render in order to get the data. So, it's not that easy I think Commented Nov 9, 2016 at 16:28

1 Answer 1

8

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;
Sign up to request clarification or add additional context in comments.

5 Comments

I have some "logic issue" with this solution, it disabled the table but also it reset the information about pagination.
Excellent! You save a lot of my time! It's work!
Excelent. You are king.
What is sUrlServerList? @GroverManuelCamposAncajima
is a constant that contains me/your URL that will fetch the rows

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.