簡體   English   中英

在數據表 ajax 調用成功時調用 function

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

是否有可能在數據表 ajax 調用成功時調用 javascript function。 這是我嘗試使用的代碼,

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(){
    //.....code goes here
}

而不是“成功”:fnCallback ------> 這是 AJAX 調用的最后一行。 在這個 function 中,我想檢查從服務器端發送的值。

您可以使用 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" }

        ]
    } );

你可以使用這個:

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

我發現的最好方法是使用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
        }
    });

對於數據表 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.
      }
    });
  }
});

這對我來說很好用。 另一種方法行不通

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

ajax 的成功選項不應更改,因為 DataTables 在內部使用它來在數據加載完成時執行表格繪制。 建議使用“dataSrc”來更改接收到的數據。

根據文檔,當 Ajax 請求完成時,將觸發xhr Ajax 事件。 所以你可以做這樣的事情:

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

也許這不完全是您想要做的,但是使用 ajax complete 解決了我在 ajax 調用返回時隱藏微調器的問題。

所以它看起來像這樣

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

是否有可能在成功進行數據表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
                    });
                }

是否可能有類似的東西,

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

而不是“成功”:fnCallback ------>這是AJAX調用的最后一行。 在此功能中,我想檢查從服務器端發送的值。 在此先感謝您的幫助。

根據問題,我認為以下內容就足夠了:

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

嘗試以下代碼。

       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