I have used javascript to print HTML table in my application.
Below function executes on click of 'Print' button. I have 'printElement' function for printing.
$("#btnlwPrint").off('click').on('click',function() {
var cboVendorName ;
cboVendorName= $('#cboVendorName').combogrid('textbox').val();
var tbodylength=$('#ssgGrid tbody tr').length;
if (cboVendorName =="" || tbodylength <= 1){
$('#warPrintEmptyRow').modal('toggle');
} else {
$('#lWVendor').text(cboVendorName);
printElement(document.getElementById("printHeader"));
printElement(document.getElementById("ssgGrid"),true);
window.print();
}
});
//Below function prints the grid
function printElement(elem, append, delimiter) {
var domClone = elem.cloneNode(true);
var $printSection = document.getElementById("printSection");
if (!$printSection) {
var $printSection = document.createElement("div");
$printSection.id = "printSection";
document.body.appendChild($printSection);
}
if (append !== true) {
$printSection.innerHTML = "";
}
else if (append === true) {
if (typeof(delimiter) === "string") {
$printSection.innerHTML += delimiter;
}
else if (typeof(delimiter) === "object") {
$printSection.appendChlid(delimiter);
}
}
$printSection.appendChild(domClone);
}