Handsontable example

by mathieuLegault

HTML

<div id="example1" class="hot handsontable htColumnHeaders"></div>

CSS

</style>< !-- Ugly Hack due to jsFiddle issue --><script src="https://docs.handsontable.com/pro/1.18.0/bower_components/handsontable-pro/dist/handsontable.full.min.js"></script><link type="text/css" rel="stylesheet" href="https://docs.handsontable.com/pro/1.18.0/bower_components/handsontable-pro/dist/handsontable.full.min.css">

JavaScript

document.addEventListener("DOMContentLoaded", function() {

  function getCarData() {
    return [{
        car: "Mercedes A 160",
        year: 2017,
        available: 1,
        comesInBlack: 'yes'
      },
      {
        car: "Citroen C4 Coupe",
        year: 2018,
        available: 0,
        comesInBlack: 'yes'
      },
      {
        car: "Audi A4 Avant",
        year: 2019,
        available: 1,
        comesInBlack: 'no'
      },
      {
        car: "Opel Astra",
        year: 2020,
        available: 0,
        comesInBlack: 'yes'
      },
      {
        car: "BMW 320i Coupe",
        year: 2021,
        available: 0,
        comesInBlack: 'no'
      }
    ];
  }

  var example1 = document.getElementById('example1'),
    hot1;


  function chck(instance, td, row, col, prop, value, cellProperties) {
    Handsontable.renderers.CheckboxRenderer.apply(this, arguments);
    td.style.fontWeight = 'bold';
    td.style.color = 'green';
    td.style.background = '#CEC';
  }

  Handsontable.cellTypes.registerCellType('PXCheckbox', {
    editor: Handsontable.editors.CheckboxEditor,
    renderer: Handsontable.renderers.CheckboxRenderer, // Or chck do the same. I cannot use Space to trigger a cell switch.
  });

  hot1 = new Handsontable(example1, {
    data: getCarData(),
    colHeaders: ['Car model', 'Year of manufacture', 'Available'],
    columnTypes: {
      checkbox: ['PXCheckbox'],
    },
    columns: [{
        data: 'car'
      },
      {
        data: 'year',
        type: 'numeric'
      },
      {
        data: 'available',
        type: 'PXCheckbox',
        checkedTemplate: 1,
        uncheckedTemplate: 0
      }
    ],
    fieldType: {}
  });

});