手掌数字单元格全球化

Handsontable numeric cell globalization

我对 js 比较陌生,现在必须在我们的项目中实现一个 handsontable。 到目前为止效果很好,但我遇到了全球化的障碍。
基本上,我们使用逗号作为小数分隔符,但是当我尝试将“100,2”之类的内容复制到指定为 'numeric,' 的单元格中时,它将显示为 1002.
如果在指定为 'text' 的单元格中输入相同的值,然后将类型更改为数字,则该值将正确显示。
为此,我已经不得不将 'de' 文化添加到 table 源代码中。(基本上是复制 'en' 并更改当前与我相关的值。)

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

当我直接从 table 复制值并将它们插入 np++ 时,它们显示为 100.2 等。但是,当将它们插入 handsontable 时,参数数组如下所示:

[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

这是我目前尝试过的方法:

    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);

我希望我已经把我的问题说清楚了,不确定我是否遗漏了什么。

是否有任何其他方法可以将正确的值返回到 table?这目前不可能吗?

提前致谢。

你问的不止一件事,让我看看能不能帮到你。

handsontable numeric documentation 中所述,您可以定义单元格的格式。如果你想显示'100,2',你将格式化如下

format: '0.,'

您可以将其更改为您真正需要的,例如,如果您正在寻找金钱价值,您可以这样做

 format: '0,0.00 $'

你问的另一件事不在最新版本中,但你可以看看它是如何工作的here

由于我们对 table 的其他要求,主要是关于向用户显示无效输入,因此我实施了自己的输入验证。

function validateInputForNumeric(parameter) {
    var value = parameter[3];
    var row = parameter[0];
    var col = parameter[1];
    if (decimalSeperator === '') {
        var tmpculture = getCurrCulture();
    }

    if (value !== null && value !== "") {
        if (!value.match('([a-zA-Z])')) {
            if (value.indexOf(thousandSeperator) !== -1) {
                value = removeAndReplaceLast(value, thousandSeperator, ''); //Thousandseperators will be ignored
            }
            if (value.indexOf('.') !== -1 && decimalSeperator !== '.') {
                //Since numeric variables are handled as '12.3' this will customize the variables to fit with the current culture
                value = removeAndReplaceLast(value, '.', decimalSeperator);
            }
            //Add decimalseperator if string does not contain one
            if (numDecimalPlaces > 0 && value.indexOf(decimalSeperator) === -1) {
                value += decimalSeperator;
            }

            var index = value.indexOf(decimalSeperator)
            var zerosToAdd = numDecimalPlaces - (value.length - index - 1);
            for (var j = 0; j < zerosToAdd; j++) {
                //Add zeros until numberOfDecimalPlaces is matched for uniformity in display values
                value += '0';
            }
            if (index !== -1) {
                if (numDecimalPlaces === 0) {
                    //Remove decimalseperator when there are no decimal places
                    value = value.substring(0, index)
                } else {
                    //Cut values that have to many decimalplaces
                    value = value.substring(0, index + 1 + numDecimalPlaces);
                }
            }
            if (ErrorsInTable.indexOf([row, col]) !== -1) {
                RemoveCellFromErrorList(row, col);
            }

        } else {
            AddCellToErrorList(row, col);
        }
    }
    //console.log("r:" + row + " c:" + col + " v:" + value);
    return value;
}

inputParameter 是一个数组,因为 handsontable 钩子将数组用于编辑事件。 parameter[2] 是旧值,如果随时需要它。

即使从 Excel(2 秒到 4 秒)复制 2k 条记录,此代码也能相当快地运行。 我在执行速度方面的主要障碍之一是我使用 handsontable .getDataAtCell.setDataAtCell 方法进行检查。这些似乎不能很好地处理大型 tables(不是批评,只是观察)。这是通过 .getData 方法遍历数据修复的。