Search code examples
globalizationhandsontable

Handsontable numeric cell globalization


I'm relatively new to js and now have to implement a handsontable into our project. This worked well so far, but I am hitting a roadblock with globalization.
Basically, we use comma as a decimal seperator, but when I try and copy something like "100,2" into a cell designated as 'numeric,' it will show as 1002.
If the same value is entered in a cell designated as 'text' and the type is changed to numeric afterwards, the value will be shown correctly.
For this I already had to add 'de' culture to the table sourcecode.(basically copying 'en' and changing the values currently relevant to me.)

    numeral.language('de', {
    delimiters: {
        thousands: '.',
        decimal: ','
    },//other non-relevant stuff here

When I copy the values directly from the table and insert them to np++ they show as 100.2 etc. However, when inserting them into handsontable the arguments-array looks as follows:

[Array[1], "paste", undefined, undefined, undefined, undefined]
    0: Array[4]
        0: 1       //row
        1: 1       //column
        2: "100.2" //previous value
        3: 1002    //new value

Here's what I have tried currently:

    hot.addHook("beforeChange", function () {
    if (arguments[1] === "paste") {
        hot.updateSettings({
            cells: function (row, col, prop) {
                var cellProperties = {
                    type: 'numeric',
                    language: 'en'
                };
                return cellProperties;
            }
        });
        //hot.updateSettings({
        //    cells: function (row, col, prop) {
        //        var cellProperties = {
        //            type: 'text',
        //        };
        //        return cellProperties;
        //    }
        //});
    }
}, hot);

hot.addHook("afterChange", function () {
    if (arguments[1] === "paste") {
        ChangeMatrixSettings(); //reset cell properties of whole table
    }

}, hot);

I hope I've made my problem clear enough, not sure if I missed something.

Are there any other ways to get the correct values back into the table? Is this currently not possible?

Thanks in advance.


Solution

  • You asked more than one thing, but let me see if I can help you.

    As explained in handsontable numeric documentation, you can define a format of the cell. If you want '100,2' to be shown you would format as follows

    format: '0.,'
    

    You can change that to what you really need, like if you are looking for money value you could do something like

     format: '0,0.00 $'
    

    The other thing you asked about is not on the latest release, but you can check it out how it would work here