如何在jQuery DataTable中获取下一个数据加载

时间:2015-09-01 20:52:30

标签: javascript jquery datatables sharepoint-2013 csom

我在jQuery DataTables中显示SharePoint数据。总行数类似于2000,但我在第一次加载时只显示100。

加载页面时,数据正确显示。所以,我想在数据表中获得下一个数据加载,但我无法弄清楚如何做到这一点。

我想过使用bServerSideiDeferLoading,但这些错误会给我json format is not correct的错误。

以下是我目前正在测试的当前代码。 任何人都可以告诉我如何获得剩余的结果?当用户到达100行结束时,如何实现分页?

为什么bServerSideiDeferLoading在我的情况下不起作用?

我在回复SharePoint时使用jQuery和REST也遇到了同样的问题。

 $(document).ready(function () {
        window.StartTime = new Date();
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
            SP.SOD.executeFunc('sp.search.js', 'Microsoft.SharePoint.Client.Search.Query.KeywordQuery', function () {
                var queryText = "ContentTypeId:0x010066FA3DE3E334C841B418C82CC475A227*";
                var clientContext = new SP.ClientContext.get_current();
                var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(clientContext);
                keywordQuery.set_queryText(queryText);
                keywordQuery.set_rowLimit(50);
                keywordQuery.set_trimDuplicates(false);
                var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(clientContext);
                var results = searchExecutor.executeQuery(keywordQuery);
                clientContext.executeQueryAsync(onQuerySuccess, onQueryError);

                function onQuerySuccess() {

                    var rows = results.m_value.ResultTables[0].ResultRows;
                    var totalRows = results.m_value.ResultTables[0].TotalRows;

                    $("#example").DataTable({

                        "bDestory":true,
                        "bProcessing":true,
                        //"bServerSide": true,
                        "iTotalRecords":totalRows,
                        "iTotalDisplayRecords":10,
                        "iDeferLoading": totalRows,
                        "aaData":rows,
                        "aoColumns":[
                            {"mData":"Title"},
                            {"mData":"Path"}
                        ] 
                    }); 
                    console.log("After added to table: " + ((new Date() - window.StartTime) / (60 * 60)) + " seconds");

                }

                function onQueryError(sender, args) {
                    alert("call failed. Error: " + args.get_message());
                }
            });
        });

    });

这是我在使用bServerSideiDeferLoading时点击“下一页”或下拉列表时的标头响应(显然Content-Type返回为text/html):

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/8.5
X-SharePointHealthScore: 0
X-AspNet-Version: 4.0.30319
SPRequestGuid: 6fc9299d-cba4-c06b-92ee-423504bab93b
request-id: 6fc9299d-cba4-c06b-92ee-423504bab93b
X-FRAME-OPTIONS: SAMEORIGIN
SPRequestDuration: 84
SPIisLatency: 1
X-Powered-By: ASP.NET
MicrosoftSharePointTeamServices: 15.0.0.4569
X-Content-Type-Options: nosniff
X-MS-InvokeApp: 1; RequireReadOnly
Date: Tue, 01 Sep 2015 19:37:35 GMT
Content-Length: 23942

如何强制它返回JSON格式化数据?

1 个答案:

答案 0 :(得分:0)

选项bServerSide不适合您,因为响应中的数据必须以某种方式构建,请参阅Server-side processing(对于DataTables 1.9)或Server-side processing(对于DataTables) 1.10)。

如果它限制了结果数量并让DataTables进行过滤,排序和分页,我会删除此行keywordQuery.set_rowLimit(50);

同时删除DataTables初始化代码中的无效或不相关选项,如下所示:

var rows = results.m_value.ResultTables[0].ResultRows;

$("#example").DataTable({
     "aaData":rows,
     "aoColumns":[
         {"mData":"Title"},
         {"mData":"Path"}
     ] 
});