Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery数据表从服务器返回附加信息_Jquery_Client_Datatables - Fatal编程技术网

Jquery数据表从服务器返回附加信息

Jquery数据表从服务器返回附加信息,jquery,client,datatables,Jquery,Client,Datatables,使用jquerydatatables,一切都很顺利 我已经研究出如何将附加信息从客户端发送到服务器。现在,我想回到另一条路 那么,如何从服务器向客户端发送额外的信息呢。我本以为我可以在返回的JSON中添加一个额外的条目,并将其拉到某个地方。我可能想发回的一项是服务器处理响应所用的时间。然后我可以向用户显示此信息 任何帮助都将不胜感激。谢谢我想你一切都很好。您只需要在JSON对象中附加附加的数据服务器端,然后在“fnServerData”中获取它。您可以将以下代码添加到Nizialization对

使用jquerydatatables,一切都很顺利

我已经研究出如何将附加信息从客户端发送到服务器。现在,我想回到另一条路

那么,如何从服务器向客户端发送额外的信息呢。我本以为我可以在返回的JSON中添加一个额外的条目,并将其拉到某个地方。我可能想发回的一项是服务器处理响应所用的时间。然后我可以向用户显示此信息


任何帮助都将不胜感激。谢谢

我想你一切都很好。您只需要在JSON对象中附加附加的数据服务器端,然后在“fnServerData”中获取它。您可以将以下代码添加到Nizialization对象:

        "fnServerData": function ( sSource, aoData, fnCallback ) {
            $.getJSON( sSource, aoData, function (json) {
//Here you can do whatever you want with the additional data
                console.dir(json);
//Call the standard callback to redraw the table
                fnCallback(json);
            } );
        }
在服务器端,您可以添加任意数量的参数:通常,json包含3个参数“iTotalRecords”(总行数)、“iTotalDisplayRecords”(如果使用过滤器,则为过滤总数)和aaData(带行的关联数组)。如果添加例如“IPProcessingTime”(处理服务器端所需的时间),则可以执行以下操作:

        "fnServerData": function ( sSource, aoData, fnCallback ) {
            $.getJSON( sSource, aoData, function (json) {
//take the processing time and put it in a div
                $('#processingTime').html(json.iProcessingTime);
//pass the data to the standard callback and draw the table
                fnCallback(json);
            } );
        }

这就是你需要的吗?

@Nicola Peluchetti的答案是正确的。但是如果您遵循这个示例,并且(出于某种原因)不想使用getJSON,那么这个方法也可以

        "fnServerData": function ( sSource, aoData, fnCallback ) {
            $.ajax({
                "dataType": 'json',
                "type": "POST",
                "url": sSource,
                "data": aoData,
                "success": function(json){
                    $('#processingTime').html(json.iProcessingTime); // iProcessingTime is the variable that we added in JSON at the server side
                    fnCallback(json);
                }
            });               
        }

还可以使用“fnInitComplete”函数从JSON文件访问信息,该函数在表的draw事件完成(包括datarows)后调用


上述建议没有帮助

我有一个ajax服务器端的可分页实现。当用户输入新的搜索词时,它必须刷新,因此使用“fnInitComplete”不是一个选项,因为它只在初始化DataTable对象时触发一次

覆盖fnServerData也不起作用

因此,我通过dataSrc从JSON获取
iProcessingTime
来结束它的实现:

var table = $('#pkgTable').DataTable({
    "processing" : true,
      "serverSide" : true,
      "sPaginationType" : "jPaginator",
      "ajax": {
          "url" : urlStr,
          "type" : "POST",
          "dataSrc": function(json) {
            var iProcessingTimeMS = json.iProcessingTime;
            var iProcessingTimeS = iProcessingTimeMS/1000;
            $("#processingTime").html("Search Time: " + iProcessingTimeMS + " ms. " + iProcessingTimeS + " s.");
          return json.aaData;
        }
      },
      "oLanguage": {
            "sProcessing":   "<span style='color: red; font-weight: bold'>Please Wait...</span>",
            "sZeroRecords":  "No Records Found...",
            "sSearch":       "Search All:",
            "sUrl":          "",
            "oPaginate": {
                             "sFirst"    : "<b>&lt;&lt;</b>",
                             "sLast"     : "<b>&gt;&gt;</b>",
                             "sPrevious" : "<b>&lt;</b>",
                             "sNext"     : "<b>&gt;</b>"
                    },
            "sLengthMenu": 'Display <select>' +
                    '<option value="10">10</option>' +
                    '<option value="20">20</option>' +
                    '<option value="50">50</option>' +
                    '<option value="100">100</option>' +
                    '</select> records'
        }
});
var table=$('#pkgTable').DataTable({
“处理”:对,
“服务器端”:正确,
“sPaginationType”:“jPaginator”,
“ajax”:{
“url”:urlStr,
“类型”:“职位”,
“dataSrc”:函数(json){
var iprocessingtimes=json.iProcessingTime;
var iProcessingTimeS=iProcessingTimeS/1000;
$(“#processingTime”).html(“搜索时间:+iProcessingTimeS+“ms.”+iProcessingTimeS+“s.”);
返回json.aaData;
}
},
“语言”:{
“sProcessing”:“请稍候…”,
“sZeroRecords”:“未找到任何记录…”,
“搜索”:“搜索全部”:,
“sUrl”:“,
“oPaginate”:{
“第一项”:“第三项”,
“sLast”:“,
“以前的”:“,
“sNext”:”
},
“显示菜单”:“显示”+
'10' +
'20' +
'50' +
'100' +
“记录”
}
});

$('#示例')。数据表({
“fnInitComplete”:函数(oSettings,json){
$('#category').html(json.category);
}
});

这对我来说似乎很好。

嘿,Lee,我正在研究如何使用fnServerData将附加信息从客户端发送到服务器。你能帮我吗@你能帮我把参数从客户端发送到服务器吗。我正在尝试将html页面上所选组合框的值传递给控制器,该控制器是我的AjaxSource和我的datatable。这正是我所寻找的。这正是我所需要的-上述建议对我不起作用。我需要的只是一个回调来处理从服务器获取的额外数据,而不影响datatables的ajax功能。
"fnDrawCallback": function( oSettings ) {
    console.log(oSettings.json);//do whatever with your custom response
  },
<div id="category"></div>

$('#example').dataTable( {
    "fnInitComplete": function(oSettings, json) {
      $('#category').html(json.category);
    }
  });
"fnDrawCallback": function( oSettings ) {
    console.log(oSettings.json);//do whatever with your custom response
  },