Custom button to export filtered search results
Custom button to export filtered search results
I'm attempting to create a custom button that exports the filtered search results into a javascript array or variable, that can then be posted to PHP for processing within my app. Rather like the export to Excel button, but into a string or array rather than to a file:
{ text: 'My button',
action: function ( e, dt, node, config ) {
var data = table.row( $(this).parents('tr') ).data();
alert (data[5]) ;
}
},
However, the browser is hanging and not reporting anything back.
I must be missing something with the examples? I'm trying to combine the examples here:
https://datatables.net/reference/api/buttons.exportData()
and here:
https://datatables.net/extensions/buttons/examples/initialisation/plugins
https://datatables.net/extensions/buttons/examples/initialisation/custom.html
Failing my poor effort (apologies, I'm new to javascript), does anyone have a working example of a custom button that exports the filtered search results into something that can be fed into an array, or posted back to a server for processing?
(Ideally I'm only wanting to export the filtered results of a single column).
Many thanks.
Answers
I also tried:
And I get:
[object Object],[object Object]
I think you need to format the information in your 'data' variable before writing it to document. That variable contains each DataTable row's array of strings or object used to populate it. So I would expect you to get exactly what you stated, [object Object],[object Object]
Does your table only have 2 rows when filter applied?
Thanks for your reply. Yes, I was filtering down to two results when I tried that.
Any ideas how to format this correctly?
Or am I barking up the wrong tree, and should I be investigating this technique? :
https://datatables.net/forums/discussion/32380/
I'm fine with the PHP side of things (if I get that far), but I have no idea how to get that "click function" into a button.
Perhaps https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
Thanks @jr42.gordon
I'll try that, and post my successful code back here once I get it all working. :)
@jr42.gordon Thanks! That worked! :)