添加 HTML DOM addEventListener 到数据表中的自定义按钮

Adding HTML DOM addEventListener to custom button in datatables

我已将自定义按钮添加到数据表,我正在尝试将 addEventListener 添加到该按钮。这是我的代码。

constructor(public router: Router) { }

    @ViewChild(DataTableDirective)
    datatableElement: DataTableDirective;
    message = '';
    title = 'angulardatatables';

    @ViewChild('dataTable') table: { nativeElement: any; };
    dataTable: any;
    dtOptions: DataTables.Settings = {};
    // someClickhandler(info: any): void {
    //     this.message = info.number + '-' + info.assignedto + '-' + info.company;
    //     console.log(this.message);
    // }
    ngOnInit(): void {
        this.dtOptions = {
            pagingType: 'full_numbers',
            pageLength: 15,
            processing: true,
            responsive: true,
            autoWidth: false,
            dom: 'Bfrtip',
            'ajax': {
                url: 'http://localhost:8080/incident-updated',
                type: 'GET',
                dataSrc: 'result',
            },
            columns: [
                {
                    title: 'Incident',
                    data: 'number'
                },
                {
                    title: 'Product',
                    data: 'u_product'
                },
                {
                    title: 'Status',
                    data: 'state'
                },
                {
                    title: 'Created By',
                    data: 'sys_created_by'
                },
                {
                    title: 'Group',
                    data: 'assignment_group'
                },
                {
                    title: 'Category',
                    data: 'u_category'
                },
                {
                    title: 'Updated on',
                    data: 'sys_updated_on'
                },
                {
                    title: 'Action',
                    data: null,
                    // render: function(data, type, full) {
                    //     return '<a class="btn btn-primary btn-sm" style="color: #fff;" id="view" (click)="view($event)">View</a>';
                    // }
                    defaultContent: '<button class="btn btn-sm btn-primary" onClick="viewer();" (click)="viewer(event)"> View </button>'
                }
            ]
        };
        this.dataTable = $(this.table.nativeElement);
        console.log('table is ==>', this.dataTable);
        $('#view tbody').on('click', 'button', function () {
            const data = this.dataTable.row($(this).parents('tr').data);
            alert('Data is ==>' + data);
        });
    }

    ngAfterViewInit(): void {
        // Called after ngAfterContentInit when the component's view has been initialized. Applies to components only.
        // Add 'implements AfterViewInit' to the class.
        // this.viewer.addEventListener('click', function() {
        //     event.stopPropagation();
        //     // this.router.navigate(['/event-viewer']);
        //     alert('shdfiskludhzj');
        // });
    }
    viewer() {
        alert('sdjfhsklzdjh');
    }
    // viewer(event: any) {
    //     event.stopPropagation();
    //     this.router.navigate(['/event-viewer']);
    // }
    // buttonInRowClick(event: any): void {
    //     event.stopPropagation();
    //     console.log('Button in the row clicked.');
    //     this.router.navigate(['/event-viewer']);
    // }
    // wholeRowClick(): void {
    //     console.log('Whole row clicked.');
    // }  

它returns错误:

issues:1 Uncaught ReferenceError: viewer is not defined
    at HTMLButtonElement.onclick (issues:1)
onclick   

我尝试了其他几种方法,none 中的方法有效。如何解决这个问题?

将此函数添加到您的组件代码中。

@HostListener('click')
 viewer() {
   // Perform your opration here
 }

@HostListener('document:click')
  viewer() {
   // Perform your operation
  }