简体   繁体   English

ajax成功函数无法处理数据表

[英]ajax success function not working on datatable

I'm using datatable to show list from database mysql 我正在使用datatable来显示数据库mysql中的列表

I need to update some input on end of table loading, then I'm using success function but this seems to prevent data rendering 我需要在表加载结束时更新一些输入,然后我使用成功函数,但这似乎阻止了数据呈现

var table = $('#example').DataTable({
'processing': true,
'serverSide': true,
'ajax': {
  type: 'POST',
  'url': url,
  'data': function (d) {
    console.log(d.order);
    return JSON.stringify( d );
  },

  // EDIT this "my" success function
  success: function( data ) {
    $('#my_input').val(data.return);
  }
}

Json returned is: Json回来的是:

{
 "data":[[ (datatable value) ]],
 "returned":"myvalue"
}

here the jsfiddle 这里是jsfiddle

EDIT http://jsfiddle.net/ntcwust8/95/ 编辑http://jsfiddle.net/ntcwust8/95/

Just remove the success callback. 只需删除success回调即可。

success - Must not be overridden as it is used internally in DataTables. success - 不得覆盖,因为它在DataTables内部使用。 To manipulate / transform the data returned by the server use ajax.dataSrc (above), or use ajax as a function 要操作/转换服务器返回的数据,请使用ajax.dataSrc(上面),或使用ajax作为函数

Datatable by default handles the success callback, Don't override it. 默认情况下, 数据表处理success回调,不要覆盖它。

Instead use complete option of AJAX to do something after data loading. 而是使用completeAJAX选项在数据加载后执行某些操作。

Updated fiddle 更新了小提琴

You just need to remove success callback. 您只需要删除成功回调。

var table = $('#example').DataTable({
        'processing': true,
        'serverSide': true,
        'ajax': {
          type: 'POST',
          'url': url,
          'data': function (d) {
            console.log(d.order);
            return JSON.stringify( d );
          }
        }

Edit 编辑

you need to use ajax.dataSrc property it will call after ajax finish. 你需要使用它将在ajax完成后调用的ajax.dataSrc属性。 It will work on refresh as well https://datatables.net/reference/option/ajax.dataSrc 它也适用于刷新以及https://datatables.net/reference/option/ajax.dataSrc

var table = $('#example').DataTable({
    'ajax': {
      type: 'POST',
      'url': url,
      'data': function (d) {
        console.log(d.order);
        return JSON.stringify( d );
      },
      "dataSrc": function (json) {
       $("#mydata").val(json.recordsTotal);
       return json.data;
        }
    },

  });

here is updated fiddle. 这里是更新的小提琴。 http://jsfiddle.net/2jkg3kqo/2/ http://jsfiddle.net/2jkg3kqo/2/

Datatable has its own complete event thats called initComplete . Datatable有自己的complete事件,称为initComplete

If you redefine success you are overriding Datatable's own function. 如果重新定义success ,则会覆盖Datatable自己的功能。

var table = $('#example').DataTable({
    'processing': true,
    'serverSide': true,
    'ajax': {
    ....
    ....
    },    
   'initComplete':function(settings, json){
        alert($('#example tbody tr').length+ ' records on screen');
 });

Reference: https://datatables.net/reference/option/initComplete 参考: https//datatables.net/reference/option/initComplete

Update fidle: http://jsfiddle.net/ntcwust8/94/ 更新信息: http//jsfiddle.net/ntcwust8/94/

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

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