I am new to handsontable. So basically what I want to do is that, when the user is about to submit the data in the table, I want to check if any of the rows contain cells which have invalid data(e.g. the numeric data in some rows have text and are showing in red,or say date cells have invalid data etc.). Now, assume that during submit, there are some rows with invalid data, and the user now clicks on the submit button. I want the script to loop through all the rows to see if some cells have invalid data. Currently, I achieve this by maintaining an array which contains row indexes for such invalid rows, and I check if this array is empty during submit( not empty means there are some invalid rows out there). So, during the aftervalidate event, if isValid passed is false, I push this row index into that array(and later remove it when cell data is correctly entered). But I do not want this extra logic which makes code complex, and error prone. I want to know if there are some cell properties which I can refer when I am looping through rows, and can tell me if that cell has invalid data. May be I will have to set data for that invalid cell during aftervalidate ?. No sure.
Thanks for your feedback. I tried to loop over the data to check cell properties of each cell. I have two cells in red. Please see the submitData() function below. But in console.log() I see that ‘htInvalid’ for all cells. Is this how we can check for ‘htInvalid’ className for each cell?.
function submitData() {
var ht = $container.handsontable(‘getInstance’);
var tableData=ht.getData();
var tot_rows=tableData.length;
console.log(“tot_rows=>”+tot_rows);
for(var i=0; i <tot_rows; ++i)
{
var rowData = tableData[i];
var totColumns=rowData.length;
for(var j=0; j < totColumns;++j){
var cellProperties = ht.getCellMeta(i, j);
if(cellProperties.invalidCellClassName=="htInvalid"){
console.log("----------");
console.log(ht.getDataAtCell(i,j));
console.log(cellProperties.invalidCellClassName);
console.log("----------");
}
}
}
}
The easiest way to check is a cell is valid or not will be like this: http://jsfiddle.net/cL82rpkc/ (I added a hot.validateCells() method to show results). If you would want to show only invalid cells then add an IF statement http://jsfiddle.net/7w5Lhdhr/
but… if you would like to check if there’s a ‘htInvalid’ class instead you can add it to the afterChange hook to fire after any cell is changed (example: http://jsfiddle.net/c9kLtu9d/).
If you ask me the first method is better but I do not know where and what you would like to get as a further part of calculations.
EDIT: and I forgot to mention these all result can be loaded in an array if you would like to get all data about invalid cells when a user makes a submit.