I'm experimenting with Handsontable add and remove column feature. But it is not working.
Here is my code :-
My index.html
<!DOCTYPE html>
<html ng-app="app">
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/handsontable/0.35.0/handsontable.full.js"></script>
<!--<link rel="stylesheet" type="text/css" href="css/style.css">-->
<script type="text/javascript" src="js/app.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/handsontable/0.35.0/handsontable.full.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/ngHandsontable/0.13.0/ngHandsontable.js"></script>
<!--<script type="text/javascript" src="data/datafactory.js"></script>-->
</head>
<body MainCtrl as ctrl>
<hot-table col-headers="true" datarows="ctrl.data">
<hot-column ng-repeat="column in ctrl.columns" data="{{column.data}}" title="column.title" read-only="column.readOnly"></hot-column>
</hot-table>
<button ng-click="ctrl.addColumn()">Add column</button>
<button ng-click="ctrl.removeColumn()">Remove column</button>
</body>
</html>
My app.js
function MainCtrl() {
var items = [[]];
this.data = items;
this.columns = [
{
data: 'id',
title: 'ID',
readOnly: true
},
{
data: 'price',
title: 'Price',
readOnly: false
}
];
this.addColumn = function() {
this.columns.push({});
};
this.removeColumn = function() {
this.columns.pop();
};
}
angular
.module('app', ['ngHandsontable'])
.controller('MainCtrl', MainCtrl);
MainCtrl.$inject = [];
When I run the code it doesn't work and doesn't allow to add or remove column. I am new to both angular and front-end design. So seeking help here.
I believe you need to order your <script>
tags properly - your code should go at the end (I imagine your code is in js/app.js
):
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/handsontable/0.35.0/handsontable.full.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/handsontable/0.35.0/handsontable.full.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/ngHandsontable/0.13.0/ngHandsontable.js"></script>
<script type="text/javascript" src="js/app.js"></script>
Also try using arrow functions where you can instead of function()... Or set up:
let ctrl = this;
You may get in trouble using this
the way you do it. Scope of this
can and sometimes will change.
Here's example of your code working in JS fiddle: https://jsfiddle.net/pegla/b6rj93cg/2/