Hi, I need to remove the columns that I don't want to be there, I'm using datatables, and it gives me that problem when printing, when exporting to excel and pdf, Image for reference:
For example, it cuts me in pdf since it is a long table, I would like to show only the columns that I want, and around excel, it adds columns such as "delete," "view" and "edit ", I would like to eliminate it too, just like when printing !
(edited)
$(document).ready(function() {
$('#example').DataTable({
language: {
"lengthMenu": "Mostrar _MENU_ registros",
"zeroRecords": "No se encontraron resultados",
"info": "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros",
"infoEmpty": "Mostrando registros del 0 al 0 de un total de 0 registros",
"infoFiltered": "(filtrado de un total de _MAX_ registros)",
"sSearch": "Buscar:",
"oPaginate": {
"sFirst": "Primero",
"sLast":"Último",
"sNext":"Siguiente",
"sPrevious": "Anterior"
},
"sProcessing":"Procesando...",
},
//para usar los botones
responsive: "true",
dom: 'Bfrtilp',
buttons:[
{
extend: 'excelHtml5',
text: '<i class="fas fa-file-excel"></i> ',
titleAttr: 'Exportar a Excel',
className: 'btn btn-success'
},
{
extend: 'pdfHtml5',
text: '<i class="fas fa-file-pdf"></i> ',
titleAttr: 'Exportar a PDF',
className: 'btn btn-danger'
},
{
extend: 'print',
text: '<i class="fa fa-print"></i> ',
titleAttr: 'Imprimir',
className: 'btn btn-info'
},
],"columnDefs":[
{
"targets": [0,1,2,3],
"visible": false
}
]
});
});
(Edited 2)
Well, I found an optional solution that worked for me, I'll produce it here, in case it helps someone! ,See you soon ! :
$('#example').DataTable({
language: {
"lengthMenu": "Mostrar _MENU_ registros",
"zeroRecords": "No se encontraron resultados",
"info": "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros",
"infoEmpty": "Mostrando registros del 0 al 0 de un total de 0 registros",
"infoFiltered": "(filtrado de un total de _MAX_ registros)",
"sSearch": "Buscar:",
"oPaginate": {
"sFirst": "Primero",
"sLast":"Último",
"sNext":"Siguiente",
"sPrevious": "Anterior"
},
"sProcessing":"Procesando...",
},
//para usar los botones
responsive: "true",
dom: 'Bfrtilp',
buttons:[
{
extend: 'excelHtml5',
text: '<i class="fas fa-file-excel"></i> ',
titleAttr: 'Exportar a Excel',
className: 'btn btn-success',
exportOptions: {
columns: [ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
}
},
{
extend: 'pdfHtml5',
text: '<i class="fas fa-file-pdf"></i> ',
titleAttr: 'Exportar a PDF',
className: 'btn btn-danger',
exportOptions: {
columns: [ 0,1,2,10,11,12,13,14,15]
}
},
{
extend: 'print',
text: '<i class="fa fa-print"></i> ',
titleAttr: 'Imprimir',
className: 'btn btn-info',
exportOptions: {
columns: [ 0,1,2,10,11,12,13,14,15]
}
},
]
});
"As you can see, if you have export buttons like I do, all you have to do is add exportOptions , and in the columns section, put the positions of the columns you want displayed, that's all!"
Hello, to hide columns of a datatable that you do not want to be shown, you must pass the visible property false.
For example I have the following datatable
I'll explain how it works... when
datatable
I pass several columns (18 in total), but below in "columnDefs", I specify that I want some of them not to be seen (they are there, but hidden)Column numbering starts at 0 (0 would be the first column), 1,2,3 from left to right. In this example the "targets" columns are hidden:
[3= estado, 7=profDestino,8=diagnistico,9=sintomas,10=solicitud,12=ambito,13=patologia GES,14=tipoEstados,15=idPaciente,16=Respuesta]
I leave you the datatable documentation below: Datatable columns