column mapping 2

by kc11

HTML

<h2>Checkbox cell type</h2>

<p>If you have cells that contains only 2 possible values, you can use <code>checkbox</code> type.
  Data in such cells will be rendered as checkbox
  and can be easily changed by checking/unchecking the checkbox. </p>

<p>Checking and unchecking can be performed using mouse or by pressing <kbd>SPACE</kbd>.
  You can change the state of multiple cells at once.
  Simply select cells you want to change and press <kbd>SPACE</kbd></p>

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

<p>
  <button name="dump" data-dump="#example1" data-instance="hot1" title="Prints current data source to Firebug/Chrome Dev Tools">
    Dump
    data to console
  </button>
</p>

CSS

</style><!-- Ugly Hack due to jsFiddle issue: http://goo.gl/BUfGZ -->

<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>

<script src="http://handsontable.com/dist/handsontable.full.js"></script>
<script src="http://handsontable.com/lib/numeral.de-de.js"></script>
<link rel="stylesheet" media="screen" href="http://handsontable.com/dist/handsontable.full.css">
<link rel="stylesheet" media="screen" href="http://handsontable.com/demo/css/samples.css?20140331">
<link rel="stylesheet" media="screen" href="http://handsontable.com/demo/css/samples.css?20140331">
<link rel="stylesheet" media="screen" href="http://handsontable.com/demo/css/samples.css?20140331">
<link rel="stylesheet" media="screen" href="http://handsontable.com/demo/css/samples.css?20140331">

<style type="text/css">
body {background: white; margin: 20px;}
h2 {margin: 20px 0;}

JavaScript

$(document).ready(function () {

  function getCarData() {
    return [
      {car: "Mercedes A 160", year: 2006, available: true, comesInBlack: 'yes'},
      {car: "Citroen C4 Coupe", year: 2008, available: false, comesInBlack: 'yes'},
      {car: "Audi A4 Avant", year: 2011, available: true, comesInBlack: 'no'},
      {car: "Opel Astra", year: 2004, available: false, comesInBlack: 'yes'},
      {car: "BMW 320i Coupe", year: 2011, available: false, comesInBlack: 'no'}
    ];
  }    
    
    var keys = $.map(getCarData()[0], function(element,key) { return key; }); 
    console.log("keys ",keys);
    var columns = new Array();

    for(var i = 0; i < keys.length; i++) {
        
       
        
      columns[i] =  {data: keys[i], type: 'text'};
    }
    columns.unshift({data: "check", type: 'checkbox'});
    console.log("c ",columns);
    
    
    var addCheckKey=function(obj){
        obj.check='true';
        return obj;
    };
    var data=$.map(getCarData(),addCheckKey);
    
        var example1 = document.getElementById('example1');
        var hot1 = new Handsontable(example1,{
            data: data,
            startRows: 7,
            startCols: 5,
            colHeaders:['check','year','car','available','comesInBlack'],
            columns: columns,
           
            columnSorting: true,
           
        });
  
  function bindDumpButton() {
  
      Handsontable.Dom.addEvent(document.body, 'click', function (e) {
  
        var element = e.target || e.srcElement;
  
        if (element.nodeName == "BUTTON" && element.name == 'dump') {
          var name = element.getAttribute('data-dump');
          var instance = element.getAttribute('data-instance');
          var hot = window[instance];
          console.log('data of ' + name, hot.getData());
        }
      });
    }
  bindDumpButton();

});