Right align PDF export column when using AJAX workaround
Right align PDF export column when using AJAX workaround
InnovaMatt
Posts: 13Questions: 7Answers: 0
I'm successfully using this workaround for exporting all AJAX records from a paginated table. I'm unclear though how to access the recordsTotal value returned from that AJAX request to use as my RowCount in the loops people are using in this thread within the PDF export's customize function.
Here's a wall of my fairly Spaghetti-ish code:
function newexportaction(e, dt, button, config) {
let self = this;
let oldStart = dt.settings()[0]._iDisplayStart;
dt.one('preXhr', function (e, s, data) {
// Just this once, load all data from the server...
data.start = 0;
data.length = 2147483647;
data.qty = $('#cBOMQty').val();
dt.one('preDraw', function (e, settings) {
// Call the original action function
if (button[0].className.indexOf('buttons-copy') >= 0) {
$.fn.dataTable.ext.buttons.copyHtml5.action.call(self, e, dt, button, config);
} else if (button[0].className.indexOf('buttons-excel') >= 0) {
$.fn.dataTable.ext.buttons.excelHtml5.available(dt, config) ?
$.fn.dataTable.ext.buttons.excelHtml5.action.call(self, e, dt, button, config) :
$.fn.dataTable.ext.buttons.excelFlash.action.call(self, e, dt, button, config);
} else if (button[0].className.indexOf('buttons-csv') >= 0) {
$.fn.dataTable.ext.buttons.csvHtml5.available(dt, config) ?
$.fn.dataTable.ext.buttons.csvHtml5.action.call(self, e, dt, button, config) :
$.fn.dataTable.ext.buttons.csvFlash.action.call(self, e, dt, button, config);
} else if (button[0].className.indexOf('buttons-pdf') >= 0) {
$.fn.dataTable.ext.buttons.pdfHtml5.available(dt, config) ?
$.fn.dataTable.ext.buttons.pdfHtml5.action.call(self, e, dt, button, config) :
$.fn.dataTable.ext.buttons.pdfFlash.action.call(self, e, dt, button, config);
} else if (button[0].className.indexOf('buttons-print') >= 0) {
$.fn.dataTable.ext.buttons.print.action(e, dt, button, config);
}
dt.one('preXhr', function (e, s, data) {
// DataTables thinks the first item displayed is index 0, but we're not drawing that.
// Set the property to what it was before exporting.
settings._iDisplayStart = oldStart;
data.start = oldStart;
});
// Reload the grid with the original page. Otherwise, API functions like table.cell(this) don't work properly.
setTimeout(dt.ajax.reload, 0);
// Prevent rendering of the full data to the DOM
return false;
});
});
// Requery the server with the new one-time export settings
dt.ajax.reload();
}
//For Export Buttons available inside jquery-datatable "server side processing" - End
function unitsdesc(){
let qty = $('#cBOMQty').val();
let unit = "unit";
if(qty > 1){
unit = unit + "s";
}
return `Quantities to produce ${qty} ${unit}`;
}
let dT_CompBOM = $('#dT_CompBOM').DataTable({
"pageLength": 5,
"autoWidth": true,
"lengthChange": true,
"searching": false,
"language": {
"emptyTable": `No components have been added for assembly ${compNo}`
},
dom: '<".dT_cBOMTop"<"grid-cBOM"<"cBOMhdg"><"cBOMQty">>B>frtip',
buttons:
[{
extend: 'collection',
className: "btn btn-primary",
text: 'Export',
buttons: [
{extend: "excel",
titleAttr: 'Excel',
background: false,
className: "btn",
text: '<i class="fas fa-file-excel" style="color: green;"></i> Excel',
attr: { id: "btn_cBOMex" },
orientation: "landscape",
title: `${compNo} - ${title}`,
"messageTop": unitsdesc,
"action": newexportaction
},
{extend: "pdf",
titleAttr: 'PDF',
background: false,
className: "btn",
text: '<i class="fas fa-file-pdf" style="color: red;"></i> PDF',
attr: { id: "btn_cBOMpdf" },
orientation: "landscape",
title: `${compNo} - ${title}`,
"messageTop": unitsdesc,
"customize": function (doc) {
// Where the loop needs to go
}
},
"action": newexportaction
},
{extend: "print",
titleAttr: 'Print',
background: false,
className: "btn",
text: '<i class="fas fa-print" style="color: black;"></i> Print',
attr: { id: "btn_cBOMprnt" },
orientation: "landscape",
title: `${compNo} - ${title}`,
"messageTop": unitsdesc,
"action": newexportaction
}
]
}],
"serverSide": true,
// "processing": true,
"columns": [
{"name": "CompNo", "searchable": false},
{"name": "Desc", "searchable": false},
{"name": "Q", "className": "ralgn", "searchable": false},
],
"ajax": {
"url": "/ajax/compbom.php",
"type": "POST",
"dataSrc": function (json) {
// console.dir("Data: " + json.data); // Log array to console
return json.data
},
"data": function(d) {
d.id = compID;
d.qty = 1;
},
error: function(response, req, err){
console.log(`CompBOM Error: ${err} | Response: ${response.data} | CompID: ${compID}`);
}
},
"drawCallback": function (settings) {
dT_PagntnCntrl($(this), this, settings)
}
}); // DataTable
$('.dataTables_length select').addClass('form-control');
$("div.cBOMhdg").html('<h3 class="hdgbtn">Bill Of Materials</h3>');
$("div.cBOMQty").html('<label for="cBOMQty">For Quantity</label><input class="form-control ralgn" id="cBOMQty" name="cBOMQty" type="number" value="1"/>');
/markdown
This discussion has been closed.
Answers
We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. 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