[英]Fire event after datatables table finishes rendering
I am using datatables and fetching the data from an ajax URL. 我正在使用数据表,并从ajax URL获取数据。 Each row of my returned table data includes a field that renders a button that looks like this:
我返回的表格数据的每一行都包含一个字段,该字段呈现如下所示的按钮:
<button type="button" data-catid="8" data-catname="Programming:JavaScript"></button>
The values assigned data-catid and data-catname come from the datatables retrieved data. 分配给data-catid和data-catname的值来自检索到的数据表。
In certain cases, when the table finishes loading, I want to trigger a click on one of these buttons. 在某些情况下,表格加载完成后,我想触发对这些按钮之一的单击。 My code for that is this:
我的代码是这样的:
$('#mydatatable').find('button[data-catid="9"]').trigger('click');
However, I cannot find a way to do this so that it occurs after the table has rendered and the button exists in the dom so I can find it and trigger the click. 但是,我找不到执行此操作的方法,以使其在呈现表格并在dom中存在按钮之后发生,因此我可以找到它并触发单击。
I have tried drawCallback and initComplete but neither of those is triggered after the button has actually been added to the dom, allowing me to find it with jquery. 我已经尝试过drawCallback和initComplete,但是在将按钮实际添加到dom后,这两个都没有触发,这使我可以使用jquery找到它。
Is there a way I can do this? 有办法吗? ie trigger my click after the mytable has finished retrieving its data via ajax and rendered it?
即在mytable通过ajax检索并呈现数据后触发我的点击?
I am using datatables v 1.10 我正在使用数据表v 1.10
EDIT: Here is how my click event handler is attached to the summary table. 编辑:这是我的单击事件处理程序如何附加到摘要表。
var selectedCat = 0;
$('#mydatatable :button').click(function () {
selectedCat = this.getAttribute("data-catId");
console.log("selecteCat is " + selectedCat);
qDetailTable.ajax.url('/datatables/question-data/' + selectedCat).load();
var selectedCatName = this.getAttribute("data-catName");
$('#questDetailCat').text('Questions about: ' + selectedCatName);
$('#questSummary').hide();
$('#questDetail').show();
});
Move the click handler into a separate function, for example: 将点击处理程序移到单独的函数中,例如:
var selectedCat = 0; function OnCategoryClick(btn){ selectedCat = btn.getAttribute("data-catId"); console.log("selecteCat is " + selectedCat); qDetailTable.ajax.url('/datatables/question-data/' + selectedCat).load(); var selectedCatName = btn.getAttribute("data-catName"); $('#questDetailCat').text('Questions about: ' + selectedCatName); $('#questSummary').hide(); $('#questDetail').show(); });
Event handler needs to be in a named function because you will not be able to trigger click event for elements that are not in DOM as with jQuery DataTables. 事件处理程序必须位于命名函数中,因为您将无法像jQuery DataTables一样触发不在DOM中的元素的click事件。
Change how you attach your click handler. 更改附加点击处理程序的方式。
$('#mydatatable').on('click', 'button', function () { OnCategoryClick(this); });
Remember to always use delegated event handlers with elements inside jQuery DataTables because elements for pages other than first will not be available in DOM. 请记住,始终将委托事件处理程序与jQuery DataTables中的元素一起使用,因为除first之外的页面的元素在DOM中将不可用。 See jQuery DataTables – Why click event handler does not work for more details.
有关更多详细信息,请参见jQuery DataTables –为什么单击事件处理程序不起作用 。
Use $()
API method to locate required button and call your handler function OnCategoryClick()
. 使用
$()
API方法找到所需的按钮,然后调用处理函数OnCategoryClick()
。
var btn = $('#datatable-summary').DataTable().$('button[data-catid="9"]').get(0); OnCategoryClick(btn);
Replace datatable-summary
with your actual ID of the summary table. 用您的摘要表的实际ID替换
datatable-summary
。
If you need to update details as soon as the summary table is initialized and drawn, use initComplete
option to define a callback and do it there. 如果您需要在初始化和绘制摘要表后立即更新详细信息,请使用
initComplete
选项定义回调并在其中进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.