Handsontable example

by semix

HTML

<script src="https://docs.handsontable.com/pro/1.14.2/bower_components/handsontable-pro/dist/handsontable.full.min.js"></script>
<link rel="stylesheet" href="https://docs.handsontable.com/pro/1.14.2/bower_components/handsontable-pro/dist/handsontable.full.min.css">
<div id="example1" class="hot handsontable htColumnHeaders"></div>

CSS

* {
  font-family: Verdana, Helvetica, Arial, FreeSans, sans-serif;
  font-size: 12px;
}

.handsontable THEAD TH.color1 {
  background: #FF6138;
  border-color: #D4441F;
}

.handsontable THEAD TH.color1 .collapsibleIndicator {
  box-shadow: 0px 0px 0px 6px #FF6138;
  -webkit-box-shadow: 0px 0px 0px 6px #FF6138;
  background: #FF6138;
  border-color: #D4441F;
}

.handsontable THEAD TH.color2 {
  background: #79BD8F;
  border-color: #549A6B;
}

.handsontable THEAD TH.color2 .collapsibleIndicator {
  box-shadow: 0px 0px 0px 6px #79BD8F;
  -webkit-box-shadow: 0px 0px 0px 6px #79BD8F;
  background: #79BD8F;
  border-color: #549A6B;
}

.handsontable THEAD TH.color3 {
  background: #BEEB9F;
  border-color: #8FC768;
}

.handsontable THEAD TH.color3 .collapsibleIndicator {
  box-shadow: 0px 0px 0px 6px #BEEB9F;
  -webkit-box-shadow: 0px 0px 0px 6px #BEEB9F;
  background: #BEEB9F;
  border-color: #8FC768;
}

.handsontable THEAD TH.color4 {
  background: #FFFF9D;
  border-color: #DEDE9A;
}

.handsontable THEAD TH.color4 .collapsibleIndicator {
  box-shadow: 0px 0px 0px 6px #FFFF9D;
  -webkit-box-shadow: 0px 0px 0px 6px #FFFF9D;
  background: #FFFF9D;
  border-color: #DEDE9A;
}

.handsontable THEAD TH.color5 {
  background: #00A388;
  border-color: #38887B;
}

.handsontable THEAD TH.color5 .collapsibleIndicator {
  box-shadow: 0px 0px 0px 6px #00A388;
  -webkit-box-shadow: 0px 0px 0px 6px #00A388;
  background: #00A388;
  border-color: #38887B;
}

JavaScript

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

  var example1 = document.querySelector('#example1'),
    example1Hot;

  example1Hot = new Handsontable(example1, {
    data: Handsontable.helper.createSpreadsheetData(20, 10),
		rowHeaders: true,
		colHeaders: true,
    hiddenColumns: true,
    columnHeaderHeight: [100, 50, 70, 100],
    nestedHeaders: [
      ['A', {
        label: 'B',
        colspan: 8
      }, 'C'],
      ['D', {
        label: 'E',
        colspan: 4
      }, {
        label: 'F',
        colspan: 4
      }, 'G'],
      ['H', {
        label: 'I',
        colspan: 2
      }, {
        label: 'J',
        colspan: 2
      }, {
        label: 'K',
        colspan: 2
      }, {
        label: 'L',
        colspan: 2
      }, 'M'],
      ['N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W']
    ],
    collapsibleColumns: true,
    width: 500,
    height: 400,
    columnSorting: true,
    sortIndicator: true,
    afterGetColHeader: function(col, TH) {
      var TR = TH.parentNode;
      var THEAD = TR.parentNode;
      var headerLevel = (-1) * THEAD.childNodes.length + Array.prototype.indexOf.call(THEAD.childNodes, TR);

      function applyClass(elem, className) {
        if (!Handsontable.dom.hasClass(elem, className)) {
          Handsontable.dom.addClass(elem, className);
        }
      }
      
      console.log(col)
			
			// first level from the top
      if (headerLevel === -4) {
        applyClass(TH, 'color1');

        // second level from the top
      } else if (headerLevel === -3) {

        if (col === 5) {
          applyClass(TH, 'color2');
        } else if (col === 1) {
          applyClass(TH, 'color3');
        }

        // third level from the top
      } else if (headerLevel === -2) {

        if (col === 5 || col === 7) {
          applyClass(TH, 'color2');
        } else if (col === 1) {
          applyClass(TH, 'color4');
        } else if (col === 3) {
          applyClass(TH, 'color5');
        }

        //...