hot-table update

hot-table update demonstration

by Nicolas Lips

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/handsontable/0.35.1/handsontable.full.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ngHandsontable/0.13.0/ngHandsontable.min.js"></script>
<div ng-app="my-app" ng-controller="MainCtrl as ctrl">

  <hot-table columns="ctrl.columns" datarows="ctrl.rows" row-headers="true" col-headers="true">
  </hot-table>
  <h2 ng-if="ctrl.rows.length == 0">
    Table vide !
  </h2>
  <p>
    Au bout de 5 secondes le dataStore commence a être initialisé (5 colonnes). Puis 5 secondes plus tard il est complétement initialisé (5 lignes). On peut rajouter autant de lignes et colonnes que l'on souhaite sans que la table ne soit supprimée et recréée.
  </p>

  <p>
    Add
    <input type="number" ng-model="ctrl.addRowsNumber" /> rows
    <button type="button" ng-click="ctrl.addRows(ctrl.addRowsNumber)">Ok</button>
  </p>
  <p>
    Add
    <input type="number" ng-model="ctrl.addColumnsNumber" /> columns
    <button type="button" ng-click="ctrl.addColumns(ctrl.addColumnsNumber)">Ok</button>
  </p>

</div>

CSS

@charset "UTF-8";

/*!
(The MIT License)

Copyright (c) 2012-2014 Marcin Warpechowski
Copyright (c) 2015 Handsoncode sp. z o.o. <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

.handsontable {
  position: relative
}

.handsontable .hide {
  display: none
}

.handsontable .relative {
  position: relative
}

.handsontable.htAutoSize {
  visibility: hidden;
  left: -99000px;
  position: absolute;
  top: -99000px
}

.handsontable .wtHider {
  width: 0
}

.handsontable .wtSpreader {
  position: relative;
  width: 0;
  height: auto
}

.handsontable table,
.handsontable tbody,
.handsontable thead,
.handsontable td,
.handsontable th,
.handsontable input,
.handsontable textarea,
.handsontable div {
  box-sizing: content-box;
  -webkit-box-sizing: content-box;
  -moz-box-sizing: content-box
}

.handsontable input,
.handsontable textarea {
  min-height: initial
}

.handsontable table.htCore {
  border-collapse: separate;
  border-spacing: 0;
  margin: 0;
  border-width: 0;
  table-layout:...

JavaScript

angular.module('my-app', ['ngHandsontable'])
  .controller("MainCtrl", ['dataStore', function(dataStore) {
    this.addRowsNumber = 5;
    this.addRows = dataStore.addRows;
    this.rows = dataStore.rows;
    this.columns = dataStore.columns;
    this.addColumnsNumber =  1;
    this.addColumns = dataStore.addColumns;
  }])
  .service("dataStore", ["$rootScope", function($rootScope) {
  	setTimeout(function () {
    	addColumns(5);
      $rootScope.$digest();
      setTimeout(function () {
        addRows(5);
        $rootScope.$digest();
      }, 5000);      
    }, 5000);
    var columns = [];
    var rows = [];
    function addRows(n) {
    	for (var i=0;i<n;i++) {
      	var newRow = {};
      	for (var j=0;j<columns.length;j++) {
        	var colName = columns[j].data;          
        	newRow[colName] = makeRandomString();
        }
        rows.push(newRow);
      }
    }
    function addColumns(n) {
    	for (var i=0;i<n;i++) {
      var colName = makeRandomString();
      	var newColumn = {
        	data: colName,
          title: colName
        };
        columns.push(newColumn);
      }
    }
    return {
      rows: rows,
      addRows: addRows,
      columns: columns,
      addColumns: addColumns
    };
  }]);

function makeRandomString() {
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < 5; i++)
    text += possible.charAt(Math.floor(Math.random() * possible.length));

  return text;
}