When attempting to retrieve the content of a table and display it in a new window for printing, I always encounter this error: The table is located within a modal and loaded dynamically into the modal via an Ajax function:
This is the Ajax function:
function view(id){
var html;
$.ajax({
url: "<?php echo site_url('admin/prescription/view_prescription_ajax') ?>/"+id,
type: 'POST',
dataType: 'html',
success:function(data){
html = data;
},
async: false
});
$('#view .modal-body').html(html);
$('#view').modal('show');
}
This is the HTML returned by the code:
$data='';
$prescription = $this->prescription_model->get_prescription_by_id($id);
$template= $this->notification_model->get_template();
$data.='<table width="100%" border="0" id="printTable" class="pt" style="padding-bottom:0px; height:100%;" >
<!-- Table content omitted for brevity -->
</tfoot>
</table>
<div class="form-group no-print">
<div class="row no-print">
<div class="col-sm-4 pull-right">
<a class="btn btn-default yes-print no-print" id="print_p" onClick="printData();" style="margin-right: 5px;"><i class="fa fa-print"></i> Print </a>
<a href="http://medicalfrequencycentre.com/drsystem/admin/prescription/pdf/'.$prescription->id.' id="pdf" class="btn btn-primary no-print" style="margin-right: 5px;"><i class="fa fa-download"></i> Generate PDF </a>
</div>
</div>
</div>
';
echo $data;
This is the JavaScript function:
function printData()
{
var divToPrint=document.getElementById("printTable");
newWin= window.open("");
newWin.document.write(divToPrint.outerHTML);
newWin.print();
newWin.close();
}