custom button to download all the data serverside

custom button to download all the data serverside

_mercury_mercury Posts: 9Questions: 3Answers: 0

how could i send a request like the same request sent by ( show all ) by a custom button , i prepared the server to respond by the csv file .. but i need a way to send a get request ( not by ajax ) by the same lonk that showcnall send ?

Answers

  • allanallan Posts: 57,730Questions: 1Answers: 9,203 Site admin

    Documentation on how to create a custom button is available here.

    If you don't want to use Ajax, you'd need to create a form and submit it. You can absolutely do that in a custom button (since it is just callback function, so you can execute any Javascript you want).

    Allan

  • _mercury_mercury Posts: 9Questions: 3Answers: 0

    But how to get the link to the send to? ..
    The ( show all ) link represent the current state?

  • kthorngrenkthorngren Posts: 17,754Questions: 25Answers: 4,215
    edited January 23

    But how to get the link to the send to?

    I would say that you need to create a link in your server to use that calls your function to create the CSV file. Then use that link in the custom button. There is nothing within Datatables to derive the link.

    Kevin

  • _mercury_mercury Posts: 9Questions: 3Answers: 0
    edited January 23

    Yes I did that on the server by** show all** button, but since it is a show all button .. datatables gives an error because it wants data, not CSV, also it is an ajax request which does not allow to download CSV file.
    so if I could know how to get the ( show all ) link and add it to the custom button ( 'I know how to create a custom button ) but I need the link because that link preserves the table state ( filters ... etc ).
    Or any other solution to the whole problem
    How to tell the backend ( the state of the table is like so ) and I want this data only !!
    I found the ( show all ) link is telling everything but how to get the link is not obvious for me !

  • kthorngrenkthorngren Posts: 17,754Questions: 25Answers: 4,215

    I'm not sure what your Show All button is doing. Can you post the code?

    This SO thread has some options for downloading a CSV and saving to a local file. Maybe one of those would work.

    How to tell the backend ( the state of the table is like so ) and I want this data only !!

    You can use the search(), order() and columns().search() APIs to get the state of the table to send as parameters.

    Kevin

  • _mercury_mercury Posts: 9Questions: 3Answers: 0

    I just want to know how show all button generate the link to simulate that

  • kthorngrenkthorngren Posts: 17,754Questions: 25Answers: 4,215

    I just want to know how show all button generate the link to simulate that

    Is this a button you currently have? If so please post the code for the button.

    Kevin

  • _mercury_mercury Posts: 9Questions: 3Answers: 0
    buttons: [
            {
                text: 'Export all',
                action: function ( e, dt, node, config ) {
                    dt.ajax.reload();
                }
            }
        ]
    
  • allanallan Posts: 57,730Questions: 1Answers: 9,203 Site admin

    Calling ajax.reload() isn't going to generate a CSV file (it will reload the data in the DataTable).

    You could do something like:

    window.location = '/generate-csv';
    

    and have that route on the server respond with a CSV file, which the browser can the download or display (depending on the MIME type and the browser's settings).

    If you need to pass information about the DataTable's state to the server, you can use state() to get the current state, and then add it as query parameters to your url.

    Allan

  • _mercury_mercury Posts: 9Questions: 3Answers: 0
    edited January 24

    thx alian
    how to convert the state to a link to be exact like show all link?

  • allanallan Posts: 57,730Questions: 1Answers: 9,203 Site admin

    Do you mean how to show the button as a link tag? If so, see this thread which is asking the same thing. You'll need to apply some custom styling if you want it to look like a regular link rather than a button.

    Alternatively, just do:

    <a href="/generate-csv">CSV</a>
    

    which sounds like basically the same thing - i.e. a custom link and have it be an a tag.

    Allan

Sign In or Register to comment.