获取可动手表的所有注释

Get all comments for handsontable table

本文关键字:注释 手表 获取      更新时间:2023-09-26

我正在使用handsontable从excel粘贴数据。我想向某些单元格添加更多信息,一个表示 CSS 类的字符串。我认为最好的方法是使用评论插件。在那里我看到了有关如何添加评论的信息,但我看不到如何阅读评论。当我尝试时:

hot1 = new Handsontable(container, {
    data: getData(),
    rowHeaders: true,
    colHeaders: true,
    contextMenu: true,
    comments: true,
    cell: [
      {row: 1, col: 1, comment: 'Some comment'},
      {row: 2, col: 2, comment: 'More comments'}
    ]
});      
hot1.getData();

我只看到表格数据,没有任何关于评论的信息。从文档中看,我可以访问特定的单元格以获取其注释,但是如果我可以在一个命令中获取所有注释(类似于我获取数据的方式),则没有参考。

你有什么想法吗?

我们可以使用 getCellMeta 方法获取所有评论。

var comments = [];
hot1.getCellMeta().cell.forEach(allComments);
console.log(comments)
function allComments(element){
  comments.push(element.comment);
}

这是一个工作JSfiddle。

@Shlomi L 询问是否有一个事件可以在评论更改时绑定以捕获。是的,有。它被称为:afterSetCellMeta。下面是一个示例:

afterSetCellMeta:  (row, col, source, val)  => {
       if(source == 'comment'){
                    console.log("[" + row + ", " + col + "]: " + source + " --> " + val);
                        console.log(source); //comment
                        console.log(val); //object containing value
                        this.saveUpdatedComment(val.value);
                    }
  }

这将在更改之前或之后出现。以下是文档