Handsontable example
by galvakojis
HTML
<div id="example1" class="hot handsontable htColumnHeaders"></div>
CSS
</style><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/handsontable.full.min.js"></script><link type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/handsontable.full.min.css">
JavaScript
document.addEventListener("DOMContentLoaded", function() {
function getCarData() {
return [
{car: "Mercedes A 160", year: 2011, price_usd: 7000, price_eur: 7000},
{car: "Citroen C4 Coupe", year: 2012, price_usd: 8330, price_eur: 8330},
{car: "Audi A4 Avant", year: 2013, price_usd: 33900, price_eur: 33900},
{car: "Opel Astra", year: 2014, price_usd: 5000, price_eur: 5000},
{car: "BMW 320i Coupe", year: 2015, price_usd: 30500, price_eur: 30500}
];
}
var cols = [
{
data: 'car'
// 1nd column is simple text, no special options here
},
{
data: 'year',
type: 'numeric'
},
{
data: 'price_usd',
type: 'numeric',
format: '$0,0.00',
language: 'en-US' // this is the default locale, set up for USD
},
{
data: 'price_eur',
type: 'numeric',
format: '0,0.00 $',
language: 'de-DE' // i18n: use this for EUR (German)
// more locales available on http://numbrojs.com/languages.html
}
];
function arraymove(arr, fromIndex, toIndex) {
var element = arr[fromIndex];
arr.splice(fromIndex, 1);
arr.splice(toIndex, 0, element);
}
var
container = document.getElementById('example1'),
hot;
hot = new Handsontable(container, {
data: getCarData(),
colWidths: 100,
colHeaders: true,
columnSorting : true,
columns: cols
});
hot.updateSettings({
contextMenu: {
callback: function(key, options) {},
items: {
"col-left": {
name: "Insert column left",
callback: (function() {
var getCol = hot.getSelected()[0][1];
var getColSet = cols.length;
cols.push({});
arraymove(cols, getColSet, getCol);
hot.updateSettings({
columns: cols
})
}),
//disabled: false
},
"col-right": {
name: "Insert column...