【问题标题】:Datatables child.row using AJAX not refereshing使用 AJAX 的数据表 child.row 不刷新
【发布时间】:2018-08-24 20:41:02
【问题描述】:

我正在尝试使用 AJAX 获取数据表中的子行数据:

$('#myTable tbody').on('click', 'td', function () {
  var tr = $(this).closest('tr');
  var row = myTable.row( tr );   
    if ( row.child.isShown() ) {
      // This row is already open - close it
      row.child.remove();
      tr.removeClass('shown');
    } 
    else {
      $.post('/salesLines', 
        { token: localStorage.getItem('token'), 
           user: user.node, 
           id: localStorage.getItem('uniqueid')
      })
      .done(function(response) { 
        $.each(response.data, function (i, d) {  
        result += '<tr>'+'<td>'+d.qtysold+ '&nbsp;&nbsp;' + d.descr + '&nbsp;&nbsp;' + d.linetotal+'</td>'+'</tr>';
        row.child( $(result) ).show();  // use selector $() for result to align child rows with main table column headings
        tr.addClass('shown');
      });
    }
});

AJAX 请求似乎缓存了数据,即使它正在使用 $.post。

任何建议将不胜感激!

【问题讨论】:

  • $.post 未缓存。 Pages fetched with POST are never cached, so the cache and ifModified options in jQuery.ajaxSetup() have no effect on these requests.来自api.jquery.com/jQuery.post
  • 你试过简单的ajax吗? $.ajax({ url: "/myURL?", cache: false, });
  • 你确定它是在缓存而不是“显示与以前相同的东西” - 我问的原因是我没有看到你实际上在任何地方更新 html,除了用它制作一个字符串 (result )。
  • @James,是的,想知道result += '&lt;tr&gt;'+'&lt;td&gt;'+d.qtysold+ '&amp;nbsp;&amp;nbsp;' + d.descr + '&amp;nbsp;&amp;nbsp;' + d.linetotal+'&lt;/td&gt;'+'&lt;/tr&gt;'; 应该做什么......
  • 对不起,忘记了将结果附加到 DOM 的部分!尝试使用带有 cache: false 的普通 $.ajax 请求,结果是相同的。我应该使用 row.child.hide() 隐藏行吗?或 row.child.remove() ?

标签: javascript jquery node.js ajax datatables


【解决方案1】:

更改了 .on 点击​​事件
$('#myTable tbody').on('click', 'td', function () {

到:

$('#myTable tbody').on('click', 'tr', function () {

现在它可以工作了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多