en datatble no ceo eso, mayotmente solo se puede imprimir las filas, eso funciona bien pero queria imprimir otro elemento de manera dinamica, parece que no se puede @Component({
selector: 'app-inventario-almacen',
templateUrl: './inventario-almacen.component.html',
styleUrl: './inventario-almacen.component.css',
})
export class InventarioAlmacenComponent
implements AfterViewInit, OnInit, OnDestroy
{
dtOptions: Config = {};
dtTrigger = new Subject();
detalleSeccion: Record<string, any>[] = [];
almacenes: Almacen[] = [];
idalmacen: number | null = null;
iconClass: string = '';
isOptionsReady: boolean = false;
constructor(private almacenService: AlmacenService) {}
ngOnInit(): void {
this.listarAlmacenes();
//this.configDataTable();
}
ngAfterViewInit(): void {
setTimeout(() => {
this.dtTrigger.next(this.dtOptions);
}, 200);
/*setTimeout(() => {
this.dtTrigger.next(this.dtOptions);
}, 200);*/
/*setTimeout(() => {
this.configDataTable(); // Configurar DataTable medio segundo después
this.isOptionsReady = true;
this.initDataTable();
this.dtTrigger.next(this.dtOptions);
}, 200); // Esperar medio segundo
*/
}
configDataTable(): void {
this.dtOptions = {
language: {
url: 'https://cdn.datatables.net/plug-ins/2.0.8/i18n/es-ES.json',
paginate: {
first: '',
last: '',
next: '',
previous: '',
},
},
responsive: true,
columnDefs: [
{ responsivePriority: 1, targets: -1 },
{ responsivePriority: 2, targets: 1 },
],
layout: {
top2Start: 'buttons',
topStart: 'pageLength',
topEnd: 'search',
},
buttons: {
dom: {
button: {
tag: 'button',
className: 'btn',
},
},
buttons: [
{
extend: 'excel',
className: 'btn btn-outline-success',
text: '<i class="bi bi-file-earmark-excel"></i>',
titleAttr: 'Excel',
messageTop: this.getDetalleSeccionMessage(),
},
{
extend: 'pdf',
className: 'btn btn-outline-danger',
text: '<i class="bi bi-file-earmark-pdf"></i>',
titleAttr: 'PDF',
},
],
},
};
}
getDetalleSeccionMessage(): string {
if (!this.detalleSeccion || this.detalleSeccion.length === 0) {
console.log('detalleSeccion está vacío o no está definido');
return 'No hay datos disponibles';
} else {
console.log(this.detalleSeccion);
return this.detalleSeccion.map(det =>
'ID Almacén: ' + det['idalmacen'] + '\n' +
'Descripción: ' + det['descripcion'] + '\n' +
'Ubicación: ' + det['ubicacion'] + '\n' +
'Capacidad: ' + det['capacidad'] + '\n' +
'Cantidad de productos: ' + det['cantidad_producto'] + '\n' +
'Espacio disponible: ' + det['espacio_disponible']
).join('\n');
}
}
initDataTable(): void {
const that = this;
this.dtOptions = {
...this.dtOptions,
ajax: (dataTablesParameters: any, callback) => {
this.listar(callback);
},
columns: [
{ title: 'ID', data: 'idproducto' },
{ title: 'Producto', data: 'nombre_producto' },
{ title: 'Categoria', data: 'nombre_categoria' },
{ title: 'Stock', data: 'stock' },
],
};
//this.dtTrigger.next(this.dtOptions);
}
listarAlmacenes(): void {
this.almacenService.listarAlmacenes().subscribe({
next: async (almacenes) => {
this.almacenes = almacenes;
this.idalmacen = this.almacenes[0].idalmacen;
await this.obtenerDetallesSeccion(this.idalmacen);
this.configDataTable();
this.initDataTable();
//setTimeout(() => {
// this.dtTrigger.next(this.dtOptions);
//}, 200);
},
error: (error) => {
console.error('Error al obtener almacenes', error);
},
});
}
obtenerDetallesSeccion(idalmacen: number): Promise {
return new Promise((resolve, reject) => {
this.almacenService.obtenerDetalleSeccion(idalmacen).subscribe({
next: (detalles: Record<string, any>[]) => {
this.detalleSeccion = detalles;
console.log('Sección de detalles cargada:', this.detalleSeccion);
resolve(detalles);
},
error: (error) => {
console.error('Error al obtener detalles de la sección', error);
reject(error);
},
});
});
}
/*
listarAlmacenes(): void {
this.almacenService.listarAlmacenes().subscribe({
next: (almacenes) => {
this.almacenes = almacenes;
this.idalmacen = this.almacenes[0].idalmacen;
this.obtenerDetallesSeccion(this.idalmacen);
/*
this.configDataTable();
this.initDataTable();
this.isOptionsReady = true;
this.dtTrigger.next(this.dtOptions)
/
/ },
error: (error) => {
console.error('Error al obtener almacenes', error);
},
});
}
/*
obtenerDetallesSeccion(idalmacen: number): void {
this.almacenService.obtenerDetalleSeccion(idalmacen).subscribe({
next: (detalles: Record<string, any>[]) => {
this.detalleSeccion = detalles;
this.getDetalleSeccionMessage();
/this.configDataTable();
this.initDataTable();
this.isOptionsReady = true;
this.dtTrigger.next(this.dtOptions);/
/* },
error: (error) => {
console.error('Error al obtener detalles de la sección', error);
},
});
}
*/
alertClass(cantProd: number, capdAlmc: number): string {
const espacio = cantProd / capdAlmc;
if (espacio < 0.6) {
this.iconClass = 'bi-check-circle-fill';
return 'alert-success';
} else if (espacio < 0.85) {
this.iconClass = 'bi-exclamation-triangle-fill';
return 'alert-warning';
} else {
this.iconClass = 'bi-exclamation-octagon-fill';
return 'alert-danger';
}
}
listar(callback: any): void {
this.almacenService.listarProductosPorAlmacen(this.idalmacen!).subscribe({
next: (almacenes: Record<string, any>[]) => {
callback({
data: almacenes,
});
},
error: (error) => {
console.error('Error a listar almacén', error);
},
});
}
filtrarProductosPorAlmacen(id: number): void {
this.idalmacen = id;
this.obtenerDetallesSeccion(this.idalmacen);
this.refreshTable();
}
refreshTable() {
const tabla = ('#tablaInventarioAlmacen').DataTable();
if (tabla) {
tabla.ajax.reload();
}
}
ngOnDestroy(): void {
this.dtTrigger.unsubscribe();
}
} - lo que esta comnetado no lo analices, no puedo imprimir de manera dinamica, las filas filtradas si las toma pero el contenido de la variable que lo envio en messageTop no, solo toma el primer valor y lo mantiene , no importa si la variable adquiere neuvos datos, solo amntiene el primer valor con elque cargo, si desde este enfoque no se puede, puedes darme otro donde no interactue con el datatable, y que luego le envie un texto predefinido al excel que descarga el datatable, osea que cuando le de al boton de descargar en excel tome una cadena por ejemplo que diha hola + una variable que vaya en aumento cada segundo para ver si lo que envio es dinamico, se puede hacer eso? solo como ejemplo, quiero ver si hay itra forma de pegar un texto en la impresion de excel, que es lo mas comun en estos casos para imprimir texto fuera de las filas dedatatble - no respondas con todo ese codigo, solo lo nuevo que agregarias -