JSFiddle - React, Tailwind, and code Playground
by mpusarla
HTML
<h2>Numeric cell type</h2>
<p>By default, Handsontable treats all cell values as <code>string</code> type. This is because <code><textarea></code>
returns a string as its value.</p>
<p>In many cases you will prefer cell values to be treated as <code>number</code> type. This allows to format
numbers nicely and sort them correctly.</p>
<p>To trigger the Numeric cell type, use the option <code>type: 'numeric'</code> in <code>columns</code> array
or
<code>cells</code> function.</p>
<p>Numeric cell type uses <a href="http://numeraljs.com/">Numeral.js</a> as the formatting library. Head over
to
their website to learn about the formatting syntax.</p>
<p>To use number formatting style valid for your language (i18n), load language definition to Numeral.js. See
"Languages" section in <a href="http://numeraljs.com/">Numeral.js docs</a> for more info.</p>
<div id="example1" class="handsontable"></div>
<p>
<button name="dump" data-dump="#example1" data-instance="hot" 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, price_usd: 6999.99, price_eur: 6999.99},
{car: "Citroen C4 Coupe", year: 2008, price_usd: 8330, price_eur: 8330},
{car: "Audi A4 Avant", year: 2011, price_usd: 33900, price_eur: 33900},
{car: "Opel Astra", year: 2004, price_usd: 7000, price_eur: 7000},
{car: "BMW 320i Coupe", year: 2011, price_usd: 30500, price_eur: 30500}
];
}
var container = document.getElementById('example1'),
hot;
hot = new Handsontable(container, {
data: getCarData(),
startRows: 7,
startCols: 4,
colHeaders: ['Car', 'Year', 'Price ($)', 'Price (€)'],
columnSorting: true,
columns: [
{
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' // this is the default locale, set up for USD
},
{
data: 'price_eur',
type: 'numeric',
format: '0,0.00 $',
language: 'de' // i18n: use this for EUR (German)
// more locales available on numeraljs.com
}
]
});
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();
});