CSV Export header doesn't work

CSV Export header doesn't work

ClaudioPieriniClaudioPierini Posts: 2Questions: 1Answers: 0

Hi
I'm trying to add a header to my csv file, I found an example here
https://datatables.net/forums/discussion/comment/100228/#Comment_100228
but it looks like it's not working.
My header is a predefined string, but by putting a \n at the end of the string the button is not generated on the screen.
As explained in the example, by putting a \n header in the end of the string the line skips then showing the contents of the file
Here is my code:
var buttons = new $.fn.dataTable.Buttons(table, {"+ "buttons: [{"+ " extend:'csvHtml5',"+ " className: 'bluenavbutton',"+ " text:'<i class=\"glyphicon glyphicon-export font-button glyph-sm\"></i>&nbsp" + user.getString("csv") + "',"+ " title:'" + reportType + reportRequest.getParameter("period") + "',"+ " titleAttr: '" + user.getString("download_csv") + "',"+ " customize: function (csv) {"+ " return \""+header.toString()+"\" + csv;"+ " }"+ "}]"+ "}).container().appendTo($('#csvNavButton'));
If i don't put the \n at the end of the string the button works fine

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,871Questions: 1Answers: 10,525 Site admin
    Answer ✓

    Can you link to a test case showing the issue please.

    Allan

  • ClaudioPieriniClaudioPierini Posts: 2Questions: 1Answers: 0

    Hi Allan
    Thanks for your fast reply.
    I have managed to solve the problem by putting an extra "\" before the "\n".
    var buttons = new $.fn.dataTable.Buttons(table, {"+ "buttons: [{"+ " extend:'csvHtml5',"+ " className: 'bluenavbutton',"+ " text:'<i class=\"glyphicon glyphicon-export font-button glyph-sm\"></i>&nbspCSV',"+ " title:'" + reportType + reportRequest.getParameter("period") + "',"+ " titleAttr: 'CSV',"+ " customize: function (csv) {"+ " return \""+header.toString()+"\\n\" + csv;"+ " }"+ "}]"+ "}).container().appendTo($('#csvNavButton'));

    Thanks again !!

  • allanallan Posts: 63,871Questions: 1Answers: 10,525 Site admin

    Thanks for posting back - good to hear you've got it working!

    Allan

This discussion has been closed.