Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

At the moment, I need a syntax that changes the base name of the CSV files that are downloaded.

I need that: When CSV's are downloaded, instead of a standardized name for all files, tell me the name of the distributor in question.

Example:
Let's say I have a dispatcher where there are several records inside a table called "patients". The moment I download that information from this table, my current code creates a CSV file with table name 1, 2, 3, 4 and so on.

If my distributor is SG459300, I want the downloaded table to be named SG459300.

What I have tried:

JavaScript
</script>

<!-- Datatable -->
<script type="text/javascript" src="../assets/js/datatables.min.js"></script>

<script src="../assets/js/table2csv.js"></script>
<script type="text/javascript">


function export2CSV(tableID)
{

	var table = $("#"+tableID).DataTable();
	 
	    table.page.len( -1 ).draw();

	
	$("#"+tableID).table2csv();
	
}

	//Automatically instanciate a datatable
    $( ".datatable" ).each(function( index ) {

  	  var orderCol = 0;
  	  var orderOrd = 'asc';

    	if ($(this).attr('data-orderdtcol')) {
    		orderCol = $(this).data('orderdtcol');
        }
    	if ($(this).attr('data-orderdtord')) {
    		orderOrd = $(this).data('orderdtord');
        }

    	$(this).dataTable(
				{
    				lengthMenu: [
    				             [ 10, 25, 50, 100, -1 ],
    				             [ '10', '25', '50', '100' , 'All' ]
    				         ],
           		 responsive: true,
           		 "order": [[ orderCol, orderOrd ]],
				 "stateSave": true,
            "language": {
                "url": "../Translations/<?=($_SESSION['user'] instanceof User)?$_SESSION['user']->getLang():'en_US';?>/LC_MESSAGES/datatable.json"
            }
        } );
    });

</script>
Posted
Updated 13-Dec-21 3:10am
v2

1 solution

Assuming you're using this jQuery plugin[^] (correct me if I'm wrong) you just need to look at the documentation on that page to work out what you need to provide.
These options apply only when the 'download' action is used

filename
default: 'table.csv'
This is the name given to the file when the 'download' action is invoked

So according to your code you'd need to call:
JavaScript
.table2csv('download', {
  filename: 'whatever_file_name_you_want.csv'
});
 
Share this answer
 
Comments
Wi-Per 13-Dec-21 9:48am    
Perfect! Incredible. I tried it and it works. If possible, I would like to ask another question: And if I want to take the code of the distributor that I'm logged in and automatically play it in the name of my CSV, I would need to get this?

"< ?=$distributorCode? >.csv"
Chris Copeland 13-Dec-21 11:20am    
Presumably yes if that's where your distributor code is being stored, that would be how to output it. Just be careful in case your distributor code somehow ended up with single or double quotation marks, you might need to use addslashes() to escape them.

Also be very careful that the distributor codes you're talking about cannot be manually input by users. Someone could maliciously add extra data which would break the script and run JS code unintentionally 😊
Wi-Per 14-Dec-21 8:56am    
I got it, thank you very much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900