一个Datatables.net表,具有来自Sharepoint 2010 REST API的多个ajax调用 [英] One Datatables.net table with multiple ajax calls from Sharepoint 2010 REST API

查看:154
本文介绍了一个Datatables.net表,具有来自Sharepoint 2010 REST API的多个ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的,所以请提前提前给我一个漫长的问题。我是SP2010的新手,到目前为止,还有以下教程让JQuery Ajax调用到我的SP列表:谁需要数据视图Web部件? SharePoint REST和DataTables.net



我的(和大家的)问题是,SP2010 listdata.svc仅返回JSON中的前1000个条目。但是,该教程建议使用预过滤方法来仅调用用户需要的数据,在我的情况下,我想将所有200个条目加载到表中,然后让用户选择/过滤任何他/她要。



我设法做了两个Ajax调用(其中一个具有前1000个条目,另一个与其余的大于2000个)进行测试,以便于看看他们中的哪一个进入表),在调试器中,两个GET请求都出来,并返回状态200-OK,数据只有一个被加载到表中。



问题:是否有任何方法将返回的jqXHR响应合并到Datatable之前,还是有任何设置表初始化,第二个数据不覆盖第一个数据?我知道,多个ajax调用是大多数的不去,但在我的情况下,由于我的服务器的速度和表的使用频率,这不是一个问题,但到目前为止,我发现了解决方案多个调用的主题填充多个表。



我的代码到目前为止:



 < script type =text / javascript > function LoadData(){var call = $ .ajax({url:https://SP2010_siteaddress.com/site/_vti_bin/listdata.svc/Listname?$select=Data1,Data2,Data3,Data4,Data5 ,type:GET,dataType:json,headers:{Accept:application / json; odata = verbose}}); var call2 = $ .ajax({url:https://SP2010_siteaddress.com/site/_vti_bin/listdata.svc/Listname?$select=Data1,Data2,Data3,Data4,Data5&$skiptoken=2000,键入: GET,dataType:json,headers:{Accept:application / json; odata = verbose}}); (call,call2).done(function(data,textStatus,jqXHR){$('#example').on('xhr.dt',function(e,settings,json){for(var i = = json.aaData.length; i< ien; i ++){json.aaData [i] .sum = json.aaData [i] .one + json.aaData [i] .two;} //注意不返回 - 操纵数据直接在JSON对象中。}).dataTable({ajax:data.json,bServerside:true,bProcessing:false,aaData:data.d.results,aoColumns:[{ mData:Data1},{mData:Data2},{mData:Data3},{mData:Data4},{mData:Data5}] bRetrieve:true,//initComplete:function(settings,json){// alert('DataTables has finished it initialization。'); //}}); call.fail(function(jqXHR,textStatus,errorThrown) {alert(检索任务时出错+ jqXHR.responseText);});});}< / script>  

/ p>

提前感谢任何建议如何处理。

解决方案

如果您无法避免限制每个Ajax调用的 1000 记录,您可以使用下面的代码。



它使用 $。当在Ajax调用成功时执行回调。

  function LoadData()
{
var call1 = $ .ajax({
url:https://SP2010_siteaddress.com/site/_vti_bin /listdata.svc/Listname?$select=Data1,Data2,Data3,Data4,Data5,
类型:GET,
dataType:json,
标题:{
接受:application / json; odata = verbose
}
});

var call2 = $ .ajax({
url:https://SP2010_siteaddress.com/site/_vti_bin/listdata.svc/Listname?$select=Data1,Data2,Data3, Data4,Data5& $ skiptoken = 2000,
类型:GET,
dataType:json,
标头:{
接受:application / json; odata = verbose
}
});

//当Ajax请求成功时
$ .when(call1,call2).done(function(a1,a2){
// a1和a2是参数解析对于call1和call2 ajax请求,
//每个参数是一个具有以下结构的数组:[data,statusText,jqXHR]

//合并来自两个Ajax调用的数据
var data = a1 [0] .d.results.concat(a2 [0] .d.results);

$('#example')。dataTable({
aaData:data,
aoColumns:[
{mData:Data1},
{mData:Data2},
{mData :Data3},
{mData:Data4},
{mData:Data5}
]
});
});
}


I am new here, so exuse me in advance for the lengthy question. I'm new to SP2010, and so far followed the following tutorial to make JQuery Ajax call to my SP List: Who Needs a Data View Web Part? SharePoint REST and DataTables.net

My (and everybody's) problem is, that SP2010 listdata.svc returns only the first 1000 entries in the JSON. However the tutorial suggest a pre-filtering method to call for only the data the user would need, in my case, I would like to load all my 200+ entries to the table, and then let the user to select/filter whatever he/she wants.

I managed to make the two Ajax calls (one with first 1000 entries, and the other with the rest above 2000 - for testing, to make it easy to see, which of them made into the table), and in debuggers both GET requests go out and come back with status 200-OK, and the data, but only one of them are loaded into the table.

The question: Are there any methods to combine the returning jqXHR responses before passing to the Datatable, or is there any setting, that after the table initialization, the second data doesn't overwrite the first one? I know, that the multiple ajax calls are a "no go" for most, but in my case, it is not a problem due to the speed of my server, and the usage frequency of the table, but so far I found solutions in the topic of multiple calls to populate multiple tables.

My code so far:

<script type="text/javascript">
function LoadData()
{
var call = $.ajax({
            url: "https://SP2010_siteaddress.com/site/_vti_bin/listdata.svc/Listname?$select=Data1,Data2,Data3,Data4,Data5",
           type: "GET",
           dataType: "json",
           headers: {
                Accept: "application/json;odata=verbose"
            }       
        });
		
var call2 = $.ajax({
            url: "https://SP2010_siteaddress.com/site/_vti_bin/listdata.svc/Listname?$select=Data1,Data2,Data3,Data4,Data5&$skiptoken=2000",
            type: "GET",
            dataType: "json",
            headers: {
                Accept: "application/json;odata=verbose"
            }       
        });
		
 (call, call2).done(function (data,textStatus, jqXHR){
            	$('#example')
					.on('xhr.dt', function ( e, settings, json ) {
					for ( var i=0, ien=json.aaData.length ; i<ien ; i++ ) {
					json.aaData[i].sum = json.aaData[i].one + json.aaData[i].two;
					}
					// Note no return - manipulate the data directly in the JSON object.
					} )
				.dataTable({
					ajax: "data.json",
                       	"bServerside" : true,
						"bProcessing": false,
						"aaData": data.d.results,
						"aoColumns": [
							{ "mData": "Data1" },
							{ "mData": "Data2" },
							{ "mData": "Data3" },
							{ "mData": "Data4" },
							{ "mData": "Data5" }
							],
							"bRetrieve": true,
				//	"initComplete": function(settings, json) {
				//	alert( 'DataTables has finished its initialisation.' );
				//			}
							});

			call.fail(function (jqXHR,textStatus,errorThrown){
            alert("Error retrieving Tasks: " + jqXHR.responseText);

});
});
}
</script>

Thanks in advance to any suggestions how to tackle this.

解决方案

If you cannot avoid the limitation of 1000 records per Ajax call, you can use the code below.

It uses $.when to execute callback when both Ajax calls were successful.

function LoadData()
{
   var call1 = $.ajax({
      url: "https://SP2010_siteaddress.com/site/_vti_bin/listdata.svc/Listname?$select=Data1,Data2,Data3,Data4,Data5",
      type: "GET",
      dataType: "json",
      headers: {
         Accept: "application/json;odata=verbose"
      }       
   });

   var call2 = $.ajax({
      url: "https://SP2010_siteaddress.com/site/_vti_bin/listdata.svc/Listname?$select=Data1,Data2,Data3,Data4,Data5&$skiptoken=2000",
      type: "GET",
      dataType: "json",
      headers: {
         Accept: "application/json;odata=verbose"
      }       
   });

   // When both Ajax requests were successful
   $.when(call1, call2).done(function(a1, a2){
      // a1 and a2 are arguments resolved for the call1 and call2 ajax requests, respectively.
      // Each argument is an array with the following structure: [ data, statusText, jqXHR ]

      // Merge data from two Ajax calls
      var data = a1[0].d.results.concat(a2[0].d.results);

      $('#example').dataTable({
         "aaData": data,
         "aoColumns": [
            { "mData": "Data1" },
            { "mData": "Data2" },
            { "mData": "Data3" },
            { "mData": "Data4" },
            { "mData": "Data5" }
         ]
      });
   });
}

这篇关于一个Datatables.net表,具有来自Sharepoint 2010 REST API的多个ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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