Comments not properly rerendered when added inside validator function

Tags: #<Tag:0x00007efc6560fb40>

I am using comments as a explanation to validation failures - if cell is invalid, comment shows the reason why. Also, it is important, that validation is done externally, so HOT validator only marks the cells status and do no real validation.

I am setting and clearing out cell comments in validation function. However, comments are not properly cleared out

http://jsfiddle.net/j6uo7mdq/17/

I have to manually rerender table in order for comments to disapear which is a bug imo.

var globalValid=true;

function validatorFn(value,pushResult){
  let isValid=globalValid;
  if(!isValid){
  	this.comments=true;
    this.comment={
        readOnly: true,
        value: "required bro!"
    }
  }else{
  	this.comments=false;
    this.comment=null;
  }
	pushResult(isValid);
}


function revalidate(rerender){
	globalValid=!globalValid;
  snapshot=hot.getData();
	hot.validateCells(valid=>{
  	if(rerender) hot.render();
  });

}

var data = function () {
  return Handsontable.helper.createSpreadsheetData(5, 5);
};

var container = document.getElementById('example');

var hot = new Handsontable(container, {
  data: data(),
  minSpareCols: 1,
  minSpareRows: 1,
  rowHeaders: true,
  colHeaders: true,
  contextMenu: true,
  comments: true,
  validator:validatorFn,
  cells:(row, column)=>{
  		if(row==0){
      	return {
        	disableVisualSelection: true,
        }
      }
      return {};
  }
});

Hi @sebastian.choina

according to the documentation we recommend using the commentsPlugin.removeCommentAtCell(row, column) method. Did you already try it?

I will try it, but it is still a bug IMO since it works fine without for adding comment, so should work the other way around.

Should I use “batch” for this? Will every removeCommentAtCell trigger rerender?

Yes, I recommend using batch() as both methods to add and remove comments are triggering cell renderer. Here we can test it https://jsfiddle.net/Lx48b2ps/ with the batch we have 3 calls to the cell renderer, without the batch, there are 7 calls.

When it comes to the code snippet you have sent it is hard to tell if that is correct as of the following use case what not covered by tests. We prepared a dedicated method for adding and removing comments (alternative you can use setCellMeta()) and there we have a full list of use cases and tests to confirm the validity of those actions. So a different approach can be correct. But it might be risky.