Custom button to export filtered search results

Custom button to export filtered search results

bsukbsuk Posts: 92Questions: 26Answers: 2
edited March 2016 in Free community support

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

  • bsukbsuk Posts: 92Questions: 26Answers: 2
    edited March 2016

    I also tried:

    action: function ( e, dt, node, config ) {
        var data = table.rows({search:'applied'}).data().toArray();
        document.write (data);
        },
    

    And I get:

    [object Object],[object Object]

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49

    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?

  • bsukbsuk Posts: 92Questions: 26Answers: 2

    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.

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49
  • bsukbsuk Posts: 92Questions: 26Answers: 2

    Thanks @jr42.gordon

    I'll try that, and post my successful code back here once I get it all working. :)

  • bsukbsuk Posts: 92Questions: 26Answers: 2

    @jr42.gordon Thanks! That worked! :)

    { text: 'My Button',
        action: function ( e, dt, node, config ) {
            data = table.column( 7, {search:'applied'} ).data().toArray();
            data9 = JSON.stringify(data); 
            document.write(data9);
             }
        },
    
This discussion has been closed.