Angular button with event binding

Tags: #<Tag:0x00007efc6abb9208>

Hello, I’m using Angular 10.1.5 with Handsontable 8.3.2.

I’m trying to make a very basic app where a button is dynamically rendered in a table cell. The button passes the value of the table cell to a function called consoleValue() in my component, which logs the value in the console. The button and function are bound using ‘(click)’.

My settings with the renderer, the data set, and my bound function in the component look like this:

Here I’ve hidden it in text form:

Summary

hotSettings: Handsontable.GridSettings = {
columns: [
{
renderer: function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);
td.innerHTML = <button class="btn btn-info mt-2 mb-2" (click)="consoleValue(${value})">TEST</button>;
},
editor: false
}
]
};

dataset = [
[1],
[2],
[3]
];

consoleValue(val: any) {
console.log('consoleValue: ', val)
}

My html file looks like this:

Screenshot%20from%202021-05-06%2020-45-51

Summary

<hot-table
hotId=“hot”
class=“hot”
[colHeaders]=“true”
[rowHeaders]=“true”
[data]=“dataset”
[settings]=“hotSettings”
licenseKey=“non-commercial-and-evaluation”>

This code/html produces a styled button in each table cell:

Screenshot%20from%202021-05-06%2020-40-38

But when I click any of these buttons, I don’t see any console messages. Is there anything I should be doing differently?

Thanks

Hi @bluefrogjam

I recommend using afterOnCellMouseDown hook to run the function instead (click) within the cell (it works completely fine outside the table)
Demo https://stackblitz.com/edit/angular-handsontable-custom-renderer-jxpfdn?file=app/app.component.ts

Related topic on Github https://github.com/handsontable/handsontable/issues/5668

Thanks Aleksandra! The Stackblitz example needed one minor change from me.

The Stackblitz:

Screenshot%20from%202021-05-07%2011-23-01

Mine:

Screenshot%20from%202021-05-07%2011-11-23

I had to define event.target as an HTMLElement or else I got the errors “Property ‘realTarget’ does not exist on type ‘MouseEvent’” and “Property ‘className’ does not exist on type ‘EventTarget’”. Otherwise it seems to be what I’m looking for.