@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 = '';
constructor(private almacenService: AlmacenService) {}
ngOnInit(): void {
this.configDataTable();
this.listarAlmacenes();
}
ngAfterViewInit(): void {
this.initDataTable();
setTimeout(() => {
this.dtTrigger.next(this.dtOptions);
}, 200);
}
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 {
const messages: string[] = [];
const finalMessage = messages.join('\n');
setTimeout(() => {
if (!this.detalleSeccion || this.detalleSeccion.length === 0) {
console.log('detalleSeccion está vacío o no está definido');
return 'No hay datos disponibles';
} else {
this.detalleSeccion.forEach(det => {
messages.push(ID Almacén: ${det['idalmacen']});
});
// Concatenar todos los mensajes del array en una sola cadena
console.log('Mensaje generado:', finalMessage);
return finalMessage;
}
}, 4000); // 2000 milisegundos = 2 segundos
console.log('detalleSeccion:', this.detalleSeccion);
return finalMessage;
}
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' },
],
};
} listarAlmacenes(): void {
this.almacenService.listarAlmacenes().subscribe({
next: (almacenes) => {
this.almacenes = almacenes;
this.idalmacen = this.almacenes[0].idalmacen;
this.obtenerDetallesSeccion(this.idalmacen);
},
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()
},
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();
}
}
- eso tengo y el problema como te dije es cuando imprimo, me sale vacio porque al parecer detecta que no hay datos en la variable pero la variable si tiene datos, como soluciono esto? solo responde con el codigo necesario,