jQuery DataTables重新加载间隔错误 [英] jQuery DataTables reload interval error

查看:102
本文介绍了jQuery DataTables重新加载间隔错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的表定期加载.我现在收到以下错误:

I'm trying to get my table to load on intervals. I am now receiving the below error:

TypeError: g is null

用户将输入表单参数,然后单击具有单击事件的提交"按钮.如下:

The user will enter form parameters and then hit the submit button which has a click event. As follows:

$('.searchSubmit').on('click', function()
{
  var data = {
    searchCriteria: {
      bill: $('#import_bill').val(), 
      ramp: $('#import_ramp').val(), 
      // few other parameters
    }
  };

  $.ajax({
    url: 'api/railmbs.php',
    type: 'POST',
    data: data,
    dataType: 'html',
    success: function(data, textStatus, jqXHR)
    {
      var jsonObject = $.parseJSON(data); 
      var table = $('#example1').DataTable({    
        "data": jsonObject,
        "columns": [
          { "data": "BILL" },   
          { "data": "RAMP" },   
          // few more columns
        ],
        "iDisplayLength": 25,
        "paging": true,
        "bDestroy": true,
        "stateSave": true,
        "autoWidth": true
      });

      var idle = 0;
      var idelInterval = setInterval(timer, 10000);
      $(this).mousemove(function(e){idle = 0;});
      $(this).keypress(function(e){idle = 0;});
      function timer()
      {
        idle = idle + 1;
        if(idle > 2)
        {
          $('#example1').DataTable().ajax.reload();  // <--error occurs here
          console.log('table reloaded');        
        }
      }
    },
    error: function(jqHHR, textStatus, errorThrown)
    {
      console.log('fail');
    }
  });
});

这是有趣的部分...上面,我指出了错误发生的位置,我本来是这样的:

Here's the funny part...above, where I pointed to where the error was occurring, I originally had it looking like this:

$('#example').DataTable().ajax.reload(); 

请注意,表名是"example"而不是"example1".表ID确实是example1,正如我在成功功能开始处附近指出的那样.当我看到重新加载间隔正在查看另一个表ID时,我对其进行了更改,这现在在顶部导致了错误.

Notice the table name was 'example' instead of 'example1'. The table ID is indeed example1, as I indicated up near where the success function begins. When I saw the reload interval was looking at a different table ID, I changed it, which now is causing the error at the top.

我认为我不应该将ID保留为示例",因为那不是正确的ID.

I don't think I should keep the ID as 'example' because that is not the correct ID.

话虽如此,我为什么会收到错误消息?

With that said, why am I getting the error?

推荐答案

您希望ajax.reload()如何工作?没有正在使用的AJAX,因此没有要重新加载的以前的AJAX.改为这样做(示意):

How do you expect ajax.reload() to work? There is no AJAX in use and therefore no previous AJAX to reload. Do this instead (schematic) :

var table = $('#example1').DataTable({    
   ajax: {
     url: 'api/railmbs.php',
     data: function() {
        return { 
          searchCriteria: {
            bill: $('#import_bill').val(), 
            ramp: $('#import_ramp').val(), 
            // few other parameters
          } 
        }
     }
   },
   "columns": [
      { "data": "BILL" },   
      { "data": "RAMP" },   
      // few more columns
   ],
   "iDisplayLength": 25,
   "paging": true,
   "bDestroy": true,
   "stateSave": true,
   "autoWidth": true
});

现在,您应该可以在可以使用table的任何地方使用table.ajax.reload().

Now you should be able to table.ajax.reload() from anywhere where table is available.

这篇关于jQuery DataTables重新加载间隔错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆