Skip to content

Instantly share code, notes, and snippets.

@vishalbasnet23
Created October 20, 2014 10:46
Show Gist options
  • Select an option

  • Save vishalbasnet23/c7fb424528b69da86aa0 to your computer and use it in GitHub Desktop.

Select an option

Save vishalbasnet23/c7fb424528b69da86aa0 to your computer and use it in GitHub Desktop.
html-table-row-to-csv-javaScript
$('.export-csv').click(function(){
var nearestRow = $(this).closest('tr');
// console.log(nearestRow);
var allValue = [];
nearestRow.each(function() {
var eventDate = $(this).find('.event-date').text();
allValue.push(eventDate.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
var eventName = $(this).find('.event-name').text();
// console.log(eventName.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
allValue.push(eventName.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
var eventLocation = $(this).find('.event-location').text();
allValue.push(eventLocation.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
var eventInstructor = $(this).find('.event-instructor').text();
allValue.push(eventInstructor.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
var approvedRegistration = $(this).find('.apvd-registration').text();
allValue.push(approvedRegistration.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
var pendingRegistration = $(this).find('.pend-registration').text();
allValue.push(pendingRegistration.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
var notApprovedRegistration = $(this).find('.noapvd-registration').text();
allValue.push(notApprovedRegistration.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
var totalAmount = $(this).find('.total-price').text();
allValue.push(totalAmount.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
var totalPaid = $(this).find('.total-paid').text();
allValue.push(totalPaid.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
var totalDue = $(this).find('.due-amount').text();
allValue.push(totalDue.trim().replace(/["~!@#$%^&*\(\)_+=`{}\[\]\|\\:;'<>,.\/?"\- \t\r\n]+/g, '-'));
});
var data = [["Event Date", "Event Name", "Location","Instructor","Approved Registrations","Pending","Not Approved Registrations","Total Price","Total Paid","Total Due"], allValue];
var csvContent = "data:text/csv;charset=utf-8,";
data.forEach(function(infoArray, index){
dataString = infoArray.join(",");
csvContent += index < infoArray.length ? dataString+ "\n" : dataString;
});
var encodedUri = encodeURI(csvContent);
$(this).attr("href", encodedUri);
$(this).attr("download", "my_data.csv");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment