临时禁用服务器对数据表的处理 [英] Temporary disable server processing on datatables

查看:50
本文介绍了临时禁用服务器对数据表的处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由服务器端数据加载的数据表,一切正常.现在,我想通过侦听来自 AWS SQS 的通知来更新数据行,当我获取新行数据并添加到表 API 然后调用draw"方法时,API 会改为从服务器端触发 ajax 刷新(表已通过服务器处理进行设置).

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).

有没有办法暂时禁用"ajax 调用?因为,我不想一直禁用,我想要服务器端处理分页和搜索,只想添加我的新行而不调用服务器.

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.

我试试这个:

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);
            }
        }
    });
}

推荐答案

我得到了解决方案,如果有人想要它:如果你想临时禁用 ajax 请求必须设置两个设置标志:oFeatures.bServerSide 和 ajax.

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;

这篇关于临时禁用服务器对数据表的处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆