【问题标题】:Custom renderer function not working in handsontable plugin自定义渲染器功能在 Handsontable 插件中不起作用
【发布时间】:2013-07-17 06:54:18
【问题描述】:

我有一个函数可以处理一些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 永远不会被调用,即使触发了警报,知道为什么吗?

谢谢

【问题讨论】:

  • 离题:fwiw,有一个有用的 JS 代码约定只为构造函数保留大写的函数名。 javascript.crockford.com/code.html#names
  • 谢谢。我没有意识到这一点
  • 在下面的答案中查看我关于渲染的评论

标签: jquery handsontable


【解决方案1】:

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

看看 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) 

对单元格进行格式化

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

【讨论】:

    【解决方案2】:

    我可以看到您的渲染器适用于 TextCell.. 它仅适用于文本单元格,检查错误渲染器是否适用于 TextCell 或 NumericCell

    【讨论】:

      猜你喜欢
      • 2016-12-03
      • 2016-02-03
      • 2015-03-29
      • 2017-11-06
      • 1970-01-01
      • 2019-02-04
      • 1970-01-01
      • 2016-04-26
      • 1970-01-01
      相关资源
      最近更新 更多