Button export csvHtml5 - skip download

Button export csvHtml5 - skip download

ltdetaltdeta Posts: 23Questions: 9Answers: 0
edited April 3 in Buttons

Hi,
I want to send the CSV data to the server and not download it
i am using the following export settings.

! exportOptions: {
! modifier: {
! search: 'applied',
! page : 'all'
! },
! columns: ':not(:first-child)',
! columns: ':visible'
! },
! extension: '.csv',
! fieldSeparator: ';',

How do I prevent only the download?

Answers

  • kthorngrenkthorngren Posts: 20,369Questions: 26Answers: 4,779
    edited April 3

    You can create a custom button to get teh table data and send to the server via jQuery ajax(). Use tows().data() with the row-selector of { search: "applied" }, ie, dt.rows( { search: "applied" } ).data() in the customer button function. You may need to use toArray() to convert the rows().data() result to a Javascript array before sending it via jQuery ajax().

    Kevin

  • ltdetaltdeta Posts: 23Questions: 9Answers: 0
    edited April 3

    Thanks,
    can i use the other options too?
    I want the same result using csv-Export config

    modifier: { page : 'all' }, columns: ':not(:first-child)', columns: ':visible'

  • kthorngrenkthorngren Posts: 20,369Questions: 26Answers: 4,779

    Sorry I gave the wrong doc link for { search: "applied" }. See the selector-modifier docs. It also shows the default is { page : 'all' }.

    I want the same result using csv-Export config

    {
    page : 'all'
    },
    columns: ':not(:first-child)',
    columns: ':visible'
    

    The rows() API doesn't support an option to supply which columns to return.modifier: You have defined the columns option twice. One will overwrite the other so only one of the options will be applied. Code will need to be written to take the rows().data() results and copy the appropriate columns to a new array. I provided an example:
    https://live.datatables.net/yakeyasu/1/edit

    It uses columns().visible() to get an array of booleans with the column visibility. It uses columns().title() (only in Datatables 2.0) to get the header titles. It uses rows().every() with the selector-modifier of { search: 'applied' } to loop through all the searched rows. This code is used to keep only the visible columns and not the first column.

                  visibleData.push( 
                    data.filter(function(value, index) {
                      // Keep only visible columns and not the first column
                      return colVisibility[index] === true && index !== 0;
                    })
                  );
    

    The visibleData result can then be sent via ajax to the server.

    Kevin

  • ltdetaltdeta Posts: 23Questions: 9Answers: 0

    Kevin, many thanks that saves me a lot of time!

    Perhaps you have a solution (< DT 2.0) to determine the column related header titles?
    I am currently still using the last DT 1.x version

  • ltdetaltdeta Posts: 23Questions: 9Answers: 0
    edited April 4

    i found a solution for the header titles

        dt.columns().iterator('column', function (obj, colIndex) {
          if (colVisibility[colIndex] === true && colIndex !== 0) {
            titles.push($(dt.column(colIndex).header()).text());
          }
        });
    
Sign In or Register to comment.