Dynamic values as DataTables settings?

Dynamic values as DataTables settings?

jLinuxjLinux Posts: 981Questions: 73Answers: 75
edited September 2015 in Free community support

I was wondering if there was a way to dynamically get the value of some settings for DataTables, but only when they're used...

Not sure thats a very clear, so for example, when someone exports a table from my application, I was trying to have the name set as the current timestamp, this way it wouldn't be filename (1).pdf whenever new search results are exported.

I tried to pass an anonymous function as the title value...

new $.fn.dataTable.Buttons( $assets_dt, {
    buttons: [
        {
            extend: 'copy',
            className: 'btn btn-xs btn-primary p-5 m-0 width-35 assets-export-btn export-copy'
        },
        {
            extend: 'csv',
            className: 'btn btn-xs btn-primary p-5 m-0 width-35 assets-export-btn export-csv'
        },
        {
            extend: 'pdf',
            className: 'btn btn-xs btn-primary p-5 m-0 width-35 assets-export-btn export-pdf',
            title: function(){
                return 'SASSET-Assets-' + tools.date('%d-%M-%Y_%H:%m:%s')
            },
            extension: '.pdf'
        },
        {
            extend: 'print',
            className: 'btn btn-xs btn-primary p-5 m-0 width-35 assets-export-btn export-print'
        }
    ]
} );

tools = {
    date: function (format, timestamp) {
        if(typeof timestamp === 'undefined' || timestamp == ''){
            timestamp = $.now();
        }

        if(typeof format === 'undefined' || format == ''){
            format = '%d-%M-%Y %H:%m:%s';
        }

        var d = new Date(timestamp);
        
        function pad(value) {
            return (value.toString().length < 2) ? '0' + value : value;
        }

        return format.replace(/%([a-zA-Z])/g, function (_, fmtCode) {
            switch (fmtCode) {
                case 'Y':
                    return d.getUTCFullYear();
                case 'M':
                    return pad(d.getUTCMonth() + 1);
                case 'd':
                    return pad(d.getUTCDate());
                case 'H':
                    return pad(d.getUTCHours());
                case 'm':
                    return pad(d.getUTCMinutes());
                case 's':
                    return pad(d.getUTCSeconds());
                default:
                    throw new Error('Unsupported format code: ' + fmtCode);
            }
        });
    }
};

And resulted in the error: buttons.html5.js: 259 Uncaught TypeError: title.indexOf is not a function

Setting the timestamp when its initialized, such as

{
    extend: 'pdf',
        className: 'btn btn-xs btn-primary p-5 m-0 width-35 assets-export-btn export-pdf',
    title: 'SASSET-Assets-' + tools.date('%d-%M-%Y_%H:%m:%s'),
    extension: '.pdf'
},

works, but obviously the timestamp might not be the correct timestamp when the export is executed, it will be the same for every one (except for the (1)..)

If anyone has a solution theyd like to share, let me know! I searched for this, but everything I found was for the legacy DT & TableTools.

If theres no solution, I can have the name just be the current day, and it will add (1) on the end, that will work, just was really trying to avoid it. This isnt an "urgent" issue in my book, would just be nice to know if its possible.

Thanks!

This discussion has been closed.