同一页面上具有不同ajax源的多个DataTable
我使用 dataTables 在单个页面上有多个表格。每个都需要有自己的“sAjaxSource”。我似乎无法确切地弄清楚如何做到这一点。这是我拥有的最少代码:
var oTable = $('.datatable').dataTable( {
"bProcessing": true,
"sAjaxSource": "/ajax/function",
"bSort": false,
"fnDrawCallback": function() {
//some click events initilized here
}
});
这基本上是最基本的设置。每个表作为数据表类和一个唯一的ID。但不确定如何根据特定表更改 AjaxSource。
谢谢你!
编辑:
这就是我最终所做的:
$('.datatable').each(function(index){
$('#'+$(this).attr('id')).dataTable( {
"bProcessing": true,
"sAjaxSource": $(this).children('caption').html(),
"bSort": false,
"fnDrawCallback": function() {
}
});
});
在表格内我放置了一个由 css 隐藏的标题标签,并包含 Ajax 源 URL。它遍历每个实例并获取 url。
到目前为止这似乎有效!
I have several tables on a single page using dataTables. Each needs to have it's own 'sAjaxSource'. I can't seem to figure out exactly how to do this. Here's the minimal code I have:
var oTable = $('.datatable').dataTable( {
"bProcessing": true,
"sAjaxSource": "/ajax/function",
"bSort": false,
"fnDrawCallback": function() {
//some click events initilized here
}
});
This is basically the bare bone setup. Each table as the datatable class and a unique ID. But not sure how to change the AjaxSource, based on a specific table.
Thank you!
EDIT:
Here's what I ended up doing:
$('.datatable').each(function(index){
$('#'+$(this).attr('id')).dataTable( {
"bProcessing": true,
"sAjaxSource": $(this).children('caption').html(),
"bSort": false,
"fnDrawCallback": function() {
}
});
});
Inside the table I put a caption tag that is hidden by css and contains the Ajax Source URL. It iterates through each instance and grabs the url.
This seems to be working so far!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了同样的问题,我使用与您类似的 html5 data- 属性和初始化代码解决了这个问题:
这样您就不必为每个 dataTable 创建一个 id
I had the same problem, which I solved using a html5 data- attribute and initialization code similar to yours:
that way you don't have to create an id for each dataTable
这行不通吗?它使用 id 而不是类来唯一标识每个数据表,并根据 id 将单独的源附加到每个表。
Will this not work? It uses the id rather than the class to uniquely identify each data table and attaches a separate source to each table based on the id.
您需要单独选择每个表并向其应用适当的 Ajax 数据源,以便获得所需的内容。现在您正在根据类名进行选择:
可能需要转换为:
我想如果您有很多表,这会变得乏味,但这几乎是您可以执行您建议执行的操作的唯一方法。
You would need to select each table sperately and apply the appropriate Ajax data source to it in order for you to get what you need. Right now you are selecting based on the class name:
will probably need to be converted to:
I guess this will get tedious if you have a lot of tables but that is pretty much the only way you can do what you are proposing to do.
您可以在同一页面上使用两个或多个。我已经做到了,数据表工作得很好。
数据表在本地存储数据,即使记录被异步删除。因此,我们需要明确的是,当我们搜索已删除的记录时,始终显示正确的结果。
由于数据表只需要初始化一次,我们需要记住,对于同一页面上的每个数据表,我们必须不断初始化数据表,因为它们存储本地记录,而我们不希望这样,因为在同一页面上我们确实存储/显示不同的数据。
所以。我们必须结合使用该方法,因为
这将解决问题。
You Can use two or more than that on the same page. I've done that and datatables works quite nicely.
Datatables stores the data locally even the records was removed from it asynchronously. Hence we need to make it clear always to show the correct result when we searched the removed records.
As datatables needs to initialized only once, we need to keep in mind, for each datatables on the same page we have to keep initializing the datables because they are storing the local records as we dont want that because on the same page we do store/ show different datas.
So. We have to use the method cohesively as
This will sort out the problem.