多个AJAX调用表初始化多次? [英] Multiple AJAX calls for a table initialized more than once?

查看:177
本文介绍了多个AJAX调用表初始化多次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题很棘手,所以我会发布上下文:

The question is tricky so I'll post the context:


  1. 我在页面上有一个片段,通过AJAX加载。

  2. 该页面包含一个表,由Datatables 由服务器端处理启动并填充。

  3. 每次我加载包含新表的片段,我告诉Datatables从头开始使用 bDestroy:true

  1. I have a fragment on the page that gets loaded via AJAX.
  2. That page contains a table that will be powered and populated by Datatables with server-side processing.
  3. Everytime I load the fragment that contains a new table, I tell Datatables to boot the table from scratch, using bDestroy : true.



问题



将JSON数据作为JSON保持打桩的AJAX调用当我通过AJAX加载新的表格时。

Problem

The AJAX calls that fetch the data as JSON keep piling up as I load new tables via AJAX.

如何保持这些呼叫的堆叠和将它们减少到单个?谢谢。

How do I keep these calls from stacking up and reduce them to a single one? Thank you.

/* datatables initializer */

$("#table").dataTable({
  bDestroy : true
, bServerSide : true
, sAjaxSource : "path/to/json.json"
, ...
});

/* script inside the AJAX loaded content. It outputs a <table>. */

$(function() {
  $("body").trigger({ type : "tableready", options : { ... } });
});


推荐答案

这个问题的答案超出了DataTables的责任心。上面的代码示例缺少问题的原因。

The answer to this issue is out of DataTables' responsability. The code sample above is missing the cause of the problem.

我已经建立了一个将绑定的事件发送给赋予表的功能。每次我调用检索表的AJAX调用,函数堆叠在顶部的另一个绑定,因此多次调用数据。

I have built a class that binds an event to a function that empowers the table. Everytime I invoked the AJAX call that retrieved the table, the function stacked another binding on top, hence the multiple calls for data.

解决方案是检查是否有已经绑定了这个事件,或者再次向 unbind()绑定事件,然后再重新发送一个 bind()

The solution is either to check if you have already bound that event or to unbind() the event and rebind() it again.

这篇关于多个AJAX调用表初始化多次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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