[英]Jquery-DataTables catch search event but not those from ordering
I am rendering a graph of the data in the Jquery DataTables table and would like to trigger a function to re-draw the graph upon a change in the table contents from the UI search OR calling a search such as below我正在渲染 Jquery DataTables 表中的数据图,并希望在 UI 搜索中的表内容发生更改或调用如下搜索时触发一个函数重新绘制该图
$('#mytable').DataTable().column(0).search('my value').draw()
on my table I have the following code在我的桌子上,我有以下代码
var table = $('#mytable').DataTable({
//my settings here
}).on( 'search.dt', function () { updateGraph( GraphData ) ; } );
The code is working but on a sort event, such as ordering a column a search followed by an order event is triggered.该代码正在运行,但在排序事件上,例如对列进行排序,然后触发搜索和排序事件。 Is there a way to trigger a change only when the table contents change?
有没有办法仅在表内容更改时触发更改?
see this example for further information.有关更多信息,请参阅此示例。https://datatables.net/examples/advanced_init/dt_events.html
https://datatables.net/examples/advanced_init/dt_events.html
That was a good catch!这是一个很好的捕获! Never realized that the
search.dt
event was triggered even on simple ordering.从未意识到即使是简单的排序也会触发
search.dt
事件。 A peculiar issue actually.其实是个奇葩问题。 Perhaps other people will come up with a better idea, but I think keeping a "snapshot" of the current rows and compare them to the rows when
search.dt
is triggered could solve the problem.也许其他人会想出更好的主意,但我认为保留当前行的“快照”并将它们与触发
search.dt
时的行进行比较可以解决问题。 If the snapshot
is based on the table rows default order, or index
, not by the applied ordering, then we could trigger a datachange.dt
event if and only if data actually has changed within the dataTable :如果
snapshot
基于表行默认顺序或index
,而不是应用顺序,那么当且仅当 dataTable 中的数据实际发生更改时,我们才能触发datachange.dt
事件:
var table = $('#example').DataTable({
snapshot: null
})
.on('search.dt', function() {
var snapshot = table
.rows({ search: 'applied', order: 'index'})
.data()
.toArray()
.toString();
var currentSnapshot = table.settings().init().snapshot;
if (currentSnapshot != snapshot) {
table.settings().init().snapshot = snapshot;
if (currentSnapshot != null) $('#example').trigger('datachange.dt')
}
})
.on('datachange.dt', function() {
alert('data has changed')
//updateGraph( GraphData )
})
demo -> http://jsfiddle.net/hftuew5u/演示 -> http://jsfiddle.net/hftuew5u/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.