Handsontable example

by Helmut Granda

HTML

<script src="https://docs.handsontable.com/pro/1.5.1/bower_components/handsontable-pro/dist/handsontable.full.min.js"></script>
<link rel="stylesheet" href="https://docs.handsontable.com/pro/1.5.1/bower_components/handsontable-pro/dist/handsontable.full.min.css">
<div>
  <div id="example1" class="hot handsontable htRowHeaders htColumnHeaders"></div>
</div>
<button class='filterData'>filter data</button>
<button class='newRow'>add row</button>
<button class='updateSettings'>update settings</button>

CSS

.handsontable td.newRowBlock {
  background: blue;
  color: white
}

JavaScript

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

  var dataObj = [
    ['AA', '10/10/2010', 'B'],
    ['AB', '10/10/2010', 'B'],
    ['AC', '10/10/2010', 'B'],
    ['AD', '10/10/2010', 'B'],
    ['C', '12/12/2012', 'D'],
    ['E', '01/01/2013', 'F']
  ];
  
  var example1 = document.getElementById('example1');
  
  var hot = new Handsontable(example1, {
    data: dataObj,
    columns: [
      {type: 'text'},
      {type: 'date', dateFormat: 'M/D/YYYY'},
      {type: 'numeric'}
    ],
    colHeaders: true,
    rowHeaders: true,
    dropdownMenu: true,
    filters: true
  });
  
  
var newRow = document.querySelector('.newRow');
newRow.addEventListener('click', function(){
	var newRow = 2;
  console.log('last visible row is ' + newRow);
	hot.alter('insert_row',newRow);
  //hot.getCellMeta(newRow, 0).className = 'newRowBlock';
  //hot.getCellMeta(newRow, 1).className = 'newRowBlock';
  //hot.getCellMeta(newRow, 2).className = 'newRowBlock';
  hot.render()
});

var filterData = document.querySelector('.filterData');
filterData.addEventListener('click', function() {
	hot.getPlugin('filters').addFormula(0, 'contains', ['A']);
	hot.getPlugin('filters').filter();
})

var updateSettings = document.querySelector('.updateSettings');
updateSettings.addEventListener('click', function() {

hot.updateSettings({
    cells: function cells(row, col, prop) {
        var indexId = 2;
        var cellProperties = {};
        if (row === indexId) {
            cellProperties.className = "newRowBlock";
        }

        if (row === indexId - 1) {
            cellProperties.className = "newRowBlock";
        }
        return cellProperties;
    }
});

});


});