Tengo una consulta como puedo imprimir los datos de determinada fila de la tabla, los datos son obtenidos desde una DB, es decir que al darle click en el boton de la impresora me arroje solo los datos de dicha fila. No he encontrado información por ningún sitio.
2 respuestas
Podrías usar javascript y jquery. Se trata de crear un elemento imprimible con los datos de la línea de la tabla y luego imprimirlo.
Creo que el código habla por si sólo...
$(".imprimir").click(function(e) {
/* Obtener el tr de la línea actual */
var table_tr = $(this).parent().parent();
imprimirElemento(table_tr);
})
function imprimirElemento(linea) {
var contents = '';
linea.find('td').each(function() {
contents += '<p>' + $(this).html() + '</p>';
});
var frame1 = $('<iframe />');
frame1[0].name = "frame1";
frame1.css({
"position": "absolute",
"top": "-1000000px"
});
$("body").append(frame1);
var frameDoc = frame1[0].contentWindow ? frame1[0].contentWindow : frame1[0].contentDocument.document ? frame1[0].contentDocument.document : frame1[0].contentDocument;
frameDoc.document.open();
frameDoc.document.write('<html><head><title>DIV Contents</title>');
frameDoc.document.write('</head><body>');
// Por si quieres añadir css.
//frameDoc.document.write('<link href="style.css" rel="stylesheet" type="text/css" />');
frameDoc.document.write(contents);
frameDoc.document.write('</body></html>');
frameDoc.document.close();
setTimeout(function() {
window.frames["frame1"].focus();
window.frames["frame1"].print();
frame1.remove();
}, 500);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<td>columna 1</td>
<td>columna 2</td>
<td>columna 3</td>
<td>columna 4</td>
</tr>
</thead>
<tbody>
<tr>
<td>linea 1</td>
<td>linea 2</td>
<td>linea 3</td>
<td><a href="#" class="imprimir">Imprimir</a></td>
</tr>
<tr>
<td>linea 1</td>
<td>linea 2</td>
<td>linea 3</td>
<td><a href="#" class="imprimir">Imprimir</a></td>
</tr>
</tbody>
</table>
Nota: Este código no funciona en StackOverflow, puedes copiarlo y pegarlo en jsfiddle, por ejemplo, y verás que si funciona.
-
Gracias por tu comentario, el código que me sugieres es muy útil para poder imprimir solo los datos de la tabla, lastimosamente los datos de la tabla que están en la imagen no son todos, en la BD tengo muchos mas, mi idea era escoger el id de la tabla y poder hacer una comparación con el id de la DB y así poder imprimir todos los datos en un pdf. Igual el código que sugeriste me servirá para algún proyecto en futuro.– Charlieel 8 mar. 2022 a las 21:45
Al ser un boton de impresión a la consulta se podria agregar a la consulta el parametro Id, pero necesitamos detalle de la API de impresion o su formato.
aca un ejemplo para dompdf:
// les articles
$pdf->SetFont('Arial', '', 8);
$y = 97;
// 1ere page = LIMIT 0,18 ; 2eme page = LIMIT 18,36 etc...
$sentencia5 = $pdo->prepare(
'SELECT dr.Observacion AS Reclamo, 1 AS Cantidad, dr.Monto AS Monto,
CAST(dr.Monto * 0.13 AS decimal(8,2)) AS Iva
FROM detallepago AS dp INNER JOIN pago AS p ON p.IdPago = dp.IdPago
INNER JOIN reclamo AS r ON r.IdReclamo = dp.IdReclamo
INNER JOIN detallereclamo AS dr ON dr.IdReclamo = r.IdReclamo
INNER JOIN contribuyentenombre AS cn ON cn.IdContribuyenteNombre = r.IdContribuyenteNombre
INNER JOIN banco AS b ON b.IdBanco = cn.IdBanco
INNER JOIN contribuyentetipo AS ct ON ct.IdContribuyenteTipo = cn.IdContribuyenteTipo
INNER JOIN contribuyente AS c ON c.Nrc= ct.Nrc
WHERE r.EstadoReclamo = 0 AND c.Nrc = \"$var_id_facture \" LIMIT \" $limit_inf \" , \" $limit_sup\" ' );
$sentencia5->execute();
$ListaDetalle = $sentencia5->fetchAll(PDO::FETCH_ASSOC) or die('rreur SQL : ' . $select . mysqli_connect_error());
foreach ($ListaDetalle as $detalle) {
// libelle
$pdf->SetXY(7, $y + 9);
$pdf->Cell(140, 5, $detalle['Reclamo'], 0, 0, 'L');
// qte
$pdf->SetXY(145, $y + 9);
$pdf->Cell(13, 5, strrev(wordwrap(strrev($detalle['Cantidad']), 3, ' ', true)), 0, 0, 'R');
// PU
$nombre_format_francais = number_format($detalle['Monto'], 2, ',', ' ');
$pdf->SetXY(158, $y + 9);
$pdf->Cell(18, 5, $nombre_format_francais, 0, 0, 'R');
// Taux
$nombre_format_francais = number_format($detalle['Iva'], 2, ',', ' ');
$pdf->SetXY(177, $y + 9);
$pdf->Cell(10, 5, $nombre_format_francais, 0, 0, 'R');
// total
$nombre_format_francais = number_format($detalle['Monto'] * $detalle['Cantidad'], 2, ',', ' ');
$pdf->SetXY(187, $y + 9);
$pdf->Cell(18, 5, $nombre_format_francais, 0, 0, 'R');
$pdf->Line(5, $y + 14, 205, $y + 14);
$y += 6;
}
sentencia podremos generar informe por linea.
-
@Charlie dompdf tiene muchas plantillas solo hace falta que lleves las variables a la plantilla y puedes ejecutar el script– pperezel 7 mar. 2022 a las 22:59