How do you export all data to a csv file (serverside) using a button
How do you export all data to a csv file (serverside) using a button
gnwankwo
Posts: 1Questions: 1Answers: 0
//sails backend makes a call to this function
download: function( req, res){
var page = req.param("draw");
var columns = req.param("columns");
var searchChars = req.param("search").value;
var where = (searchChars ===''?{}:{ email : { 'contains' : searchChars } });
User.count({}, function (err, count) {
User.find(where).then (function (users) {
return res.end(JSON.stringify({ data: users }));
});
});
}
};
//front end
var user_table = $('#datatable').dataTable({
"columnDefs": [ { "searchable": true, "targets": 3 }],
"processing": true,
"ordering": false,
"bFilter": true,
"scrollX": false,
"ajax": "/users/explore",
"serverSide": true,
"columns": customColumns,
dom: "Bifpl",
"buttons": [
{
text: 'Export All to Excel',
action: function (e, dt, button, config){
$.ajax({
"url": "/users/download",
"data": dt.ajax.params(),
"success": function(res, status, xhr) {
res = JSON.parse(res).data;
var csvData = new Blob([res], {type: 'text/csv;charset=utf-8;'});
var csvURL = window.URL.createObjectURL(csvData);
var tempLink = document.createElement('a');
tempLink.href = csvURL;
tempLink.setAttribute('download', 'listOfUsers.csv');
tempLink.click();
}
});
}//end action
}//end buttons
]
});
});
The produced csv shows objects and does not show columns with new headers in them
This discussion has been closed.
Answers
Hi @gnwankwo ,
We're happy to take a look, but it would help, as per the forum rules, if you could link to a running test case showing the issue so we can offer some help. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin