Add date to the exported filename.

Add date to the exported filename.

blockheadsilverblockheadsilver Posts: 12Questions: 0Answers: 0
edited May 2013 in TableTools
How to add date to the filename when exporting datatables as pdf or excel ?

Replies

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    Use the sFileName option: http://datatables.net/extras/tabletools/button_options#sFileName

    Allan
  • blockheadsilverblockheadsilver Posts: 12Questions: 0Answers: 0
    Hi Allan,

    I know about that option but i have stored the date in a variable and then i want to put it into the filename...so when ever the user exports thje pdf he would get current date into the filename.


    for date : var currentDate = new Date()
    var day = currentDate.getDate()
    var month = currentDate.getMonth() + 1
    var year = currentDate.getFullYear()

    var d = day + "/" + month + "/" + year;


    then what shall i put into sFilename : ""
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    Then use `sFileName: d` no?

    Allan
  • blockheadsilverblockheadsilver Posts: 12Questions: 0Answers: 0
    sFileName: d doesnt work it only takes a string as parameter.
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    d is a string from your above code is it not?
  • blockheadsilverblockheadsilver Posts: 12Questions: 0Answers: 0
    Hi Allan,

    The above thing sFileName=d; is not working,please help me on this...I have got the date into a variable:
    for date : var currentDate = new Date()
    var day = currentDate.getDate()
    var month = currentDate.getMonth() + 1
    var year = currentDate.getFullYear()

    var d = day + "/" + month + "/" + year;

    Now i want to put it into my export filename along with the table name


    "sDom": 'T<"H"lfr>t<"F"ip>',
    "oTableTools": {

    "sSwfPath": "media/swf/copy_csv_xls_pdf.swf",
    "aButtons": [

    "copy",
    "print",

    {
    "sExtends": "collection",
    "sButtonText": "Export",
    //"mColumns": "visible",
    "aButtons": [
    {
    "sExtends": "csv",
    "sButtonText": "CSV",
    "mColumns": "visible"
    },
    {
    "sExtends": "xls",
    "sButtonText": "Excel",
    "mColumns": "visible"
    },
    {
    "sExtends": "pdf",
    "sButtonText": "PDF",
    "sPdfOrientation": "landscape",
    "sFileName": d,
    "mColumns": "visible"
    }]
    }
    ]
    }


    I had put the sFileName:d; but it doesnt even open the pdf saving box now..nothing happens when the pdf button is clicked...please suggest me on this or if you may have some other option which can help...
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    Looks like it should to me. Can you link to a page showing the problem please.

    Allan
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    You probably want to include the file extension btw.
  • blockheadsilverblockheadsilver Posts: 12Questions: 0Answers: 0
    I cant its for an client application :( ...if you could try it out on a datatable to use sFileName="somename"+d; and see if that works cause for me I was using it for the pdf option but now the pdf option itself is not working(nothing happens if clicked on pdf).
  • blockheadsilverblockheadsilver Posts: 12Questions: 0Answers: 0
    Not working with the .pdf extension aswell...:(
  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin
    It's the `/` in the file name. That's invalid on most operating systems. Apparently Flash doesn't give a working about it.

    Use `-` instead. This works for me:

    [code]
    $(document).ready( function () {
    var currentDate = new Date()
    var day = currentDate.getDate()
    var month = currentDate.getMonth() + 1
    var year = currentDate.getFullYear()

    var d = day + "-" + month + "-" + year;

    $('#example').dataTable( {
    "sDom": 'T<"clear">lfrtip',
    oTableTools: {
    aButtons: [ {
    sExtends: 'csv',
    sFileName: d+'.csv'
    } ]
    }
    } );
    } );
    [/code]

    Allan
  • blockheadsilverblockheadsilver Posts: 12Questions: 0Answers: 0
    Thanks a lot Allan...:)
    It worked...
This discussion has been closed.