Handsontable:如何更改渲染函数中的单元格值

Handsontable : how to change cell value in render function

我有类似的代码:

<script>
$(document).ready(function () {
    var data = @Html.Raw(ViewBag.Data);

function sumPreviousValues(instance, td, row, col, prop, value, cellProperties) {
  Handsontable.renderers.TextRenderer.apply(this, arguments);
    td.val = 'SUM SEVERAL PREVIOUS CELLS';
}

     container = document.getElementById('example1'),
      settings1 = {
        data: data,
        colHeaders: @Html.Raw(ViewBag.TableHeader),
        cells: function (row, col, prop) {
            var cellProperties = {};

            if (col == 16) {
                cellProperties.renderer = sumPreviousValues;
            }
    return cellProperties;
        }
      },

    hot = new Handsontable(container,settings1);
    hot.render();

关键是修改函数sumPreviousValues()

但我不知道如何访问 td 值?以及是否可以访问其他单元格的值,如 td[index]。 任何想法?我没有在文档中找到选项。

谢谢

为了限定这个问题,你能检查传递给 sumPreviousValues()td 参数吗?这将有助于理解这是什么,我假设它是一个 HTML DOM 节点。如果是这样,您可以使用 td.innerHTML

调整它的值

可在此处找到更多相关信息: http://www.w3schools.com/jsref/prop_html_innerhtml.asp

我想我明白你想做什么,这里有一个简单的解决方案:

从自定义渲染器内部,您可以调用:

instance.getDataAtCell(row, col);

这将为您提供 row,col 单元格的数据(值)。希望有所帮助:)

此外,要访问当前 td 处的值,您只需使用 value 变量 :P 查看函数定义。您实际上是在传递它并且可供您使用。