Handsontable example

by handsoncode

HTML

<!-- handson table -->
 
    <div id="handson"></div>
 
<!--
<script src="https://cdn.jsdelivr.net/npm/handsontable-pro@6.2.0/dist/handsontable.full.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/handsontable-pro@6.2.0/dist/handsontable.full.min.css" rel="stylesheet" media="screen">
-->
 
<script src="https://cdn.jsdelivr.net/npm/handsontable/dist/handsontable.full.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/handsontable/dist/handsontable.full.min.css" rel="stylesheet" media="screen">
 
<!--
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handsontable/0.19.0/handsontable.full.js"></script>
<script src="/app/assets/javascripts/handsontable-chosen-editor.js"></script>
-->

Babel + JSX

var data =  Handsontable.helper.createSpreadsheetData(10, 10)
 
/*
var dynamicColumns = ['Person', 'Certification'];
*/
var dynamicColumns = [ {readOnly: true},  {readOnly: true}];
 
for (var i = 0; i < 10 ; i++) {
    var col = new Object();
    //col.data = "Name" + i.toString();
    //col.title = "Name " + i.toString();
    col.type = "checkbox";
    col.renderer = checkboxRenderer; // Custom renderer for checkboxes
    dynamicColumns.push(col);
}
 
 
var container = document.getElementById('handson');
// var plugin = hot.getPlugin('hiddenColumns');
 
var hot = new Handsontable(container, {
  data: data,
  rowHeaders: false,
  colHeaders: true,
 // colHeaders: ["User", "Type", "Level 1 Approvers", "Level 2 Approvers" ], 
 
 colHeaders: [ 'Person', 'Certification', '-' ],
 
  contextMenu: true,
  contextMenu: ['undo', 'redo'],
 
 // dropdownMenu: true,
  dropdownMenu: ['filter_by_condition', 'filter_by_value', 'filter_action_bar'],
 
  stretchH: 'all',
   // enable filtering
  filters: true,
  licenseKey: 'e5270-20d53-75916-04418-0b00c',
  //columns: dynamicColumns
 
});
 
 
function checkboxRenderer(instance, td, row, col, prop, value, cellProperties) {
  // Create a checkbox input element
  const checkbox = document.createElement('input');
  checkbox.type = 'checkbox';
  checkbox.checked = value;
 
  // Handle the checkbox click event
  checkbox.addEventListener('click', (event) => {
    data[row][prop] = event.target.checked; // Update the data source
  });
 
  // Append the checkbox to the table cell
  td.appendChild(checkbox);
  td.style.textAlign = 'center'; // Center-align the checkbox
 
  Handsontable.renderers.CheckboxRenderer.apply(this, arguments); // Call the built-in checkbox renderer
}