import { AfterViewInit, Component, ElementRef, OnDestroy, OnInit, Renderer2,…
- User
import { AfterViewInit, Component, ElementRef, OnDestroy, OnInit, Renderer2, TemplateRef, ViewChild,
} from ‘@angular/core’; import { Config } from ‘datatables.net’; import { Producto } from ‘…/…/models/producto.model’; import { Subject } from ‘rxjs’; import { ProductoService } from ‘…/services/producto.service’; import Swal from ‘sweetalert2’; import { Router } from ‘@angular/router’; import { ADTSettings } from ‘angular-datatables/src/models/settings’;
@Component({ selector: ‘app-producto-listar’, templateUrl: ‘./producto-listar.component.html’, styleUrl: ‘./producto-listar.component.css’, }) export class ProductoListarComponent implements AfterViewInit, OnInit, OnDestroy { dtOptions: Config = {}; dtTrigger = new Subject();
@ViewChild(‘demoNg’, { static: true }) demoNg!: TemplateRef;
constructor( private productoService: ProductoService, private router: Router, private renderer: Renderer2 ) {}
ngOnInit(): void {
}
ngAfterViewInit(): void { const that = this; this.dtOptions = { ajax: (dataTablesParameters: any, callback) => { this.listar(callback) },
columns: [ { title: ‘ID’, data: ‘idproducto’ }, { title: ‘Fecha’, data: ‘fecha’ }, { title: ‘Nombre’, data: ‘nombre’ }, { title: ‘Precio Compra’, data: ‘preciocompra’ }, { title: ‘Precio Venta’, data: ‘precioventa’ }, { title: ‘Imagen’, data: ‘imagen’ }, { title: ‘Activo’, data: ‘activo’ }, { title: ‘Categoría’, data: ‘categoria.nombre’ },
{ title: ‘Acciones’, data: null, render: function (data, type, row) { const buttons = that.botones(row.idproducto, row.nombre); return buttons.join(’ ‘) /return <button class="btn btn-warning btnActualizar me-1" data-id="{row.idproducto}"></button> <button class="btn btn-danger btnEliminar" data-id="{row.idproducto}" data-nombre="{row.nombre}"></button>;/ } }
], };
setTimeout(() => { this.dtTrigger.next(this.dtOptions); }, 200);
this.registerEvents(); }
botones(id: number, nombre: string): string[] { const btnActualizar = <button class="btn btn-warning btnActualizar me-1" data-id="{id}"></button>; const btnEliminar = <button class="btn btn-danger btnEliminar" data-id="{id}" data-nombre="${nombre}"></button>; return [btnActualizar, btnEliminar]; // Return an array of button strings }
registerEvents(): void { this.dtTrigger.subscribe(() => {
setTimeout(() => {
const botonesActualizar = document.querySelectorAll(’.btnActualizar’); const botonesEliminar = document.querySelectorAll(‘.btnEliminar’);
botonesActualizar.forEach(boton => { this.renderer.listen(boton, ‘click’, (event: Event) => { const id = (event.target as HTMLElement).getAttribute(‘data-id’); if (id) { this.btnActualizar(parseInt(id, 10)); } }); });
- Assistant