How to print every record of loop in a separate page, While getting data through ajax and PHP in array. i.e
I want row1
data in every page, And row2
data every record in a separate page.
Trying the following script, not giving the desired result.
var response = {
row1: [{group: 'Group A'}],
row2: [
{team: 'Team A', player: 'Jema', result: 43},
{team: 'Team B', player: 'Deno', result: 34},
{team: 'Team B', player: 'Niob', result: 56},
{team: 'Team B', player: 'Skion', result: 49},
],
};
$("#group").html(response.row1.group);
var cats = {};
response.row2.forEach(function(element) {
cats[element.team] = cats[element.team] || [];
cats[element.team].push(element);
});
Object.keys(cats).forEach(function(team) {
let html = '';
html = '<tr>';
html += '<td>' + team + '</td>';
html += '</tr>';
$('table').append(html);
// Loop through the results for this category
cats[team].forEach(function(element) {
var names = element.player;
var result = element.result;
html = '<tr>';
html += '<td>' + names + '</td>';
html += '<td><input type="text" value="' + result + '"></td>';
html += '</tr>';
$('table').append(html);
});
// Append the textarea at the end of the results
html = '<tr>';
html += '<td><textarea placeholder="textarea..."></textarea></td>';
html += '</tr>';
$('table').append(html);
});
var PrintThis = document.getElementById('print');
var PrintStyle = '<style type="text/css">' +
'@media print{' +
'.Break { page-break-after: always }' +
'}' +
'</style>';
PrintStyle += PrintThis.innerHTML;
myWin = window.open("");
myWin.document.write(PrintStyle);
myWin.print();
myWin.close();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<div id="print">
<h1 id="group">
</h1>
<table class="table bordered">
<thead>
<th>Name</th>
<th>Result</th>
</thead>
<tbody>
</tbody>
</table>
</div>