在datatable ajax调用中成功调用函数

raj*_*tsm 29 javascript ajax jquery datatables

是否可以在datatable ajax调用中成功调用javascript函数.这是我尝试使用的代码,

var oTable = $('#app-config').dataTable(
            {
                "bAutoWidth": false,                                                
                "bDestroy":true,
                "bProcessing" : true,
                "bServerSide" : true,
                "sPaginationType" : "full_numbers",
                "sAjaxSource" : url,                    
                "fnServerData" : function(sSource, aoData, fnCallback) {
                    alert("sSource"+ sSource);
                    alert("aoData"+ aoData);
                    $.ajax({
                        "dataType" : 'json',
                        "type" : "GET",
                        "url" : sSource,
                        "data" : aoData,
                        "success" : fnCallback
                    });
                }
Run Code Online (Sandbox Code Playgroud)

是否可能有类似的东西,

success : function(){
    //.....code goes here
}
Run Code Online (Sandbox Code Playgroud)

而不是"成功":fnCallback ------>这是AJAX调用的最后一行.在这个函数中,我想检查从服务器端发送的值.在此先感谢任何帮助....

Jos*_*one 46

你可以使用dataSrc:

这是datatables.net的典型示例

var table = $('#example').DataTable( {
    "ajax": {
            "type" : "GET",
            "url" : "ajax.php",
            "dataSrc": function ( json ) {
                //Make your callback here.
                alert("Done!");
                return json.data;
            }       
            },
    "columns": [
            { "data": "name" },
            { "data": "position" },
            { "data": "office" },
            { "data": "extn" },
            { "data": "start_date" },
            { "data": "salary" }

        ]
    } );
Run Code Online (Sandbox Code Playgroud)

  • 它对我有用,但不适用于返回“json.data”,仅返回“json”,谢谢 (2认同)

Chr*_*ris 30

我找到的最好的方法是使用initComplete方法,因为它在检索数据后触发并呈现表.注意这只会发射一次.

$("#tableOfData").DataTable({
        "pageLength": 50,
        "ajax":{
            url: someurl,
            dataType : "json",
            type: "post",
            "data": {data to be sent}
        },
        "initComplete":function( settings, json){
            console.log(json);
            // call your function here
        }
    });
Run Code Online (Sandbox Code Playgroud)

  • 除非刷新表,否则永远不会再次调用initComplete,因为它已被初始化。 (2认同)

Ana*_*afi 25

你可以用这个:

"drawCallback": function(settings) {
   console.log(settings.json);
   //do whatever  
},
Run Code Online (Sandbox Code Playgroud)

  • 这是问题的解决方案,特别是当您使用服务器端处理来绘制数据表时。 (3认同)

Gia*_*nis 17

这对我来说效果很好。另一种方法效果不好

                'ajax': {
                    complete: function (data) {
                        console.log(data['responseJSON']);
                    },
                    'url': 'xxx.php',
                },
Run Code Online (Sandbox Code Playgroud)


小智 6

对于数据表1.10.12.

$('#table_id').dataTable({
  ajax: function (data, callback, settings) {
    $.ajax({
      url: '/your/url',
      type: 'POST',
      data: data,
      success:function(data){
        callback(data);
        // Do whatever you want.
      }
    });
  }
});
Run Code Online (Sandbox Code Playgroud)

  • 对我来说,这会阻止填充表的过程。 (5认同)