自定义渲染器功能无法在handsontable插件中工作

时间:2013-07-18 10:54:33

标签: jquery handsontable

我有一个处理一些onChange事件的函数,效果很好。该函数调用另一个函数来检查单元格的内容,如果有错误则应该更改单元格颜色。

function Check(x, y)
{
    var content =   $editorTableContainer.handsontable('getDataAtCell', y, x);
    var split   =   content.split(' ');    

    $.each(split, function (key, value) {
        $.get('check.php?word=' + value, function (data) {
            //blank if no error otherwise it returns an array of suggestions (only need to check if there is an error)
            if (data) {
                alert("test");
                var meta = $editorTableContainer.handsontable('getCellMeta', y, x);
                meta.renderer = ErrorRenderer;
            }
        });
    });

    return;
}

这是我简单的ErrorRenderer:

function ErrorRenderer(instance, td, row, col, prop, value, cellProperties)
{
  Handsontable.TextCell.renderer.apply(this, arguments);
  console.log(row);
  td.style.fontWeight = 'bold';
  td.style.color = 'green';
  td.style.background = '#CEC';
}

永远不会调用ErrorRenderer,即使触发警报,也不知道为什么?

谢谢

2 个答案:

答案 0 :(得分:1)

如果您使用的是handontable,为什么不使用其内置功能?

看看HT conditional formatting

此外,在0.9.5版中,添加了一个列选项validator。详情here

validator (value: Mixed, callback: Function) 

validator : RegExp Object

然后使用该事件(详情here):

afterValidate (isValid: Boolean, value: Mixed, row: Number, prop: String, source: String) 

进行单元格的格式化

此外,在您的示例中,您正在设置渲染器,但实际上是否正在渲染单元格?你需要重新渲染吗?

答案 1 :(得分:0)

我可以看到你的渲染器用于TextCell ..它只适用于文本单元格,检查错误渲染器应该适用于TextCell还是NumericCell