Hello,
I have a problem regarding the styling of my Handsontable.
I am using the setCellMeta
method to apply some styles (changing the background color of the cells, to be more specific) if some conditions apply.
Some more information, maybe it’s relevant: I have an endpoint that loads the data that I wish to display inside the table, and I call it inside the useEffect hook of React. After that, I call this styleTable method, that applies styles to certain cells. So my component looks (roughly) something like this:
const [tableData, setTableData] = useState([])
useEffect(() => {
loadDataFromServer()
}, [])
useEffect(() => {
tableData.forEach(item => {
styleTable(item);
});
hot.render();
}, [tableData])
const loadDataFromServer = async () => {
const payload = {
//…
};
const response = await getData(payload);
if (response && response.data) {
setTableData(response.data);
}
};
const styleTable = item => {
if (item.something === true) {
hot.setCellMeta(rowIndex, i, "className", “my-class”);
hot.setCellMeta(rowIndex, i, "readOnly", true);
}
};
Now, inside the afterChange
hook I call a method that sends the new data to the server:
const hotSettings = {
// …
afterChange: (changes, source) => handleSave(changes)
}
const handleSave = async changes => {
const payload = {
//…
};
const response = await sendToServer(payload);
//…
};
The problem I am facing is that whenever there are changes, my style disappears, so “my-class
” is removed and the table is again all white with no readOnly
cells.
Can you please tell me if I am missing something here? Should I do this another way?
Thank you!