That’s my React code where I fix the HandsonTable
useEffect(() => {
if (!hotInstance.current) {
// Inicializar Handsontable al montar el componente
hotInstance.current = new Handsontable(container.current, {
data: datasetValues,
colHeaders: function(col) {
const headerValue = titles[col];
const buttonStyle = columnStates[col] ? ‘’ : ‘background-color: #e6adad;’;
return <button style="${buttonStyle}">${headerValue}</button>
;
},
columnHeaderHeight: 30,
rowHeaders: function(row) {
const headerValue = row + 1;
const buttonStyle = rowStates[row] ? ‘background-color: #a1d690;’ : ‘’;
return <button style="${buttonStyle}">${headerValue}</button>
;
},
rowHeaderWidth: 75,
afterOnCellMouseDown: function(event, coords) {
if (coords.row === -1 && coords.col === -1) {
setColumnStates(Array(Object.keys(datasetData[1]).length).fill(true));
setRowStates(Array(Object.keys(datasetData).length).fill(false));
} else if (coords.row === -1) {
let newColumnStates = {…columnStates};
newColumnStates[coords.col] = !newColumnStates[coords.col];
setColumnStates(newColumnStates);
} else if (coords.col === -1) {
let newRowStates = {…rowStates};
newRowStates[coords.row] = !newRowStates[coords.row];
setRowStates(newRowStates);
}
hotInstance.current.render();
},
cells: function(row, col, prop) {
const cellProperties = {};
if (!columnStates[col]) {
cellProperties.renderer = function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);
td.style.backgroundColor = '#e6adad';
};
} else if (rowStates[row]) {
cellProperties.renderer = function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);
td.style.backgroundColor = '#a1d690';
};
} else {
cellProperties.renderer = function(instance, td, row, col, prop, value, cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);
td.style.backgroundColor = '';
};
}
return cellProperties;
},
afterRender: function(isForced) {
// Selecciona el TH de la esquina si aún no tiene el botón
var cornerHeader = document.querySelector('.ht_clone_top_left_corner .htCore > thead > tr > th');
if (cornerHeader && !cornerHeader.querySelector('.corner-header-button')) {
cornerHeader.innerHTML = '';
var button = document.createElement('button');
button.textContent = 'CLEAR';
cornerHeader.appendChild(button);
}
},
licenseKey: 'non-commercial-and-evaluation',
type: 'numeric'
// Otras opciones de configuración...
});
}
// Función de limpieza al desmontar el componente
return () => {
if (hotInstance.current) {
hotInstance.current.destroy();
hotInstance.current = null;
}
};
}, );