简体   繁体   English

在数据表 ajax 调用成功时调用 function

[英]call a function in success of datatable ajax call

Is that possible to invoke a javascript function in success of datatable ajax call.是否有可能在数据表 ajax 调用成功时调用 javascript function。 Here is the code am trying to use,这是我尝试使用的代码,

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

is it possible to have something like,有没有可能有类似的东西,

success : function(){
    //.....code goes here
}

instead of "success": fnCallback ------> which is last line of AJAX call.而不是“成功”:fnCallback ------> 这是 AJAX 调用的最后一行。 In this function I would like to check a value send from server side.在这个 function 中,我想检查从服务器端发送的值。

You can use dataSrc :您可以使用 dataSrc :

Here is a typical example of datatables.net这是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" }

        ]
    } );

You can use this:你可以使用这个:

"drawCallback": function(settings) {
   console.log(settings.json);
   //do whatever  
},

The best way I have found is to use the initComplete method as it fires after the data has been retrieved and renders the table.我发现的最好方法是使用initComplete方法,因为它在检索数据并呈现表后触发。 NOTE this only fires once though.请注意,这只会触发一次。

$("#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
        }
    });

For datatables 1.10.12.对于数据表 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.
      }
    });
  }
});

This works fine for me.这对我来说很好用。 Another way don't work good另一种方法行不通

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

The success option of ajax should not be altered because DataTables uses it internally to execute the table draw when the data load is complete. ajax 的成功选项不应更改,因为 DataTables 在内部使用它来在数据加载完成时执行表格绘制。 The recommendation is used "dataSrc" to alter the received data.建议使用“dataSrc”来更改接收到的数据。

Based on the docs , xhr Ajax event would fire when an Ajax request is completed.根据文档,当 Ajax 请求完成时,将触发xhr Ajax 事件。 So you can do something like this:所以你可以做这样的事情:

let data_table = $('#example-table').dataTable({
        ajax: "data.json"
    });

data_table.on('xhr.dt', function ( e, settings, json, xhr ) {
        // Do some staff here...
        $('#status').html( json.status );
    } )

Maybe it's not exactly what you want to do, but using the ajax complete solved my problem of hiding a spinner when the ajax call returned.也许这不完全是您想要做的,但是使用 ajax complete 解决了我在 ajax 调用返回时隐藏微调器的问题。

So it would look something like this所以它看起来像这样

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

Is that possible to invoke a javascript function in success of datatable ajax call.是否有可能在成功进行数据表Ajax调用时调用javascript函数。 Here is the code am trying to use,这是我要使用的代码,

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

is it possible to have something like,是否可能有类似的东西,

success : function(){
    //.....code goes here
}

instead of "success" : fnCallback ------> which is last line of AJAX call.而不是“成功”:fnCallback ------>这是AJAX调用的最后一行。 In this function I would like to check a value send from server side.在此功能中,我想检查从服务器端发送的值。 Thanks in advance for any help....在此先感谢您的帮助。

As per the question, I think the following should suffice:根据问题,我认为以下内容就足够了:

 'ajax': {
              complete: function (data) {
               console.log(data['responseJSON']);
             },
                
            },

Try Following Code.尝试以下代码。

       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
                }).success( function(){  alert("This Function will execute after data table loaded");   });
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM