【问题标题】:Handsontable numeric cell globalizationHandsontable 数字单元全球化
【发布时间】:2015-04-16 18:07:06
【问题描述】:

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

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

当我直接从表中复制值并将它们插入到 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);

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

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

提前致谢。

【问题讨论】:

    标签: globalization handsontable


    【解决方案1】:

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

    handsontable numeric documentation 中所述,您可以定义单元格的格式。如果您想显示“100,2”,您可以按如下格式设置

    format: '0.,'
    

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

     format: '0,0.00 $'
    

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

    【讨论】:

    • 感谢您的回答,我已经通过自定义验证方法解决了这个问题并发布了答案。您的解决方案也可以,但它并不能完全满足我目前对此的需求。
    【解决方案2】:

    由于我们对表格的其他要求主要是向用户显示无效输入,因此我已经实现了自己的输入验证。

    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 是一个数组,这是由于使用数组进行编辑事件的可动手操作的钩子。 parameter[2] 是旧值,如果在任何时候都需要它。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-13
      • 1970-01-01
      • 2017-04-02
      • 2015-08-26
      • 2010-11-18
      • 2013-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多