NonInventPurchasingSystem/CPRNIMS.WebApps/wwwroot/JsFunctions/Utilities/ExportCSVExcel.js

91 lines
2.8 KiB
JavaScript

function exportAllData(format, api, loader, reportTitle, column) {
const params = new URLSearchParams({
searchPRNo: ($('#srchPRNo').val() || '').trim(),
searchItemName: ($('#srchItem').val() || '').trim(),
searchDept: ($('#srchDept').val() || '').trim(),
searchStatusName: ($('#srchStatus').val() || '').trim(),
pageNumber: 1,
pageSize: 999999
});
loader.show();
$.ajax({
url: `${api}?${params}`,
type: 'GET',
success: function (json) {
loader.hide();
if (!json.data || json.data.length === 0) {
toastr.warning('No data to export.');
return;
}
triggerClientExport(json.data, format, reportTitle, column);
},
error: function () {
loader.hide();
toastr.error('Failed to fetch data for export.');
}
});
}
function triggerClientExport(data, format, reportTitle, column) {
const tempTableId = 'tempExportTable';
// Filter out action/button columns (data: null) and strip render functions
const exportColumns = column
.filter(c => c.data !== null && c.data !== undefined)
.map(c => ({
data: c.data,
title: c.title || c.data,
// Keep render only for non-display logic (dates etc),
// but strip HTML from output
render: c.render
? function (data, type, row) {
if (type === 'export' || type === 'display') {
const result = c.render(data, 'display', row);
// Strip HTML tags for export
return $('<div>').html(result).text();
}
return data;
}
: undefined
}));
$('body').append(`
<div id="tempExportWrapper" style="position:fixed;left:-9999px;top:-9999px;">
<table id="${tempTableId}"></table>
</div>
`);
const tempTable = $(`#${tempTableId}`).DataTable({
data: data,
columns: exportColumns,
dom: 'Brt',
buttons: [
{
extend: format,
title: reportTitle,
exportOptions: {
columns: ':visible',
format: {
body: function (data) {
// Strip any remaining HTML tags
return $('<div>').html(data).text();
}
}
}
}
],
paging: false,
searching: false,
info: false
});
setTimeout(function () {
tempTable.button(0).trigger();
}, 500);
setTimeout(function () {
tempTable.destroy();
$('#tempExportWrapper').remove();
}, 2000);
}