当单击“打印”按钮时,打印动态生成的数据--表有一个ID,但是表数据(td和tr)数据是动态生成的。
我能够在表中获取数据,并尝试使用window.print java-script打印所有内容。
HTML代码-
<div class="panel-body">
<table class ="table table-hover" id="TableData">
<caption>Representative Report</caption>
<thead>
<th>Date</th>
<th>Doctor's Name </th>
<th>Sampling Tablets </th>
<th>Samling Quantity</th>
<th>Chemists</th>
<th>POB</th>
<th>Location</th>
<th>Area</th>
</thead>
<tbody id="tbody"></tbody>
</table>
</div>Ajax调用以生成表中的报表-
for (element in data)
{
var productsArray = data[element].products.split(',');
var quantityArray = data[element].quantity.split(',');
var chemistArray = data[element].Retailername.split(',');
var pobArray = data[element].Pob.split(',');
// find the largest row number
var maxRows = Math.max(productsArray.length, quantityArray.length, chemistArray.length, pobArray.length);
var content = '';
var date = '<td rowspan="' + maxRows + '">' + data[element].date + '</td>';
var doctorName = '<td rowspan="' + maxRows + '">' + data[element].doctor_name + '</td>';
var locations = '<td rowspan="' + maxRows + '">' + data[element].locations + '</td>';
var area = '<td rowspan="' + maxRows + '">' + data[element].area + '</td>';
content += '<tr>' + date + doctorName;
for (var row = 0; row < maxRows; row++) {
if (row !== 0) {
content += '<tr>';
}
// the ternary operator is used to check whether there is items in the array
// if yes, insert the value between the <td></td> tag
// if not, just add an empty <td></td> to the content as a placeholder
content += '<td>' + (productsArray[row] ? productsArray[row] : '') + '</td>';
content += '<td>' + (quantityArray[row] ? quantityArray[row] : '') + '</td>';
content += '<td>' + (chemistArray[row] ? chemistArray[row] : '') + '</td>';
content += '<td>' + (pobArray[row] ? pobArray[row] : '') + '</td>';
if (row === 0) {
content += locations + area + '</tr>';
} else {
content += '</tr>';
}
}
$('#tbody').append(content);
}
},这里我把所有的td和tr都放在桌子上
当单击print Button时,我只想打印表中的一些列(Date、Doctorname和chemistname)。
我可以使用Onclick="Window.print“,但是这会打印整张表的整个页面。
任何帮助都会很感激的。
发布于 2019-01-05 08:57:35
您可以为此使用css。只需定义要隐藏的列。这是一个样本。我隐藏了第二篇也是最后一篇专栏。希望能帮上忙,新年快乐,我的朋友:)
<style>
@media print {
table td:last-child {display:none}
table th:last-child {display:none}
table td:nth-child(2) {display:none}
table th:nth-child(2) {display:none}
}
</style>
<div class="panel-body">
<table class ="table table-hover" id="TableData">
<caption>Representative Report</caption>
<thead>
<th>Doctor's Name</th>
<th>Chemists</th>
<th>POB</th>
<th>Location</th>
<th>Area</th>
</thead>
<tbody id="tbody">
<tr>
<td>Iron Man</td>
<td>ABC</td>
<td></td>
<td>China</td>
<td>Asia</td>
</tr>
<tr>
<td>Captain</td>
<td>XYZ</td>
<td></td>
<td>England</td>
<td>Europe</td>
</tr>
</tbody>
</table>
</div>
<button id="btnPrin" onclick="window.print();">Print</button> https://stackoverflow.com/questions/54050122
复制相似问题