如何为Handontable中的所有空单元格指定默认值0

How to give default value of 0 to all the empty cells in Handsontable?

本文关键字:单元格 默认值 Handontable      更新时间:2023-09-26

我有一个简单的手控表,它从数据库中获取数据,但不是所有的单元格都有数据,有些单元格只有空值。是否可以放置一个默认值为0.00的数字格式器,而不是手动显示表中的空单元格??

这是解决方案。如果不工作,请检查并告诉我。我已经在jsfiddle上测试过了>http://jsfiddle.net/yL8t1psf/1/

$(document).ready(function () {
  var container = document.getElementById('basic_example');
  var data = [
      ['', 'Kia', 'Nissan', 'Toyota', 'Honda'],
      ['2014', 10, 11, 12, 13],
      ['2015', 20, null, , 13],
      ['2016', null, 15,'', null]
  ];
  var hot = new Handsontable(container, {
    data: data,
    height: 396,
    colHeaders: true,
    rowHeaders: true,
    stretchH: 'all',
    columnSorting: true,
    contextMenu: true,
    cells: function (row, col, prop,value) {
      var cellProperties = {};          
          cellProperties.renderer = firstRowRenderer;
      return cellProperties;
    }
  });
});

  function firstRowRenderer(instance, td, row, col, prop, value, cellProperties) {
    Handsontable.renderers.TextRenderer.apply(this, arguments);
    if(!value || value === '' || value == null ) {                        
        td.innerHTML = "0.00";
    }    
  }

将其添加到条件格式中。

http://docs.handsontable.com/0.20.0/demo-conditional-formatting.html

if (!value || value === '') {
  td.innerHTML = "0.00";
}